• Post Reply Bookmark Topic Watch Topic
  • New Topic

JPanel and Layouts

 
Barry Andrews
Ranch Hand
Posts: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I set a JPanel layout to null and use setBounds to position components? I have tried doing this but it does not work. It compiles, but does not show anything. Am I missing something here?
JPanel panel = new JPanel();
JButton button = new JButton("Button");
panel.setLayout(null);
panel.add(button);
button.setBounds(20,30,20,30);
 
eric moon
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you need to set bounds before you add.... try that.
 
Barry Andrews
Ranch Hand
Posts: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, actually I did try that and it did not work either. I ended up using GridBagLayout which is kind of a pain to use but it got the job done. I am still curious about setting a JPanel to null though. Thanks!!!

Barry
 
sailesh gadia
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had tried a similar thing sometime back and it worked.
Have you done ?
panel.setVisible(true)
If you have done this, I can't think of any other reason
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!