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 8959663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:29:30+00:00 2026-06-15T15:29:30+00:00

Someone can tell why the combobox is not showing ? I have a Controller:

  • 0

Someone can tell why the combobox is not showing ? I have a Controller:

public class TestController extends JPanel {

TestView cgView;

public TestController() 
{

    setLayout(null);

    cgView=new TestView();

    add(cgView);

}
public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
             JFrame fr = new JFrame("testt");
                fr.setSize(1200,1000);
                fr.setResizable(false);

                TestController cgc=new TestController();
                fr.setBackground(Color.white);
                fr.setVisible(true);

                fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                fr.add(cgc);

         }
        });
    }


}

And a view

public class TestView extends JPanel{
    private static final long serialVersionUID = 1L;

    public JComboBox<String> comboBox; 

    public TestView() {

          comboBox= new JComboBox<>(new String[] {"option1", "option2" });
          comboBox.setBounds(100,500, 100, 20);
          add(comboBox);

    }
}

Because of setLayout(null) in TestController, I can’t see the comboBox. If I add add(cgView.comboBox) to my TestContoller(), so that it looks like this:

public TestController() 
    {

        setLayout(null);

        cgView=new TestView();

        add(cgView);
        add(cgView.comboBox);

    }

Than I can see it. Can someone tell why?

So my solution is to always add the components in TestController, or to pass TestController as an atribute to TestView (so in TestView() I would add them like this this.parentPanel.add(comboBox). Is there any other solution?

  • 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-15T15:29:31+00:00Added an answer on June 15, 2026 at 3:29 pm
    • Don’t use null layout, almost ever
    • Instead use the best combination of layouts nested in JPanels to achieve a pleasing layout for your GUI.
    • If you do use null layout then you are fully responsible for setting the size and location of all components added to that container.
    • Your current problem is that you never give TestView a size or location and have then added it to a null layout-using container.
    • You shouldn’t add a component (above, your JComboBox) to more than one container.
    • Don’t call setVisible(true) on the JFrame until after you’ve added all components and called pack() on it.

    e.g.,

    import java.awt.*;
    import javax.swing.*;
    
    public class TestController extends JPanel {
       private static final int PREF_W = 1000;
       private static final int PREF_H = 800;
       TestView cgView;
    
       public TestController() {
          setLayout(null);
          cgView = new TestView();
          cgView.setSize(getPreferredSize());
          add(cgView);
       }
    
       @Override
       public Dimension getPreferredSize() {
          return new Dimension(PREF_W, PREF_H);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                JFrame fr = new JFrame("testt");
                // fr.setSize(1200, 1000);
                fr.setResizable(false);
                TestController cgc = new TestController();
                fr.setBackground(Color.white);
                // fr.setVisible(true);
                fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                fr.add(cgc);
                fr.pack(); //!! added 
                fr.setVisible(true); // !! moved
             }
          });
       }
    }
    

    But better off using layouts:

    import java.awt.*;
    import javax.swing.*;
    
    public class TestController extends JPanel {
       private static final int PREF_W = 1000;
       private static final int PREF_H = 800;
       TestView cgView;
    
       public TestController() {
          //!!  setLayout(null);
          cgView = new TestView();
          //!! cgView.setSize(getPreferredSize());
          add(cgView);
       }
    
       @Override
       public Dimension getPreferredSize() {
          return new Dimension(PREF_W, PREF_H);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                JFrame fr = new JFrame("testt");
                // fr.setSize(1200, 1000);
                fr.setResizable(false);
                TestController cgc = new TestController();
                fr.setBackground(Color.white);
                // fr.setVisible(true);
                fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                fr.add(cgc);
                fr.pack(); //!! added 
                fr.setVisible(true); // !! moved
             }
          });
       }
    }
    
    class TestView extends JPanel {
       private static final long serialVersionUID = 1L;
       public JComboBox<String> comboBox;
    
       public TestView() {
          comboBox = new JComboBox<String>(new String[] { "option1", "option2" });
          // comboBox.setBounds(100, 500, 100, 20);
          add(comboBox);
       }
    }
    

    Edit
    The OP asked in a comment:

    ‘Almost never’? In which cases you would use it [the null layout]?

    I use it rarely, such as when I want to move components around via animation or with a MouseListener, but even then, many suggest that you create your own layout to handle that such as Rob Camick’s Drag Layout

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone tell me what I am doing wrong please? I have.. JComboBox comboBox
I have been playing with Expect/TCL today and I was hoping someone can tell
Can someone tell me why I'm getting successCallback is not a function in my
I have sinned on so many levels. I'm hoping someone can tell me a
If someone can tell me what I'm doing wrong it'd be great. If not,
Can someone tell me how to modify this regex to allow periods in a
can someone tell me what seems to be the issue with this <?php $increment
can someone tell me the order in which a thread starts to execute?. I
Can someone tell me how can I select the Row under the one on
Can someone tell me why this processes all the files and then does it

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.