aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes How to get a panel to display a button and a component? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to get a panel to display a button and a component?" Watch "How to get a panel to display a button and a component?" New topic
Author

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

alan ze
Greenhorn

Joined: Jan 29, 2011
Posts: 7
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

Joined: Oct 24, 2010
Posts: 560
Is the container large enough so that all are visible? Try resizing to see.


Always learning Java, currently using Eclipse on Fedora.
Linux user#: 501795
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
alan ze
Greenhorn

Joined: Jan 29, 2011
Posts: 7
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

Joined: May 03, 2008
Posts: 4542
    
    5

What is that RectangleComponent? Maybe its size is [0, 0]


luck, db
There are no new questions, but there may be new answers.
alan ze
Greenhorn

Joined: Jan 29, 2011
Posts: 7
The code for the component is

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2182
    
    7
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get a panel to display a button and a component?