Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Set Invisible or close a frame

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Location
    Greece
    Posts
    14
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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
    Last edited by Souperk; November 29th, 2011 at 03:06 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Set Invisible or close a frame

    but it don't work
    Please explain what happens/

     if(str == "OK")
    You should use the equals() method to compare the contents of Strings, not the == operator.

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Location
    Greece
    Posts
    14
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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.

  5. #5
    Junior Member
    Join Date
    Nov 2011
    Location
    Greece
    Posts
    14
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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.
        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.

  7. The Following User Says Thank You to Norm For This Useful Post:

    Souperk (November 30th, 2011)

  8. #7
    Junior Member
    Join Date
    Nov 2011
    Location
    Greece
    Posts
    14
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Set Invisible or close a frame

    Thank you I made it work with 2 new Action Listener.

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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.

  10. #9
    Junior Member
    Join Date
    Nov 2011
    Location
    Greece
    Posts
    14
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Set Invisible or close a frame

    Your last post: with 2 new Action Listener
    was confusing.

Similar Threads

  1. JFrame invisible border issue
    By gargamel7 in forum AWT / Java Swing
    Replies: 2
    Last Post: September 25th, 2011, 04:32 PM
  2. Opening a new frame on close.
    By Zein in forum Java Theory & Questions
    Replies: 1
    Last Post: August 24th, 2011, 03:25 PM
  3. Is this correct.Am I close? Help please
    By eagle09 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 26th, 2011, 07:26 AM
  4. Swing Components Invisible as Startup
    By SpiceProgrammer in forum AWT / Java Swing
    Replies: 5
    Last Post: January 23rd, 2011, 02:26 PM
  5. invisible box game
    By new2java in forum Loops & Control Statements
    Replies: 1
    Last Post: September 27th, 2009, 12:46 PM