CardLayout - positioning components - java

Is there any way to tell a JPanel using CardLayout where to add a component?
Let' s say I have one of these panels in the center of a frame, and I want to display 3 components inside that panel. Would this be possible?

Sure, it's easy. Just put a JPanel as one of the cards, then position the components in the panel using whatever layout works best.

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.

What layout should I use for chat history

![two muppets][1]
I want to make a chat history in java swing.
I have a JScrollPane. I append dinamic multiple JPanels in him.
I use BoxLayout but I have a problem:
- When appending the first JPanel it are height 100% of JScrollPane.
- When appending the second JPanel, both are height 50% of total JScrollPane.
I want to make each JPanel have fixed height (40px).
What layout should I use or what should I do?
First off, I'd consider using a JList and not a grid of JPanels.
Your JList cell may easily display what looks like a JPanel view by simply using the right renderer.
And you'd lesson GUI override doing this.
If you must use JPanels, I'd put them in a GridLayout or BoxLayout container (JPanel)
Add this container to a BorderLayout using container (JPanel), to its BorderLayout.PAGE_START position
And then add this final container to the JScrollPane's viewport.

How to create a rectangle with multiple images inside?

What is the best way to display let's say rectangle (3x5) with icons 20x20 px.? I want to change the image file of every pic icon later (= it's not just static pictures). I tried to make JFrame full of JPanels, but i was able to display only one panel at a time. I don't want to use GridLayout, because I need just small rectangle inside a frame. Any ideas how to do it? Couldn't find any tutorial or solution. I'm completely new to GUI developement. Thanks
You do want to use a GridLayout. Your problem is that the JFrame you put the icons into uses a BorderLayout by default (and really, you shouldn't change the layout of a top level component).
What this means is that, if you add multiple panels to the frame, without using one of the NORTH, EAST, SOUTH, WEST constraints, only one of the panels will be visible and take up all the space. If you use a GridLayout for that one panel you get, the icons will be stretched, because the panel receives all the space due to the frame's BorderLayout. An alternate layout that doesn't stretch its contents is FlowLayout, but the layout to use depends heavily on your context.
To display the icons, a JLabel is handy. Use an ImageIcon for the label's icon. You can later use setIcon() on the label to choose a new icon.
overall, my approach would be this:
use a JFrame which has a BorderLayout
to the frame, add a JPanel to the frame. The default layout is a FlowLayout, which will prevent the stretching
to the panel, add a JPanel with an appropriate GridLayout
to that panel, add the JLabels, each having an appropriate ImageIcon

How to make a JPanel dynamic?

I have a JFrame that has a JPanel inside. I call "setPreferredSize(new Dimension(500, 600));" but I want the JPanel and its contents to resize when someone resizes the JFrame.
BorderLayout is the way to go. Components start at their preferred size, but are expanded as needed to fill the region they are in.
Set your layout on your frame with BorderLayout
Add your JPanel by
frame.add(yourPanel, BorderLayout.CENTER);
This will allow it to stretch vertically & horizontally
As for the Contents inside a JPanel, give it a layout that will accommodate stretching as well.
Use a layout manager instead of setting the bounds for each component.
It is going to vary from program to program how you want your components to move.
Take a look at this and try to see which layout will work best for you.

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