I need to set to open a different JFrame as the defaultCloseOperation of the JFrame that I'm currently working.
Can I do that in Netbeans ?
http://docs.oracle.com/javase/7/docs/api/java/awt/event/WindowListener.html
Add a WindowListener to the first frame and when it closes, make the seconds frame and close this first frame. Then set defaultCloseOperation to DO_NOTHING_ON_CLOSE.
Non-working code just so you get the idea:
public class FirstFrame extends JFrame implements WindowListener {
(other stuff)
#Override
public void windowClosed(WindowEvent e) {
//make second frame
dispose();
}
}
Related
I have a JFrame that has some JLabels, and when I click any JLabel, it opens another JFrame but I want the other to stay disabled. First I did:
JFrame frame2 = new JFrame();
frame2.setVisible(true);
this.setEnabled(false);
Then I want to setEnabled(true) the 1st frame when I close the 2nd frame. What I did is giving the 2nd frame a JFrame attribute and giving a param JFrame in its Constructor, and with a WindowListener I use the method windowClosing() like this:
public class My2ndJFrame extends JFrame implements ActionListener, WindowListener {
My1stJFrame frame;
public My2ndJFrame(My1stJFrame frame) {
this.frame = frame;
//moreCodeWeDontNeedNow
}
#Override
public void windowClosing(WindowEvent e) {
frame.setEnabled(true);
}
It works but I dont know if this is good programming practice, so I would like to know if there's a better way and if this is not good, why. Thank you.
I'm writing a GUI. I want to print something after I close. But the WindowListener does not work. I write a window then I want to have a boolean to mark the windows is closed. So after I can use that boolean in if clause to write the next statement which should be executed after windows close.
public class MyFrame extends JFrame implements WindowListener {
private boolean close=false;
MyFrame() {
this.close=false;
setSize(300,250);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
addWindowListener(this);
// after help, I've added addWindowListener although still don't
//work
}
public void windowOpened()/windowIconified()/windowDeiconified()...
public void windowClosing(WindowEvent e) {close=true;}
public void windowClosed(WindowEvent e) {close=true;}
}
public class MyFrameControl {
MyFrame setframe() {
MyFrame fr = new MyFrame();
fr.setVisible(true);
return fr;
}
}
public class test {
public static void main(String args[]) {
MyFrameControl frameCtrl= new MyFrameControl();
MyFrame tmpFrame = frameCtrl.setframe();
if(tmpFrame.close==true) {
System.out.println("close is true");
}
}
}
When I close the JFrame/MyFrame, isn't the boolean close supposed to be true when the print line is executed?
Still can't work under JFrame after adding addWindowListener, is there any solution under JFrame other than switching to JDialogue?
No.
Your code is not waiting for the JFrame to be closed again and like #Abra already mentioned, you never add the WindowListener to the JFrame.
Your k.setVisible(true) returns immediately and your frame remains visible.
So no windowClosing()/windowClosed() is called at the time the condition is checked.
Maybe you could transform your JFrame to a JDialog which can be set to modal (setModal()/setModalityType()). A modal dialog's setVisible() returns after the dialog has been closed.
To simulate this with a JFrame you have somehow to wait for the close to occur. You could put your System.out... in a thread (Runnable) which waits until close==true.
A JFrame is not modal by definition (How to make a JFrame Modal in Swing java)
I made a JFrame in Java (Netbeans) with a button. When pressing that button another JFrame opens and the first frame has setEnabled to false. When I close my second Frame, I want the first one to be enabled again.... how can I do that?
Assuming you have JFrame subclass objects, you can pass your first frame as an argument to your second frame, and enable the first frame again in the close event of your second frame.
MyJFrame class:
private MyJFrame alphaFrame;
public MyJFrame(MyJFrame alpha) {
alphaFrame = alpha;
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
alphaFrame.setEnabled(true);
}
});
}
You can then instantiate your second frame (from the first, I assume) with something like this:
MyJFrame secondFrame = new MyJFrame(this);
I have a subclass of JFrame that uses a class extended from JPanel
public class HelloWorld extends JPanel implements KeyListener
I add an object of HelloWorld to the frame - app.add(helloWorld);. Now, when I press any keyboard key non of the KeyListener methods gets called and it seems that helloWorld doesn't have window focus. I have tried also to invoke helloWorld.requestFocusInWindow(); but still doesn't respond.
How can I make it respond to key press?
Did you set that KeyListener for your HelloWorld panel would be that panel itself? Also you probably need to set that panel focusable. I tested it by this code and it seems to work as it should
class HelloWorld extends JPanel implements KeyListener{
public void keyTyped(KeyEvent e) {
System.out.println("keyTyped: "+e);
}
public void keyPressed(KeyEvent e) {
System.out.println("keyPressed: "+e);
}
public void keyReleased(KeyEvent e) {
System.out.println("keyReleased: "+e);
}
}
class MyFrame extends JFrame {
public MyFrame() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(200,200);
HelloWorld helloWorld=new HelloWorld();
helloWorld.addKeyListener(helloWorld);
helloWorld.setFocusable(true);
add(helloWorld);
setVisible(true);
}
public static void main(String[] args) {
new MyFrame();
}
}
JPanel is not Focusable by default. That is, it can not respond to focus related events, meaning that it can not respond to the keyevents.
I would suggest trying to setFocusable on the pane to true and trying again. Make sure you click the panel first to make sure it receives focus.
Understand though, you WILL get strange focus traversal issues, as the panel will now receive input focus as the user navigates through your forms, making it seem like the focus has been lost some where.
Also, KeyListeners tend to be unreliable in this kind of situation (due to the way that the focus manager works).
simple you have to add
addKeylistener(new HelloWorld());
add this in MyFrame method;
HelloWorld() helloWorld = new HelloWorld();
this.addKeyListener(helloWorld);
How could I create a window which is modal and has a maximize button?
So is it possible to create a modal JFrame or create a JDialog with maximize button?
On most look and feels, modal windows (such as JDialog) do not have a maximise button simply because they're not supposed to be maximised (or minimised) at all.
It's possible with some tricks to add a maximise button, but it would be completly against the way JDialog is supposed to work.
If you need a maximise button, the best solution would be using a JWindow or a JFrame instead of a JDialog. Those windows support maximisation and minimisation.
WARNING: You shouldn't do that, no matter what.
A trick to do this in JDialog:
setUndecorated(true);
getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
Solution 1: Tested on Windows
I used a JFrame for the modal window
JFrame mainWindow = new JFrame;
mainWindow.setVisible(true);
JFrame modalWindow = new JFrame();
// The next two sentences gives modalWindow modal beahaviour
mainWindow.setEnabled(false);
mainWindow.setFocusable(false);
modalWindow.setVisible(true);
Solution 2: Tested on Ubuntu
I added a WindowFocusListener
addWindowFocusListener(new java.awt.event.WindowFocusListener() {
public void windowGainedFocus(java.awt.event.WindowEvent evt) {}
public void windowLostFocus(java.awt.event.WindowEvent evt) {
formWindowLostFocus(evt);}
private void formWindowLostFocus(java.awt.event.WindowEvent evt) {
this.requestFocus();
this.toFront();}
Here is an alternate / slightly more detailed answer.
Try Are You Missing Maximize Button? (formerly here). This is a github archive of blog articles and code by Santhosh Kumar Tekturi from the now defunct JRoller site.
It is a complete utility class that makes a Frame mimic a Dialog, similar to the other answers. It involves adding a WindowListener to the Frame to keep the frame on top of its owner and keep its owner frame disabled (warning: in the windowClosed method it should probably be frame.removeWindowListener(this);, and a WindowListener to the owner to keep the frame on top of it and to remove the listener. It also uses its own EventQueue to process events. Note this is an old post, so as mentioned in the code there may be newer APIs to deal with this code better.
Here is the core function. See the link for the rest. Note: the code in the repository differs from the article; I believe the repository is more developed.
// show the given frame as modal to the specified owner.
// NOTE: this method returns only after the modal frame is closed.
public static void showAsModal(final Frame frame, final Frame owner){
frame.addWindowListener(new WindowAdapter(){
public void windowOpened(WindowEvent e){
owner.setEnabled(false);
}
public void windowClosing(WindowEvent e) {
owner.setEnabled(true);
}
public void windowClosed(WindowEvent e){
frame.removeWindowListener(this); // originally called on owner
}
});
owner.addWindowListener(new WindowAdapter(){
public void windowActivated(WindowEvent e){
if(frame.isShowing()){
frame.setExtendedState(JFrame.NORMAL);
frame.toFront();
}else
owner.removeWindowListener(this);
}
});
owner.toFront();
frame.setVisible(true);
try{
new EventPump(frame).start();
} catch(Throwable throwable){
throw new RuntimeException(throwable);
}
}