Java hide JFrame1 and JFrame2, when JFrame0 is deactivated? - java

is there a way to hide all the other JFrames of my application, when the user clicks out of the "mainFrame"?
I tried with this
public void windowActivated(WindowEvent we) {
frame1.setVisible(true);
frame.setVisible(true);
}
public void windowDeactivated(WindowEvent we) {
frame1.setVisible(false);
frame2.setVisible(false);
}`
but this doesn't work. All of my Windows start blinking. I cannot set JFrame2 unfocusable.
Is there any other way to do this?

Use non-modal dialogs instead and the problem is sorted by default.
import javax.swing.*;
class TestDialogMinimize {
public static void main(String[] args) {
Runnable r = new Runnable() {
public void run() {
JFrame f = new JFrame("Has a Dialog");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(400,400);
JDialog d = new JDialog(f);
d.setSize(200,200);
f.setVisible(true);
d.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}

The non-modal dialog suggestion in this answer is one way to go. See also this answer elsewhere.
If for some reason you need to continue using frames, you can minify them with
frame1.setState(Frame.ICONIFIED)
and raise them with
frame1.setState(Frame.NORMAL)
Handle these in a code block like:
frame0.addWindowStateListener(new WindowStateListener() {
#Override
public void windowStateChanged(WindowEvent e) {
// handle change
}
});
as described in this question's answers.
If you want to close all frames when the frame0 is closed, you can use:
frame0.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
to exit the program and close all frames when the frame0 is closed. If you are just hiding on close use a window listener. You can use frame1.setVisible(false) in your WindowListener.

Related

How to hide/Shoe Jframe in NetBeans?

I am designing a project in NetBeans. For that I need to display a jdialog as a login window at first. But if I run the project both jframe and jdialog will appear. How can I hide jframe and how to set jdialog as a starting one in my project?
I wrote
LoginDlg.setVisible(true);
this.setVisible(false);
In construcor.
Start by taking a look at How to Make Dialogs
You want to make the JDialog a modal dialog, so that it will stop the execution of additional code.
In your program launch code, you want to show the dialog first, then, based on what the user does, show the main frame.
Alternatively, you could use a CardLayout, see How to Use CardLayout for more details, which will allow you to switch the active view based on your needs
In your main method, show only the dialog. Then add a button or something(e.g timer) that hides the dialog, and add a listener to that dialog so when the dialog disposes the main frame is shown.
public class JavaApplication3{
private static JFrame f = new JFrame("alabala");
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
final JDialog d = new JDialog();
d.add(new JLabel("pla"));
d.setVisible(true);
Timer t1 = new Timer(1000, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
d.dispose();
}
});
t1.setRepeats(false);
t1.start();
d.addWindowListener(new WindowAdapter() {
#Override
public void windowClosed(WindowEvent e) {
f.pack();
f.setVisible(true);
}
});
}
}

How to perform a task before a window/frame is closed?

The JFrame provides a method setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) . I want to run some code before this Frame is actually closed. How do I do that ?
JFrame frame = new JFrame();
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
// The window is closing
}
});
Implement WindowListener and capture window closing event. Something like-
yourWindow.addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
// do something here
}
});
Do this
frame.addWindowsListener(new WindowAdapter() {
public void windowClosing() {
// do your work here
frame.dispose();
}
}
Please refer WindowListener documentation

awt window not closing when close button is clicked

I implemented a sample class for a Virtual KeyBoard and ran this VirtualKeyboardTest.The keyboard appears but the main problem is that it is not closing properly when the x button is clicked.How can i rectify this?
import java.awt.*;
import java.awt.event.*;
public class VirtualKeyboardTest
{
public static void main(String args[])
{
VirtualKeyboard vk = new VirtualKeyboard();
vk.setSize(500,300);
vk.setVisible(true);
Frame f1 = new Frame();
f1.addWindowListener( new WindowAdapter() {
#Override
public void windowClosing(WindowEvent we) {
System.exit(0);
}
} );
}
}
You code is incorrect. Instead of
f1.addWindowListener( new WindowAdapter() {
...
try
vk.addWindowListener( new WindowAdapter() {
...
This will close your window.
It's better to use the method public void dispose()
vk.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
vk.dispose(); // use dispose method
}
}
);
AWT is heavyweight i.e. its components uses the resources of system.
Windows are non-blocking. Meaning that once you create one in code, your code continues to execute.
This means that your Window probably goes out of scope immediately after creation, unless you explicitly stored a reference to it somewhere else. The Window is still on screen at this point.
This also means you need some other way to get rid of it when you're done with it. Enter the Window dispose() method, which can be called from within one of the Window's listeners.
Check this:
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
It tells basically the windows manager to shutdown your app when "X" is pressed.

How to capture a JFrame's close button click event?

I want to call a method confirmExit() when the red close button of the title bar of a JFrame is clicked.
How can I capture that event?
I'd also like to prevent the window from closing if the user chooses not to proceed.
import javax.swing.JOptionPane;
import javax.swing.JFrame;
/*Some piece of code*/
frame.addWindowListener(new java.awt.event.WindowAdapter() {
#Override
public void windowClosing(java.awt.event.WindowEvent windowEvent) {
if (JOptionPane.showConfirmDialog(frame,
"Are you sure you want to close this window?", "Close Window?",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION){
System.exit(0);
}
}
});
If you also want to prevent the window from closing unless the user chooses 'Yes', you can add:
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
Override windowClosing Method.
public void windowClosing(WindowEvent e)
It is invoked when a window is in the process of being closed. The close operation can be overridden at this point.
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
also works. First create a JFrame called frame, then add this code underneath.
This may work:
jdialog.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
System.out.println("jdialog window closed event received");
}
public void windowClosing(WindowEvent e) {
System.out.println("jdialog window closing event received");
}
});
Source: https://alvinalexander.com/java/jdialog-close-closing-event
This is what I put as a menu option where I made a button on a JFrame to display another JFrame. I wanted only the new frame to be visible, and not to destroy the one behind it. I initially hid the first JFrame, while the new one became visible. Upon closing of the new JFrame, I disposed of it followed by an action of making the old one visible again.
Note: The following code expands off of Ravinda's answer and ng is a JButton:
ng.addActionListener((ActionEvent e) -> {
setVisible(false);
JFrame j = new JFrame("NAME");
j.setVisible(true);
j.addWindowListener(new java.awt.event.WindowAdapter() {
#Override
public void windowClosing(java.awt.event.WindowEvent windowEvent) {
setVisible(true);
}
});
});
Try this:
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
It will work.

wait for jdialog to close

I have a class FilePathDialog which extends JDialog and that class is being called from some class X. Here is a method in class X
projectDialog = new FilePathDialog();
projectDialog.setVisible(true);
projectDialog.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.out.println("Window closing");
try {
doWork();
} catch (Throwable t) {
t.printStackTrace();
}
}
public void windowClosed(WindowEvent e) {
System.out.println("Window closed");
try {
doWork();
} catch (Throwable t) {
t.printStackTrace();
}
}
});
doWork never gets called when the JDialog window closes. All I'm trying to do is wait for the JDialog to close before it proceeds in the method. I also tried using SwingWorker and Runnable but that did not help.
Again, the key is is the dialog modal or not?
If it's modal, then there's no need for a WindowListener as you will know that the dialog has been dealt with since code will resume immediately below your call to setVisible(true) on the dialog. i.e., this should work:
projectDialog = new FilePathDialog();
projectDialog.setVisible(true);
doWork(); // will not be called until the dialog is no longer visible
If on the other hand it's mode-less, then a WindowListener should work, and you've likely got another problem in code not shown here, and you'll want to post an sscce for us to analyze, run, and modify.
Edit for gpeche
Please check out this SSCCE that shows that the 3 types of default closing parameters will trigger the window listener:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DialogClosing {
private static void createAndShowGui() {
JFrame frame = new JFrame("DialogClosing");
JPanel mainPanel = new JPanel();
mainPanel.add(new JButton(new MyAction(frame, JDialog.DISPOSE_ON_CLOSE, "DISPOSE_ON_CLOSE")));
mainPanel.add(new JButton(new MyAction(frame, JDialog.HIDE_ON_CLOSE, "HIDE_ON_CLOSE")));
mainPanel.add(new JButton(new MyAction(frame, JDialog.DO_NOTHING_ON_CLOSE, "DO_NOTHING_ON_CLOSE")));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
class MyAction extends AbstractAction {
private JDialog dialog;
private String title;
public MyAction(JFrame frame, int defaultCloseOp, final String title) {
super(title);
dialog = new JDialog(frame, title, false);
dialog.setDefaultCloseOperation(defaultCloseOp);
dialog.setPreferredSize(new Dimension(300, 200));
dialog.pack();
dialog.addWindowListener(new WindowAdapter() {
#Override
public void windowClosed(WindowEvent e) {
System.out.println(title + " window closed");
}
#Override
public void windowClosing(WindowEvent e) {
System.out.println(title + " window closing");
}
});
this.title = title;
}
#Override
public void actionPerformed(ActionEvent arg0) {
dialog.setVisible(true);
}
}
Javadoc for WindowListener.windowClosed():
Invoked when a window has been closed as the result of calling dispose on the window.
And for JDialog.setDefaultCloseOperation():
Sets the operation which will happen by default when the user initiates a "close" on this dialog. The possible choices are:
DO_NOTHING_ON_CLOSE - do not do anything - require the program to handle the operation in the windowClosing method of a registered WindowListener object.
HIDE_ON_CLOSE - automatically hide the dialog after invoking any registered WindowListener objects
DISPOSE_ON_CLOSE - automatically hide and dispose the dialog after invoking any registered WindowListener objects
The value is set to HIDE_ON_CLOSE by default.
That suggests that you should call projectDialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); after instantiating FilePathDialog.
answer to your question
add WindowListener to your JDialog (JDialog.DO_NOTHING_ON_CLOSE) , on windowClosing event to try to run your code, if ends with success then call dispose(), or setVisible(false)
I don't agreed with your idea, another workaround
create only one JDialog (JDialog.DO_NOTHING_ON_CLOSE) with WindowListener, and re-use that for another Action, final action will be always setVisible(false), if your code ends with success, then remove the child(s) from JDialog, your JDialog is prepared for another job

Categories

Resources