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 15 of 15

Thread: Frame not becoming invisible

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Frame not becoming invisible

    Here is a code I made to practice how to make JFrame and add components to it.
    But the problem is:
    1. On line 8, shouldn't the frame turn invisible?
    2. On line 7, shouldn't the frame be in the center of the screen?

    Could anyone help?
    P.S : I did it in Ready to Program (if thats the source of any problem)

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class JButtonDemo2 {
            public static void main(String[] args) {
                    mkay app = new mkay();
                    app.setLocationRelativeTo(null);
                    app.setVisible(false);
            }
    }
    class mkay extends javax.swing.JFrame{        
            public mkay() {
                init();
            }
            JFrame jtfMainFrame;
            JButton jbnButton1, jbnButton2;
            JTextField jtfInput;
            JPanel jplPanel;
            public void init() {
                    jtfMainFrame = new JFrame("Which Button Demo");
                    jtfMainFrame.setSize(50, 50);
                    jbnButton1 = new JButton("Button 1");
                    jbnButton2 = new JButton("Button 2");
                    jtfInput = new JTextField(20);
                    jplPanel = new JPanel();
                    jbnButton1.addActionListener(new ActionListener() {
     
                            public void actionPerformed(ActionEvent e) {
                                    jtfInput.setText("Button 1!");
                                    mkay app2 = new mkay();
                                    app2.setLocationRelativeTo(null);
                            }
                    });
                    jbnButton2.addActionListener(new ActionListener() {
     
                            public void actionPerformed(ActionEvent e) {
                                    jtfInput.setText("Button 2!");
                            }
                    });
                    jplPanel.setLayout(new FlowLayout());
                    jplPanel.add(jtfInput);
                    jplPanel.add(jbnButton1);
                    jplPanel.add(jbnButton2);
                    jtfMainFrame.getContentPane().add(jplPanel, BorderLayout.CENTER);
                    jtfMainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    jtfMainFrame.pack();
                    jtfMainFrame.setVisible(true);
            }
    }
    Last edited by sbjibo; June 27th, 2012 at 07:33 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,193
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Frame not becoming invisible

    What happens when you execute the program?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Frame not becoming invisible

    When you execute the program, a frame with a text field, and 2 buttons labeled button1 and button 2.
    But the program is not supposed to show anything

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,193
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Frame not becoming invisible

    Try changing the setVisible parameter to true and see what happens.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2012
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Frame not becoming invisible

    When you write app.setVisible(true), it creates a new window, but it has no components. its just an empty frame.

  6. #6
    Junior Member
    Join Date
    Apr 2012
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Frame not becoming invisible

    Does the fact that I made it in 'Ready To Program' matter ? because my friend made a code simililar to this in netbeans, and it worked.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,193
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Frame not becoming invisible

    The IDE should not make any difference. Your friend's code must have been different.

    Did you see two windows after the change I suggested?
    Look at the code. There are two JFrame objects being created.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Apr 2012
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Frame not becoming invisible

    I dont understand why two windows are created. When I say 'mkay app = new mkay(), that should make a window. So where is the other window coming from?

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,193
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Frame not becoming invisible

    What title is shown in the window with the components? Do a Find in the source for that String to see where it is used. Or look for a statement that has: new JFrame(
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Apr 2012
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Frame not becoming invisible

    It is created at line 20 of my program. The title of the window is "Which button Demo"

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,193
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Frame not becoming invisible

    Do you now see where there are two windows being created?
    If you don't understand my answer, don't ignore it, ask a question.

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

    sbjibo (June 27th, 2012)

  13. #12
    Junior Member
    Join Date
    Apr 2012
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Frame not becoming invisible

    I think thats the only line where the window is created. Could you tell me where is the other window being created?

  14. #13
    Junior Member
    Join Date
    Apr 2012
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Frame not becoming invisible

    Wait, the mkay class extends the JFrame, so that s the other wiindow, right?

    The code works if you dont create another frame inside the mkay class.



    --Thanks for helping me

  15. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,193
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Frame not becoming invisible

    Right.
    One was mkay that extends JFrame

    The other was:
    JFrame jtfMainFrame;
    ...
    jtfMainFrame = new JFrame("Which Button Demo");
    If you don't understand my answer, don't ignore it, ask a question.

  16. #15
    Junior Member
    Join Date
    Jun 2012
    Posts
    20
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default Re: Frame not becoming invisible

    See,2 frames are being created because u have created 2 objects-app and jftMainFrame

Similar Threads

  1. Replies: 1
    Last Post: January 19th, 2012, 02:44 PM
  2. [SOLVED] Set Invisible or close a frame
    By Souperk in forum Java Theory & Questions
    Replies: 9
    Last Post: November 30th, 2011, 10:56 AM
  3. JFrame invisible border issue
    By gargamel7 in forum AWT / Java Swing
    Replies: 2
    Last Post: September 25th, 2011, 04:32 PM
  4. Swing Components Invisible as Startup
    By SpiceProgrammer in forum AWT / Java Swing
    Replies: 5
    Last Post: January 23rd, 2011, 01:26 PM
  5. invisible box game
    By new2java in forum Loops & Control Statements
    Replies: 1
    Last Post: September 27th, 2009, 12:46 PM

Tags for this Thread