I have a JFrame with some component on it. I want that the frame disappears when i click on a special button for example exit button.
I wrote this code in exit button
this.setvisible(false);
but it only hides the component on it and frame doesn't disappear.
What can I do that when I click on exit button the frame disappears?
Here's an example of a button that hides the frame:
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
final JButton hideButton = new JButton("hide frame");
frame.add(hideButton);
hideButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.setVisible(false);
}
});
frame.setVisible(true);
frame.pack();
In your call this.setVisible(false), this probably refers to the button and not the frame.
You need to call setVisible() on the Frame not the Button.
Also make sure you are calling dispose() on the frame to clean up all resources.
Additionally you should also use
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
during creation of the frame, to make sure the windows is properly closed and disposed when the user clicks the "standard" close button in the upper right corner (on Windows).
This tutorial might also help you understand what's going on better:
http://download.oracle.com/javase/tutorial/uiswing/components/frame.html
call it on JFrame object.
example:
// when exit is pressed
fr.setVisible(false); // fr is a reference to object of type JFrame `
Related
Good afternoon!
I have this code:
private static class ClickListener implements ActionListener {
public ClickListener() {
}
#Override
public void actionPerformed(ActionEvent e) {
JFrame frame = new JFrame();
JLabel label = new JLabel("Opção Indisponivel");
JPanel panel = new JPanel();
frame.add(label, BorderLayout.CENTER);
frame.setSize(300, 400);
JButton button = new JButton("Voltar");
button.addActionListener(new CloseWindowListener());
panel.add(button);
frame.add(panel, BorderLayout.SOUTH);
frame.setVisible(true);
}
}
private static class CloseWindowListener implements ActionListener {
public CloseWindowListener() {
}
#Override
public void actionPerformed(ActionEvent e) {
setVisible(false);
}
}
What I want to do is when i click on the button "voltar" (which is in another window, not on the "main" one as you can see) it closes the windows but not the app itselft. The setVisible line gives me an error about that it cannot be referenced by a static context which I understand because I need the reference of the frame. How do I solve this?
EDIT: Changed JFrame to JDialog but still no sucess. Both windows are shutdown.
Thanks in advance,
Diogo Santos
The setVisible line gives me an error about that it cannot be referenced by a static context which I understand because I need the reference of the frame. How do I solve this?
You can access the component that generated the event. Then you can find the window the component belongs to. This will give you generic code to hide any window:
//setVisible(false);
JButton button = (JButton)e.getSource();
Window window = SwingUtilities.windowForComponent(button);
window.setVisible(false);
You can also check out Closing an Application. The ExitAction can be added to your button. Now when you click the button it will be like clicking the "x" (close) button of the window. That is whatever default close operation your specify for the window will be invoked.
I am trying to create a GUI with a main window for input, and a separate box to show output below the main window. However, the width of the output box will be different from the main window and I don't want the difference in width to be part of the frame (meaning that the difference should be invisible and user can click through the empty space to the back of the application).
But I also want to output box to be shown as 1 window with the main window. I can set the position of the output box with a listener to make it stick to the main window, but I don't want it to be shown as a separate window in the Windows(Microsoft) task bar. I also don't want the output box to be selectable
as a result of being a separate window.
Therefore is there any method to create a JPanel outside JFrame, or is there a way I can do this with JFrame and make it work with my constraints? Any other methods?
Show the separate JPanel in a window as a non-modal JDialog. You can make it undecorated if you don't want the menu buttons on the top right.
I'm not sure what you mean by:
(meaning that the difference should be invisible and user can click through the empty space to the back of the application).
Regarding
I also don't want the output box to be selectable as a result of being a separate window.
Make sure that you have no focusable components in the dialog, or if focusable, have the focusable property set to false.
Edit
To prevent the dialog window from being focused, add a line:
dialog.setFocusableWindowState(false);
For example:
import java.awt.Dimension;
import javax.swing.*;
public class DialogNoFocus {
public DialogNoFocus() {
// TODO Auto-generated constructor stub
}
private static void createAndShowGui() {
DialogNoFocus mainPanel = new DialogNoFocus();
JFrame frame = new JFrame("JFrame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(Box.createRigidArea(new Dimension(400, 300)));
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
JDialog dialog = new JDialog(frame, "JDialog", false);
dialog.getContentPane().add(Box.createRigidArea(new Dimension(500, 100)));
int x = frame.getLocation().x;
int y = frame.getLocation().y + frame.getHeight();
dialog.setLocation(x, y);
dialog.pack();
dialog.setFocusableWindowState(false);
dialog.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
JDialog
How about to put 2 internal Frames(one is decorated, another one - undecorated und not selectable) or 1 internal Frame and panel in one big translucent undecorated Frame?
At first you will get only one icon in the Windows(Microsoft) task bar. Internal frames wont show up in the task bar.
The main frame should be translucent : how to set JFrame background transparent but JPanel or JLabel Background opaque?
I'm trying to add components to a JDialog after it has been created and displayed. Nothing I try makes the changes actually update to the screen, and I've read and applied every question I could find related to this.
This example code creates a modal JDialog showing the word "test". I cannot get it to display "test2". Almost exactly the same code but with a JFrame instead of a JDialog behaves as I expect, so I don't understand. I'm new to Java and especially to swing.
import javax.swing.*;
public class DialogTester {
public static void main(String[] args) {
new DialogTester();
}
public DialogTester() {
JFrame jframe = new JFrame();
jframe.setVisible(true);
JDialog jdialog = new JDialog(jframe,true);
JPanel jpanel = new JPanel();
jpanel.add(new JLabel("test"));
jdialog.add(jpanel);
jdialog.setVisible(true);
jpanel.add(new JLabel("test2"));
jpanel.revalidate();
jdialog.getContentPane().validate();
jdialog.pack();
}
}
I also tried calling
jdialog.repaint();
which did nothing.
You created a modal dialog. So, as soon as you call setVisible(true), the following instructions wait for the dialog to be closed to be executed.
Put the code adding a label before the dialog is made visible, or put it in an event handler called after the dialog is shown, for example, when you click on a button in this dialog.
I'm using a KeyListener on a JFrame object which I set as FullScreenWindow, something like this code:
class Game{
private GraphicsDevice device;
...
public void run(){
JFrame frame = new JFrame();
frame.addKeyListener(new MarioKeyListener());
device.setFullScreenWindow(frame);
}
...
}
And it works fine if I just create a Game object in my main method and call run().
However I want to do this inside the mousePressed() function of a MouseAdapter which I added to another JFrame-s MenuItem. The result is that the program runs as normal but doesn't accept any keyboard input.
JMenu gamemenu = new JMenu("Game");
JMenuItem newGame = new JMenuItem("New Game");
newGame.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e){
Game g = new Game();
g.run();
}
});
gamemenu.add(newGame);
I think My frame object is not in focus, but calling setFocusable(true) and requestfocusinwindow() did not help.
If anyone knows whats wrong or how to fix this, help would be greatly appreciated...
Tomi
requestFocusInWindow()..
Requests that this Component get the input focus, if this Component's top-level ancestor is already the focused Window.
Are you checking the return value? I suspect it is failing because the new window is not the focused component at the moment the method is called.
If that is the case, the answer might be found in similar fashion to the dialog focus strategy of adding a RequestFocusListener to the mix.
My program starts with a picture with a textfield in a JFrame. I want when the user types start it closes the picture JFrame and opens another JFrame with the main program. I've tried
processEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
on the Image frame but it closes all the windows.
The method JFrame.setVisible can be used to hide or display the JFrame based on the arguments, while JFrame.dispose will actually "destroy" the frame, by closing it and freeing up resources that it used. Here, you would call setVisible(false) on the picture frame if you intend to reopen it, or call dispose() on the picture frame if you will not be opening it again, so your program can free some memory. Then you would call setVisible(true) on the main frame to make it visible.
you also can use this code
for example
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
Maybe if you set the picture JFrame's default close operation to something besides EXIT_ON_CLOSE, perhaps DISPOSE_ON_CLOSE, you can prevent your application from closing before the second JFrame appears.
This post is a bit old but nevertheless.
If you initialize the form like that:
JFrame firstForm = new JFrame();
firstForm.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
firstForm.setSize(800, 600);
firstForm.setLocationRelativeTo(null);
firstForm.setVisible(true);
And for instance create or open another form by a button:
JFrame secondForm = new JFrame();
secondForm.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
secondForm.setSize(800, 600);
secondForm.setLocationRelativeTo(null);
secondForm.setVisible(true);
this.dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
This will dispose and destroy the first window without exiting the program.
The key is to set setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE).
It also raises the events (I've tested it with the WindowClosing event).
Here is my solution to this problem:
public void actionPerformed(ActionEvent e) {
String userName = textField.getText();
String password = textField_1.getText();
if(userName.equals("mgm") && password.equals("12345")) {
secondFrame nF = new secondFrame();
nF.setVisible(false);
dispose();
}
else
{
JOptionPane.showMessageDialog(null, " Wrong password ");
}
}
you also can use this :
opens_frame frameOld= new opens_frame();
frameOld.setVisible(true);
Closing_Frame.setVisible(false);
Closing_Frame.dispose();
private void closeTheCurrentFrameAndOpenNew(java.awt.event.ActionEvent evt){
dispose();//To close the current window
YourClassName closeCurrentWindow = new YourClassName();
closeCurrentWindow.setVisible(true);//Open the new window
}
I was searching for the same thing and found that using "this" is the best and easiest option.
you can Use the following code:
this.dispose();
For netbeans use the reference of the current Object and setVisible(false);
for example
private void submitActionPerformed(java.awt.event.ActionEvent evt)
{
// TODO add your handling code here:
this.setVisible(false);//Closing the Current frame
new login().setVisible(true);// Opening a new frame
}
First call it
new Window().nextjframe.setVisible(true);
thisjframe.setVisible(false);
if(username.equals("gaffar")&&password.equals("12345"))
{
label.setText("Be ready to continue");
//Start of 2nd jframe
NewJFrame1 n=new NewJFrame1();
n.setVisible(true);
//Stop code for ist jframe
NewJFrame m=new NewJFrame();
m.setVisible(false);
dispose();
}
This is what i came up for opening a new jframe while closing the other one:
JFrame CreateAccountGUI = new JFrame();
CreateAccountGUI.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
CreateAccountGUI.setSize(800, 600);
CreateAccountGUI.setLocationRelativeTo(null);
CreateAccountGUI.setVisible(true);
this.setVisible(false);