• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get a panel to display a button and a component?

 
alan ze
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I display the buttons and the component at the same time?

The component doesn't display when I try to run the code:



 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the container large enough so that all are visible? Try resizing to see.
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you add components to (or remove from) a JComponent after it's already visible, you need to revalidate() and repaint() the JComponent. Another easy fix is to not display the user interface until it's completely finished - move the call to frame.setVisible(true) to the end.
 
alan ze
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tried resizing and putting frame.setVisible(true) at the end but the component still won't show. It only shows when I remove the buttons and panel and display it on the frame. Any other ideas?

 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is that RectangleComponent? Maybe its size is [0, 0]
 
alan ze
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code for the component is

 
Rob Camick
Ranch Hand
Posts: 2613
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It only shows when I remove the buttons and panel and display it on the frame


When you add the Rectangle directly to the frame it get added to the content pane which uses a BorderLayout by default. The component get added to the center which means it automatically get resized to fill the entire area of the frame.

By default a JPanel uses a FlowLayout, which displays components at their preferred size.

As Darryl suggested you did not override the getPreferredSize() method of your component so its size is 0 and it doesn't get painted. You need to implement the getPreferredSize() method to return an appropriate value.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic