I'm having trouble using CardLayout with a JPanel inside of a JScrollPane. Basically I have a main JFrame and a JPanel inside of a JScrollpane, and then a whole bunch of different JPanels that are acting as 'cards' and pop up on different button clicks. The issue is, that some panels aren't scrollable all the way down to the bottom in the JScrollPane, they only scroll to the point of the initial panel.
Can anybody with some experience with NetBeans tell me how to make sure that each panel is fully scrollable in the scroll pane? If you only know the java code for setting this option I'd appreciate that too.
Thanks.
Related
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
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.
I am working in the NetBeans. For that when I am working in the new JFrame form or the design view if I add the panel and then add the label and rest of the contents, it makes no difference if I don't add a panel and add the contents like JLabel, JButton, etc. it makes no difference.
Is there any reason why panel should be added to the frame? I tried to close the application when the panel was inserted and when it was not inserted on the frame, the application closes both the times. (When I press run and try to close the application, both times it closes.)
Then what is the use of putting JPanel on a JFrame?
When you add components to the frame the components are added to the content pane of the frame which by default is a JPanel. Read the section from the Swing tutorial on Using Top Level Containers for more information.
By creating a separate panel and adding components to that panel you give yourself more flexibility when designing your application. For example you may want to use a CardLayout which allows you to swap out different panels on the frame. The tutorial also has a section on using CardLayout.
I'm developing a graphic interface using Java and Swing, and I'm having a hard time getting the JButtons to stay in their position while changing from one panel to another.
There are three buttons in a row aligned in the left bottom of each panel, all the panels the same size, but somehow they manage to change their position a little when I run the application (on the design preview they show up in the right place). It's getting a bit annoying. I'd appreciate any help
Are you trying to do tabs? If you are, a JTabbedPane will do this much better than a button.
Since you are using the Netbeans GUI Builder, look at the options in the Component panel on the left. It has Swing tabbed panes and AWT panes if you really want.
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.