How to add multiple components to a JFrame? - java

I have a JFrame.
I also have a Box class which extends Component.
This box class has a paint method which makes a filled rectangle.
When I add multiple of these Box components to my JFrame, only the most recently added one is displayed when I call repaint on the JFrame.
I took a look at the layout managers, but I am not sure that's what I want. All I want is to be able to make an animation of whole bunch of rectangles wherever I want on the screen.
(I also tried creating a panel, adding the panel to the JFrame, and then adding all the Box components to the panel. This did not work either).
Thanks in advance!

You have 2 choices.
You can change the layout of your frame:
JFrame frame;
frame.setLayout(new FlowLayout());
Now, if you add more than one box, it will show up on the frame.
The other option is to do what you said you tried. (Adding a panel to the frame)
JPanel pane = new JPanel();
frame.add(pane);
(add the boxes to 'pane')
Also, you should be careful with the sizing of your Box. You will probably want a call to setPreferredSize() somewhere in the creation of the Box. This will tell Java what size to make the box when it is added to the layout.
You should also take a look at the Java Layout Manager Tutorials. There is lots of great info there.
And, one more thing. The reason only one box at a time was being displayed on the frame was because JFrame's layout manager is BorderLayout. And, when you call add on a component that has a BorderLayout, the component is automatically added to the center of the component. Subsequent calls to add will overwrite the center component, leaving only one component in the middle.

You do need to check out other layout managers. JFrame by default uses BorderLayout and without specifying the "place" a component is added, they get added to CENTER. Depending on what you want your UI to look like depends on the layout manager to use. I would suggest maybe using Netbeans GUI builder.
EDIT: Missed the part about what you want to add but the concept is still the same, if you just add these components to the default layout manager, they will get overwritten. Sounds like you may need to do your painting inside of just one of your Box components or create a JPanel and set the layout to null but then you would have to place them explicitly. Really depends on what you want to do with it exactly.

Do your layout on paper first, then read up on Swing layout managers.
Be aware that some Swing components only allow one component to be added to them. I've run across this when using Tabbed panes. Each tab can only accept one control (JPane?) so you have to create a separate panel with a layout to arrange the related controls and then as a unit add the pane to the tab. There are similar arrangements in the Swing library.

You could set the frame layout to null and then use setBounds() to position your boxes exactly where you want.

Thank you for all your answers.
Since I am using my own custom class, Box, I have the ability of setting the position of my the rectangle through the paint method.
I realized my Box class was extending the wrong thing. It should have been extending javax.swing.Jcomponent.
If I now use a panel with an OverlayLayout, add my components to that panel, they all show up properly.

Related

Make children of JLayeredPane fill available space of container

I have two JPanel instances in a JLayeredPane, on different z-orders. I want both of my child JPanels to always fill the space of the LayeredPane.
The idea is for me to toggle the display of a 2nd panel over top of the first to display a modal-like dialog. Yes, I could just use a JDialog, but I thought it would be fun to try and create some transparancy overtop of the covered JPanel for a nice effect.
I find that using a layout manager on the JLayeredPane, like BorderLayout, and trying to set both children to CENTER conflicts since both panels can't be in the Center.
Is there a trick that I'm not seeing?
The idea is for me to toggle the display of a 2nd panel over top of the first
The easiest way to do this is to use a Glass Pane.
Check out the Disabled Glass Pane for an example of this approach.
There are two ways to create some "Glass Panel like" overlay for JPanels with JLayeredPane:
Add a ComponentListener to the JLayeredPane and update the sizes of all child components whenever the size of the JLayeredPane changes
Create a simple FillLayout, which expands the size of its child Components to the size of the Layout Container (In our case the JLayeredPane). You need to keep a list of children Components. During layoutContainer you copy the dimensions of the Container to these child Components. I wrote this and its really simple, but unfortunately I can't post it, since it's corporate. But if anyone is interested just ask in the comments. The implementation basically consists of one-liners.
For both solutions you need to make sure, that the panels on top are transparent, by setting setOpaque to false. This ensures that underlying panels render their content.

Can't add component to JPanel in Swing

