How to add multiple components to a JFrame?
Asked Answered
L

5

11

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!

Luther answered 15/11, 2010 at 16:58 Comment(2)
Code would be helpful. Sounds like you are overwriting a reference or smth...Dutiable
Will do in the future. Thanks.Luther
T
15

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.

Terpineol answered 15/11, 2010 at 17:2 Comment(8)
Is there any layout which lets me simply add as many components as I want, without having to explicitly select where (eg. LEFT, RIGHT, or CENTER)?Luther
The other option I have is to add 1 component, and have the paint method in that component draw all the rectangles I want. (rather than having each rectangle represented with its own component)Luther
@zucch, the FlowLayout will let you add as many components as you want.Terpineol
@zucch, yes, there is the option of overriding the paintComponent() method and draw the rectangles yourself.Terpineol
@zucch, how do you want the rectangles laid out on the pane?Terpineol
All I am trying to get is to get many (let's say 25) rectangles moving around in the frame to represent (gas particles floating around in a container). I will have a loop and every iteration I will update the position of all the rectangles, and then repaint the frame.Luther
I don't really care about a layout (and don't want to deal with it) I would like to draw a whole bunch of rectangles based on an x,y coordinate.Luther
@zucch. If that's the case, then you will want to either use one panel, and custom draw them yourself, or layout the components without a layout manager - #2821372Terpineol
G
0

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.

Genaro answered 15/11, 2010 at 17:2 Comment(1)
He's trying to draw components from runtime, I can't see how the Netbeans GUI builder could be of any help there.Dutiable
E
0

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.

Exoenzyme answered 15/11, 2010 at 17:11 Comment(0)
P
0

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

Personalty answered 15/11, 2010 at 17:20 Comment(0)
L
0

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.

Luther answered 15/11, 2010 at 18:1 Comment(1)
Watch for conflicts with javax.swing.Box.Helicline

© 2022 - 2024 — McMap. All rights reserved.