Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9215983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:19:21+00:00 2026-06-18T02:19:21+00:00

I am just now learning the basics of java GUI. I have this weird

  • 0

I am just now learning the basics of java GUI. I have this weird situation that I can’t really explain.

I have a GUI class, where I build a simple JFrame. If I use .setVisible(true) within the constructor everything works fine, if I use it outside, nothing loads (the window is visible, but the buttons and what not aren’t).

Why is this happening ?

public class GUI extends JFrame {


    private JTextField humanYears_TextField = new JTextField(3);
    private JTextField dogYears_TextField = new JTextField(3);
    private JButton convert_Button = new JButton("Convert");
    private JLabel greeting = new JLabel ("Dog years to Human years!");

    public GUI () {

        JFrame window = new JFrame();
        JPanel content = new JPanel();


        content.setLayout(new FlowLayout());
        content.add(this.greeting);
        content.add(new JLabel("Dog Years: "));
        content.add(this.dogYears_TextField);
        content.add(this.convert_Button);
        content.add(new JLabel("Human Years: "));
        content.add(this.humanYears_TextField);

        window.setContentPane(content);
        pack(); // aplica contentPane-ul
        window.setLocationRelativeTo(null);

        window.setTitle("Dog Year Converter");
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setVisible(true); // IF IT'S HERE IT WORKS
    }
}

public static void main(String[] args) {

    GUI dogYears = new GUI();
    //dogYears.setVisible(true); // IF IT'S HERE
                                 //NOTHING EXCEPT THE WINDOW LOADS
}

Why is this happening ?

In this example it doesn’t matter, but what if I want to make a window visible only if I click a button or something ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:19:23+00:00Added an answer on June 18, 2026 at 2:19 am

    1) You create 2 instances of JFrame the first from extending JFrame on the class and the second from JFrame window=... You than go on to call setVisible(..) on the window and in your main you attempt to call setVisible(...) on your dogYears.

    Solution

    You should only create one JFrame per Swing GUI. Get rid of the extends JFrame (and the code that goes with it), as its not good practice to extend JFrame in Swing. Thus of course you wont be able to call setVisible(true) in your constructor which is fine either call it after creating the GUI in the constructor or make a method like setVisible in GUI class which will be a wrapper for your JFrames setVisble(boolean b) method.

    Other suggestions

    • Always create your Swing components on the Event Dispatch Thread which you should do in via SwingUtilitites.invokeLater(Runnable r) block. Have a read on Concurrency in Swing.

    • Remember to call pack before setting JFrame visible and after adding components.

    • setLocationRelativeTo(..) should come after pack().

    • Better to use JFrame.DISPOSE_ON_CLOSE unless using Timers.

    • Also no need for setContentPane simply call add(..) on JFrame instance.

    Here is your code with above mentioned fixes:

    enter image description here

    import java.awt.FlowLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
    
    public class GUI {
    
        private JTextField humanYears_TextField = new JTextField(3);
        private JTextField dogYears_TextField = new JTextField(3);
        private JButton convert_Button = new JButton("Convert");
        private JLabel greeting = new JLabel("Dog years to Human years!");
        private JFrame window = new JFrame();
        private JPanel content = new JPanel();
    
        public GUI() {
            content.setLayout(new FlowLayout());
            content.add(this.greeting);
            content.add(new JLabel("Dog Years: "));
            content.add(this.dogYears_TextField);
            content.add(this.convert_Button);
            content.add(new JLabel("Human Years: "));
            content.add(this.humanYears_TextField);
    
            window.add(content);
    
            window.setTitle("Dog Year Converter");
            window.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    
            window.pack();
            window.setLocationRelativeTo(null);
            //window.setVisible(true); //works here now
        }
    
        //our wrapper method so we can change visibility of our frame from outside the class
        void setVisible(boolean visible) {
            window.setVisible(visible);
        }
    
        public static void main(String[] args) {
            //Create Swing components on EDT
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    GUI dogYears = new GUI();
                    dogYears.setVisible(true);//works here too
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm just learning MVC3 now and this is really confusing me. I have a
I'm learning scheme and until now have been using guile. I'm really just learning
I have just started learning basics about Graphics2D class, So far I am able
I have started learning Objective-C, and I just wanted to verify that my understanding
Im new to asp.net mvc and just learning the basics right now. Im wondering
As a beginner programmer, just now learning the basics of OOP, I've run into
I just started learning the very basics of c++, I have experience with vb.net
I'm just now learning to programming at age 17. It's hard for me to
I am now learning on the audio framework of iphone. Just tried to play
MOV is probably the first instruction everyone learns while learning ASM. Just now I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.