Close JFrame with a "really close?" dialog - java

I have a JFrame and if I press the close button in the top right corner I invoke a JDialog and ask if the user really wants to close.
This is the ActionListener on the close Button in my main JFrame:
...
//close listener
addWindowListener(new WindowAdapter() {//invoke "wirklich schließ3n" window if Alt+F4
#Override
public void windowClosing(WindowEvent event) {
MainGuiWindow.this.saveSettings();
CloseDialog cd = new CloseDialog(MainGuiWindow.this);
if (cd.getResult()) {
System.exit(0);
} else {
//MainGuiWondow is "setVisible(false)" but still running | I don't know how to fix it
MainGuiWindow.this.setVisible(true);//doesn't work
}
}
});
The JDialog has just two buttons (YES/NO) (after pressing any button i call dispose(); in the ActionListeners of the Buttons in the JDialog and after the dispose(); i return the answers:
yes returns true
no returns false
Can anybody tell me what to do in the else case of my Close ActionListener in my MainGuiWindow.
The CloseDialog is closed cause of the dispose(); but the main window is is setVisible(false); but still running

This my help you
You need to set default close operation on the JFrame to “do nothing”:
jFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
add a WindowListener for the frame.
jFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
handleClosing();
}
});
And Refer Here

Related

How to do action when closing the frame?

How do I do action when I'm going to terminates the programme.
For example :
When I'm going to terminates the programme, I want to set
if( the x button is clicked or something )
deleteRow from database;
else
just end.
Is it should be done inside the ui frame ?
Use window listeners for that. windowClosing method is used to execute code when window is closing, windowClosed method can be used if you want to do something after the window has closed.
JFrame window=new JFrame("Window");
window.addWindowListener(new WindowAdapter(){
#Override
public void windowClosed(WindowEvent evt)
{
//Do something after window has closed.
}
public void windowClosing(WindowEvent evt)
{
//Do something when window is closing.
//Useful when you have to access data in window(buttons, textfields etc)
}
});
There are also other window event listeners.Documentation of WindowListener
addWindowListener(new WindowAdapter()
{
#Override
public void windowClosing(WindowEvent e)
{
.....
}
});

Disble the Maximize button in JFrame

My JFrame opens in a minimized Mode but it can be maximized.
I want to disable the maximize icon so that user cannot maximize the frame and can see it only in the minimized or default mode.
Is it possible?
Use frame.setResizable(false). It disables the maximize button but let the the 'close' and 'minimize' buttons active.
Use this in your code, try using JDialog instead of JFrame for main window.
frame.setResizeable(false);
there is no direct way to remove the maximize button off the
Resizable JFrame as it is created by Windows and it is not painted
using Swing so U can’t touch this.
so you can replace JFrame with JDialog or remove the title bar and implement customized title bar.
You can disable it by using frame.setResizeable(false); or you can remove it completely by using following code
public class Test extends JDialog {
public Test(JFrame frame, String str) {
super(frame, str);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
}
public static void main(String[] args) {
try {
Test myFrame = new Test(new JFrame(), "Removing maximize button");
JPanel panel = new JPanel();
panel.setSize(100, 100);
myFrame.add(panel);
myFrame.setSize(100, 100);
myFrame.setVisible(true);
} catch (IllegalArgumentException e) {
System.exit(0);
}
} }

How to tell if a JFrame window has already been opened?

I have a JFrame with a Button that opens a different JFrame. But I want the button to only open the second frame once. Problem is, every time I click it I get a new instance of the frame. This must be a very common problem, since I'm following a book on how to create this GUI. I find it odd that the author didn't mention this "feature".
So how do I keep my button from opening multiple copies of the new frame?
Instead of letting the button create a new JFrame every time, make the second JFrame a member of the first JFrame, and only let the button call jframe2.setVisible(true);:
class JFrame1 {
JFrame2 jframe2=...;
JButton button=...;
JFrame1() {
...
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jframe2.setVisible(true);
}
});
...
}
}
UPDATED!
try this:
JFrame frame2 = new JFrame(); // instance variable
...
//when button is clicked
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if(!frame2.isVisible())
frame2.setVisible(true);
}
});
make sure you are handling the closing of all of the JFrames manually like this:
frame2.addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
// handle closing the window
frame2.setVisible(false);
frame2.dispose();
}
});
instead of using the JFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
hope this helps.
You should keep a reference to the sub frame you open for first time. At second time you first check if you have a reference or not and then decide to create a new frame or to put the focus onto the existing open frame.
Example as answer to comment of OP (similar to other answer of #AlexanderTorstling, but not immediately creating the sub frame):
class MainFrame extends JFrame {
private JFrame subFrame = null;
MainFrame() {
...
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (subFrame == null) {
subFrame = new JFrame();
...
}
subFrame.setVisible(true);
}
});
}
}
This example has also the advantage to give you the possibility to close the subframe via a registered WindowAdapter if the main frame is closed.
please try this one
JFrame frame2 = new JFrame(); // instance variable
boolean secondWindowIsOpne = false;
...
//when button is clicked
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if(secondWindowIsOpne == false){
frame2.setVisible(true);
secondWindowIsOpne = true;
}
else{
System.out.println("This Window is already running");
}
});
make sure you are handling the closing of all of the JFrames manually like this:
frame2.addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
// handle closing the window
secondWindowIsOpne = false;
frame2.setVisible(false);
frame2.dispose();
}
});

setDefaultCloseOperation not working in Swing

I need to stop the default operation of window being closed when red x mark is clicked on the swing window. I am using the JDialog and adding WindowsListener to it to capture the WindowClosing event, there I decide whether to dispose JDialog or to not dispose it, I am also setting the following:
setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
But still when I click on the red x mark, the window closes. Any ideas?
Adding Window listener to the JDialog gave me the power to handle the window actions and I works fine in my application.
You can try creating a WindowListener and do nothing when the close buttion is clicked.
jdialog.addWindowListener(new WindowAdapter()
{
public void windowClosed(WindowEvent e)
{
}
public void windowClosing(WindowEvent e)
{
}
});

Java. Swing. Disable JFrame close button

Is it possible to disable JFrame close operation?
EDIT.
frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(
new WindowAdapter() {
#Override
public void windowClosing(final WindowEvent e) {
}
});
And this code doesn't solve the problem.
EDIT
Is it possible to consume WindowClosing event?

Categories