I want to add at runtime a JLabel under the purple pane which contains already some components, say under the progress bar:
Here is the structure of the elements:
And this is my code which is issued when an event occurs (it's getting there i checked with debug) :
jPanel1.add(new JLabel("Stack Overflow"));
jPanel1.revalidate();
jPanel3.revalidate();
I'm not seeing any changes whatsoever and have no clue where to go from here. When i put a
textarea in the purple pane and then call it's setText() method at the same place i try to add the JLabel component it works.
You need to learn more about layouts and how they work. I strongly suggest you read the entire layout manager tutorial, since understanding layouts are the solution here, and just using BorderLayout isn't the way to solve it. You'll likely want to nest layouts, perhaps using BorderLayout for the overall GUI, and having a central JPanel use BoxLayout to allow you to stack components on top of each other inside of it. Then perhaps add this JPanel to the main JPanel that uses BorderLayout in the BorderLayout.CENTER position.
Just a hunch, but maybe you need to call repaint() in addition to revalidate()
Java Swing revalidate() vs repaint()

Is it possible to add components dynamically to the middle of BoxLayout

I'm building a small swing application where I've created contentpane with BoxLayout aligned to Y axis and added another 2 JPanels to it. So far so good.
Now I've stumbled upon a problem where I have to re-create the first JPanel and add it to the middle of other two JPanels.
There might be more than one occurrence where another panel is added to the layout so I wish it would expand appropriately.
Conclusion: What I'm looking for is to add components to the BoxLayout with option to add them before the last component.
Thanks.
Check out the Container API. The add(...) method has overloaded methods that allow you to specify the position of the component in the container. Then revalidate() and repaint() the panel.
I would simply remove all components and then re-add them in the order desired being sure to call revalidate() and repaint() on the BoxLayout-using container after you've completed this action.
If you need your GUI itself to change size, then you may have to call pack() on the top-level window that holds these components.

How to set JPanel size?

I'm using Netbean's form creator and I'm trying out some things. I'm not sure if it's the layout manager, but when I create my own JPanel and add it to the main content pane of the my window, the size of the panel always maximizes inside the FrameView no matter what re-dimensioning methodology I use such as setSize or setPreferred size. I'm very new to AWT and Swing.
With NetBeans WYSIWYG designer it's a peace of cake - just be sure to use Free Form Design and use mouse for resizing. Maybe the panel is itself larger than FrameView, so double click on it (you are editing it exclusively) and make it smaller. Than double click to get back to parent component and you should be fine.
Or maybe check some tutorials at NetBeans site.
You're not supposed to set sizes manually. Leave that to the layout managers, and your GUI will not break when the window size or the font or even just a button label changes.
The trick with layout managers is that you can use not just one but several, in nested JPanels. That way, nearly any layout is possible.
This happens because the layout manager of JPanel's container (perhaps it is either JFrame or another JPanel) instructs the JPanel to maximize.
Do the following:
Find out, who is the parent of this JPanel (take a look at NetBeans's containment object tree)
Check, what layout is defined for the container
Read the documentation about LayoutManager's (they're in java.awt package)
???
PROFIT!
In my experience, Swing is a very picky thing. I'd try setMaximumSize and setPreferedSize. As a side note though: when ever I used GridLayout, it always stretches whatever is in each of the cells (to make it symmetrical I guess). Flow, Box, Border, and I think GridBag don't have that problem though.
-Brett

Resize a JPanel in line with a JDialog

I've got a JDialog which contains a series of JPanels in a CardLayout. Some of these panels contain JTables which I would like to be resized in line with any resizing of the JDialog. I am not sure how to achieve this and any help would be greatly appreciated.
At present the tables simply remain their current size and do not scale. I'm sure its something simple but I'm having trouble locating the exact approach needed.
Many thanks in advance. I will provide any more information if required.
edit: The JDialog is used as a wizard, so only one of the panels is being displayed at any one time, hence the use of CardLayout. I would ideally like to keep this is layout manager, although if it is the source of the problems then obviously I would rethink!
You can keep the CardLayout, but you need to do the following:
1. set the layout of your JDialog to BorderLayout, and add a new JPanel (contentPanel) to the JDialog
2. now set this contentPanel layout to be CardLayout
3. add your other panels to the cardlayout as required.
4. Also make sure off the layouts you use on each of the panels you're adding to the CardLayout. By default JPanel uses FlowLayout, I think, and this is not ideal for a JTable. So you might need to play around with the layout of the panel containing the table as well; try out BoxLayout or BorderLayout for that as well.

Categories

Resources