Java switch between panels (using custom buttons) on JFrame - java

I want to make a some kind of modern GUI that has tabs on left, like shown on this picture:
Any ideas how could I make panels switch between while not disposing data that's written/set, for example into text fields, text area, check boxs, scroll bars and etc.?
I was thinking about just removing old one panel and adding another panel, but when I click on panel I visited before controls data will have been reset, and I think it would flicker.

Thanks to Frakcool:
Use a CardLayout for switching panels

I don't know if this is helpful, but you could try this;
Make a JTabbedPane, add it to the JFrame's contentPane.
add JPanels to the tabs.
then make sure that the tabs are aligned left and vertical by doing the following:
tabbedPane.setTabPlacement(JTabbedPane.LEFT);
and then, you can even do this:
JTabbedPane .setTabComponentAt(1, new WebVerticalLabel("Title1"));
to make the label text displayed vertical.
I hope this helps you,
Lenard

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.

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".

java border gui

I am not good with GUIs or User Interfaces in Java.
Would a Border or JPanel be good for something like the image below?
So, what would be the best option for me? Thank you.
Read the section from the Swing tutorial on Using Layout Managers. You can easily nest panels to get the desired effect.
Maybe start with a BorderLayout. Then you can add a panel that uses a GridLayout, which contains all your image tiles, to the CENTER of the BorderLayout. Then you can add the scrollpane containing the text area to the SOUTH. Then you can create another panel to add to the EAST.
Be creative and experiment.
You can make 4 seperate panels for a border, using BorderLayout.NORTH,BorderLayout.EAST,BorderLayout.SOUTH,and BorderLayout.WEST, This is the easiest way in my opinion.
By the way, in the top right of your picture, where you wanted the information panel, you should put an information LABEL (JLabel) instead, because they hold text. JLabel topRight = new JLabel(); then set the text, position, etc.
p.s. to erase the borders around every tile (if you want to do so), use setBorderPainted(false).

How do I layout a panel on the top of my Dialog so that it has two buttons?

I need to layout a panel on the top of my Dialog so that it has two buttons (Save and Cancel).
I want the save to be on the left and Cancel to be on the right side.
I've created a JPanel using the MigLayout and docked it to the north of the content pane, and can't for the life of me figure out how to add the two buttons to it so that they appear as I want them. Docking them within the panel seems to get rid of all padding in the dialog (which looks terrible).
Any help would be greatly appreciated.
As an aside, you should probably not be dictating which button is on the left or right. That's one of the way cool things about MiGLayout (platform independence, even on things such as where the cancel button should go).
p.add(cancelButton, "tag cancel");
p.add(okButton, "tag ok");
Now the buttons will appear in the correct order, based on platform.
Here's an article with code doing what you are going for. I strongly recommend avoiding trying to force the size of components like buttons (these really should come from the platform look and feel). Also, docking is fine if it makes sense to do so, but I rarely find it to be necessary. Instead of building a totally separate panel for your buttons, just span the row that contains the buttons - much cleaner, and you don't wind up with all of the nested panels.
It's hard to break from the border layout technique of nested panels, but once you get the hang of it, MigLayout is a dream. BTW - I understand that there are times where you might want to build up the button panel in a library - if that's the case, then separate panels may make sense (although you could also have the library add a button row to an existing panel, instead of returning a panel that you then add to the layout).
Doh, always happens as soon as you ask a question, the answer pops out:
JPanel buttonPanel = new JPanel(new MigLayout("fill","[50][50]",""));
buttonPanel.add(saveChangesButton);
buttonPanel.add(cancelButton, "align right");
getContentPane().add(buttonPanel, "dock north");
Note that the content pane is using the MigLayout too.

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