Closing a swing application once code is complete - java

This is no doubt a simple question for expert 'swingers' but after some research I've not come up with anything.
I have code in this form:
public static void main(Strings[] args) {
final JFrame frame = new JFrame("Symbol Formatter");
final JButton button = new JButton("Begin");
final JFileChooser filechoose = new JFileChooser();
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
filechoose.showOpenDialog(frame);
filename = filechoose.getSelectedFile();
doSomething(filename);
button.setEnabled(false);
frame.setVisible(false); //edit for Andrew Thompson
}
});
JPanel panel = new JPanel();
panel.add(button);
frame.add(panel);
frame.pack();
frame.setSize(250, 75);
frame.setLocationByPlatform(true);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
}
I want to close the swing application once doSomething(filename) is completed. At the moment, the original frame remains where it is and the application only terminates when the frame is closed manually. How can I close once doSomething(filename) has run once.
I'm sure this is a simple question and just requires changing setDefaultCloseOperation however, the Jframe documentation hasn't helped me much.
Also, is there a way to add a title to the showOpenDialog? So that is has something like "Choose library file" as the title

You could use:
frame.dispose()

Call..
frame.setVisible(false);

I believe if you change the default close operation to
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Calling dispose should terminate the application & exit the JVM.
If that fails, as has been mentioned, call System.exit(0) will certainly terminate te JVM

For the file chooser dialog title, see the javadoc.

Related

How to let the user stop a java application

I have an application which mainly uses a bot to press keys when a condition is met. I am used to working with applets so I am wondering about how the user stops the application. I did it by using the task manager but that is not very user friendly, is it?
So how can I make I UI for a standalone java application? Or is there an other way to give the user the opportunity to stop the application?
You need to add a button to the GUI, set an action listener to it and in the addActionListener call the System.exit() method..
Example:
JButton showDialogButton = new JButton("Exit the app");
showDialogButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
Do you have a JFrame?
If not Create a new JFrame, JPanel, and JButton with:
JFrame frame = new JFrame("title");
JPanel panel = new JPanel();
JButton button = new JButton("Close");
//You need this for the screen to show.
frame.setSize(width,height);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
//add the panel to the frame and the button to the panel
frame.add(panel);
panel.add(button);
button.addActionListener(this);//the class needs to implements ActionListener
frame.setVisible(true);
//this is the actionperformed method which will run if the button is clicked.
public void actionPerformed(ActionEvent e) {
if(e.getSource==button){//If you have more than one button.
System.exit(0);
}
}

JFrame: All the frames close when I try to close just one of them

public class Scratch {
public static void main(String[] args) {
Dimension d = new Dimension(300,300);
JFrame frame1 = new JFrame("Frame-1");
JFrame frame2 = new JFrame("Frame-2");
frame1.setSize(250,250);
frame2.setSize(d);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setVisible(true);
frame2.setVisible(true);
}
}
When I run this, the two frames show up as expected, but when I close one of them, both of them close. I want to achieve the functionality where only the frame I click 'x' on closes and the other remains open until I click the 'x' on it. How do I do it?
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
The "EXIT" tells the JVM to stop so all the windows are closed:
So you could be using:
frame1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Then only the frame you click on will close. When both frames are closed the JVM will exit.
However, that is not a good solution. An application should generally only have a single JFrame and then use a JDialog for a child window. Using this approach the code would be:
JDialog dialog = new JDialog(frame1);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
Depending on you requirement you would make the dialog modal or non-modal.
Using this approach the JVM will exit when you close the frame however it will stay open when you close a child dialog.
Read this forum question about multiple JFrames: The Use of Multiple JFrames: Good or Bad Practice?. It will give more thoughts on why using 2 JFrames is not a good idea.
public class Scratch {
public static void main(String[] args) {
Dimension d = new Dimension(300,300);
JFrame frame1 = new JFrame("Frame-1");
JFrame frame2 = new JFrame("Frame-2");
frame1.setSize(250,250);
frame2.setSize(d);
frame1.setVisible(true);
frame2.setVisible(true);
}
}
you add one of them to your frame.
setVisible(false);
dispose();
setDefaultCloseOperation(DISPOSE_ON_CLOSE);

JFrame stuck, i can't move across sreen

I made a frame with some labels and a button on it, created an executable ear and now when i start it i can't move the application on my screen. Sometimes it works, sometimes it doesn't. I just can't drag the thing..
JFrame frame = new JFrame("Name");
frame.setPreferredSize(new Dimension(260, 160));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton exitButton = new JButton("Exit");
exitButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
frame.add(exitButton, BorderLayout.SOUTH);
frame.add(vertBox, BorderLayout.CENTER);
frame.setVisible(true);
frame.pack();
frame.setLocationRelativeTo(null);
Any ideas what's wrong? It's really annoying that i can't move it.
P.S. There is a code part that i skipped, where i made the labels and added them
Perhaps (in the code you didn't show) you were disabling your frame through
frame.setEnabled(false);
PS: I know it has been 2 years since you asked the question, but it might help someone else

Closing a JFrame using a button in eclipse

I'm a rookie when it comes to programming. We have this project about a log-in profile account. I just started doing it; and I came across a specific problem. I want to close a frame using a button.
buttonenter.setText("Enter");
buttonenter.addActionListener(new ActionListener (){
public void actionPerformed (ActionEvent ae){
}
});
I tried placing my frame.dispose();, set.Visible(false) etc. but i just got an error. I don't quite get. I really appreciate the help! Thank you!
Here's a simple example of what you're trying to do. What errors are you receiving?
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton btnClose = new JButton("CLOSE");
btnClose.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.setVisible(false);
}
});
frame.getContentPane().add(btnClose, BorderLayout.NORTH);
}
from JButtons ActionListener you can to call
JFrame#dispose(); (terminating current JVM)
JFrame#setVisible(false); (hide JFrame)
System.exit(0); (terminating current JVM)
and/or with (another standard ways are)
setDefaultCloseOperation();
add WindowListener and override proper event windowClosing();
when you close any objects by false visible, actually you just hide that object whereas the object stays in the memory yet.
you better use the
frame.dispatchEvent(new windowEvent(frame,windowEvent.window_closeing)); method

Why doesnt my JButton always pop up on a GUI?

So im making a game, and i want a jbutton to pop up in the window and when you click it you can log in. the problem is that when i start the game, it doesnt always pop up, which is kind of annoying. the only think would be the problem is the order im doing things. here is the code:
public static void createWindow() {
ImagePanel panel = new ImagePanel(
new ImageIcon(backgroundFile).getImage()); //used for the background
JButton login = new JButton(new AbstractAction("Login") {
public void actionPerformed(ActionEvent e) {
Login.createWindow();
}
});
login.setBounds(300, 300, 100, 100);
frame.getContentPane().add(panel); //sets the background to a pic
frame.setJMenuBar(MenuBar.menuBarCreator()); creates the menu bar
frame.pack();
frame.setResizable(false);
frame.setTitle("*Game Title* Beta 0.0.1 ADMINISTRATOR VERSION");
frame.setSize(ImagePanel.img.getWidth(null),
ImagePanel.img.getHeight(null));
frame.setLocation(Monitor.setLocationHeight(),
Monitor.setLocationWidth());
frame.setVisible(true);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
StreamingLineSound.start(soundFile); //starts a music file
frame.add(login);
}
any help would be fantastic. so basically all i want is an idea why it doesnt pop up all the time. thanks
Code line frame.setVisible(true); must be last line in the public static void createWindow() {, because you display JFrame and thenafter add frame.add(login);

Categories