Set Invisible or close a frame
Hello!
I want to make a program which
1) will open a frame with a button.
2) When I press the button it will open a new frame with another button.
3)When I press the button of the second frame I want to close the second frame.
PHP Code:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Listener
implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
JFrame newFrame = new JFrame("New Frame");
String str = e.getActionCommand();
JButton button = new JButton("DEL");
button.addActionListener(this);
newFrame.add(button);
newFrame.setSize(200,200);
if(str == "OK")
{
newFrame.setVisible(true);
}
if(str.equals(button))
{
newFrame.setVisible(false);
}
}
public static void main(String[] args)
{
Listener listen = new Listener();
JFrame frame = new JFrame();
JButton button = new JButton("OK");
button.addActionListener(listen);
frame.add(button);
frame.setSize(50,150);
frame.setVisible(true);
}
}
Have done this which
1) will open a frame with a button.
2) When I press the button it will open a new frame with another button.
As you see i have tried the setVisible(false) but it don't work so here is my question. What I could use to close or set invisible the frame ? and why the setVisible(false) don't work?
Thanks in advance for your time wasting,
Kostas
Re: Set Invisible or close a frame
Please explain what happens/
You should use the equals() method to compare the contents of Strings, not the == operator.
Re: Set Invisible or close a frame
First of all, sorry for not specifying my question.
Now my question: How can i set invisible a frame when a press a button.
I hope this will help you to help me.
Thanks in advance,
Kostas
Re: Set Invisible or close a frame
What happens when you call setVisible(false) for a visible window?
Change your posted code to call setVisible(false) for the currently visible window. Don't do anything else, just call that method and see what happens.
Re: Set Invisible or close a frame
if you mean this
PHP Code:
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setVisible(false);
i tried that
PHP Code:
if(str.equals(button))
{
newFrame.setVisible(false);
System.out.println("OK");
}
But there wasn't output in the console(in eclipse)
then i tried with the == operator and i had as output OK
but the == operator and the method equals() aren't the same?
Also when i debugged i found that it use the println but it don't use the setVisible(false) or i think it stuck
I think the problem is in the if statement but i can't look at it anymore I will sleep (Step 0) and i will spent the whole day tomorrow thinking it. If you find why it don't work please let me know.
Thank you in advance
I think the most i have written would be very difficult to understand but i am Greek and i don't know enough English to specify it more
Re: Set Invisible or close a frame
No, that is not I meant. I meant to change the listener to something like this so when the button was pressed, setVisible(false) would execute and the window with the button would disappear.
Code :
public void actionPerformed(ActionEvent e)
{
jf.setVisible(false); // This Hides the window
}
You must pass a reference to the window to the listener's class and save it in a variable: jf.
Re: Set Invisible or close a frame
Thank you I made it work with 2 new Action Listener.
Re: Set Invisible or close a frame
I'm sure there are several ways you could do it. More ActionListeners sounds more complicated than the way I did it.
Re: Set Invisible or close a frame
I used the following code
PHP Code:
JButton b = new JButton();
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0)
{
frame.setVisible(false);
}
});
for to close it and then setVisible(true) to show it. It found it more simple than using a whole class for a simple work of 2 lines
Re: Set Invisible or close a frame
Your last post: with 2 new Action Listener
was confusing.