How to set JPanel size? - java

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

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.

Auto scaling components with Java JFrame [duplicate]

Not sure if what I need is possible.
I have a container (JPanel) that contains some internal elements.
I was wondering if it is possible to force internal elements to fit into the container's size.
I need them to be fully visible i.e., resize to fit inside the Panel's size and not cut some parts of the internal elements.
Scrolling is not an option.
Is this possible by using a Layout or something?
EDIT: Important clarification:
The thing is that I do not have access to the internal elements neither to their properties so I would say that a Layoutmanager capable of resizing child elements to fit to its size is needed. I tested BorderLayout and GridBagLayout but the result is always the same, the internal elements are cut out.
It's for exactly that reason that LayoutManagers exist. All the LayoutManagers work for simple containers directly, excluding GridBagLayout which is to able to handle most complete GUIs directly.
For most complete GUI's you have some choices as follows:
Look for a 3rd party layout such as MigLayout or here
Use GridBagLayout
Very easy way is use nested layout, where there is more than one JPanel and each has child JPanels with the same or different LayoutManager
Or custom layout, should be hard..., but same as using GridBagLayout
You could set the JPanel layout to border layout, then add the single child to the center. If there are multiple children, this approach becomes less useful since components added to the the NORTH, SOUTH, EAST, and WEST will remain statically sized while the centre resizes to fill the remainder.
In short, this isn't an ideal solution. All layouting in Swing is made all the more complex by the fact that different components behave in different ways, so you really need to provide further details of the child components you wish to add to your panel, and any behaviour that has been overridden on those components.
The best way is to try a couple of simple examples to see what mileage you get and whether subtle redesign of your child component nesting could help.
you can use a layout, like GridBagLayout, or BorderLayout depending on the situation. With proper weights it is possible.
this sounds to me like you should just peek an appropriate layout manager and use it. For example, look at BorderLayout - put your component in the CENTER and it will occupy all the area. Its up to each concrete layout manager to decide what will be the size of the components.
Mark
I was using a JInternalFrame inside JDesktopPane. I wanted the internal_frame to auto resize as desktop pane is resized, so I had to implement the AncestorResized event for the internal frame where I placed the following code:
this.setPreferredSize(this.getParent().getPreferredSize());
this.pack();

Keeping a component in a fixed position within a BoxLayout

I have a JPanel that uses a horizontal Box layout and contains a JLabel that I would like to keep in the exact same position as other components within the JPanel are setVisible(false). Currently, the JLabel moves to the left as other components become invisible.
What's the easiest way to go about this?
EDIT: Pics added
So this is what the JPanel look like with all components visible
When I set the three JTextFields on the right to invisible, the JLabel set to text X moves to the left like this:
But I would like it to stay where it was like this:
EDIT2: I'm actually using Netbeans GUI editor's Free Design for this particular JLabel. I'm sorry for the mistake - I've been using a lot of BoxLayouts recently and I got confused!
Currently, the JLabel moves to the left as other components become invisible.
Yes, layout managers are designed to only work with visible components. I'm not sure if any of the default layout manager will work, but I would look into using the GridBagLayout, since this layout is based on a grid structure so as long as you have components in that grid on another row the label should not shift.
Otherwise, you could dislay the "other components" in a panel using a CardLayout. Then instead of making the components invisible, you swap the panel with an empty panel.
Read the section from the Swing tutorial on How to Use CardLayout for more information and working examples.
Edit:
Based on your picture the easiest solution is to use "glue":
panel.add(Box.createHorizontalGlue);
panel.add(xLabel);
Now the label will always be displayed at the far right of the panel. Read the tutorial on How to Use BoxLayout for more information about "glue".

A Flexible Layout manager that will change the layout according to resize

In my JPanel, I have 6 buttons laid out in a row (using FlowLayout as of now). The default size of the panel is enough to accommodate these buttons in one row. But, when the frame is resized it gets stuck at the size that is the sum of the minimum sizes of each button.
I need a layout manager that simply puts the buttons in a new row on re-sizing of the panel.
I'm pretty new to Java Swing so I apologize in advance if this is a trivial question.
MigLayout is by far the best layout manager I've ever used. Things that used to require nested containers and lots of hard to understand code can be done in a single container with simple to understand (and maintain) string constraints.
The flow layout is capable of your desired behavior (moving components into new row if they cannot fit). Check out the swing tutorial (run FlowLayoutDemo). You'll have to show us your source code to find out, whether there is some other constrain which prevents it.
FlowLayout does actually paint components on a new row, but the problem is that the preferred size of the panel doesn't change so in many cases you can't see the components (unless you happen to add the panel to the CENTER of a BorderLayout).
One solution is to use the Wrap Layout, which extends FlowLayout to recalculate the preferred size of the panel so that you see the buttons on a new row.

How to add multiple components to a JFrame?

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.

Categories

Resources