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?
setVisible(true)
on the JFrame until after you've added all components and calledpack()
on it.e.g.,
But better off using layouts:
Edit
The OP asked in a comment:
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