I am creating a JFrame application that contains a menu, toolbar, table, and button panel. I want to use a scroll pane as the top-level container so that if the user resizes the application the buttons, etc. don't just fall off the screen.
Here is my constructor code for the main frame:
JPanel mainPanel = new JPanel(); // borderlayout, north=toolbar, center=table, south=buttton panel
mainPanel.setLayout(new BorderLayout());
mainPanel.setBorder(BorderFactory.createEmptyBorder());
mainPanel.add(new MainToolBar(inventoryActions, false), BorderLayout.NORTH);
InventoryTable inventoryTable = new InventoryTable();
JScrollPane tableScrollPane = new JScrollPane(new JTable());
mainPanel.add(new JScrollPane(tableScrollPane), BorderLayout.CENTER);
mainPanel.add(tableScrollPane, BorderLayout.CENTER);
inventoryTable.adjustColumnWidths();
mainPanel.add(new InventoryActionsButtonPanel(inventoryActions), BorderLayout.SOUTH);
JScrollPane mainScrollPane = new JScrollPane();
mainScrollPane.setBorder(BorderFactory.createEmptyBorder());
mainScrollPane.setViewportView(mainPanel);
getContentPane().add(mainScrollPane, BorderLayout.CENTER);
As you can see.. I am using a JPanel to contain the toolbar, table (nested in another scroll pane), and button panels. I then put this panel inside the main scroll pane. Finally, I add the main scroll pane to the content pane.
The initial size of my application is 800x600. When I run it everything looks fine. Here is a screenshot:
When I resize the frame, however, the scroll panel "activates" way before the frame gets small as you can see here:
Any idea as to why my scroll panel is showing the scroll bars while the frame is still that big? Is there some preferred size that I have to set?
Thank you.
Note, if I comment out the line of code that adds the scroll pain to the main panel (center) or if I use another swing component, such as a JButton, it doesn't behave as before:
//mainPanel.add(tableScrollPane, BorderLayout.CENTER);
Here is a screenshot:
The (main) problem is the fact that the JScrollPane is using the JTable's getPreferredScrollableViewportSize, which overrides the getPreferredSize property.
By default this is set to 450x400.
You can change this by using JTable#setPreferredScrollableViewportSize
Another trick is to add the JTable to another container (like a JPanel) and set this as the scroll pane's view instead
Related
Sorry if I might mix terms up. So I have a basic application where I would press the button and Jpanel1 with a label in it, would then switch/replace to JPanel2 that'll have a picture in it, all within the panel.
The JPanel inside the box would change from Jpanel1 to JPanel 2 after pressing the button. So is their a way to instance a JPanel in a panel or JFrame? I can't find the method on how to fill the panel with the JPanel.
frame.getContentPane().add(panel, /*layout.location*/);
You mean add
The exact way to do this depends on the LayoutManager you use. I suggest checking out the Visual Guide to LayoutManagers. For example, if you use a BorderLayout, you can add a panel to the center and then replace it with a different panel when the user clicks a button.
I ended using layeredPane with a card layout. Then I placed a panel using the center Border Layout, then instanced the JPanel as the panel from a different class.
Picture of Windowbuilder layout
panelPPBrowser = new JPanel();
layeredPane.add(panelPPBrowser, "name_216881785358769");
panelPPBrowser.setLayout(new BorderLayout(0, 0));
panelBrowser panelBrowserCon = new panelBrowser();
panelPPBrowser.add(panelBrowserCon, BorderLayout.CENTER);
I have been working on a JFrame, which contains a panel of multiple components. However, the frame cannot accommodate all components due to its size. I wish to use a scrollable frame, and came across a link where JScrollPane use is suggested. However, when the frame is resized, the scroll pane container does not take up the size of the resized frame.
Is there a way to achieve this?
By default a JFrame uses a BorderLayout. When you add a component to the CENTER of the BorderLayout the component will take all the available space of the frame.
So the basic logic is:
JPanel panel = new JPanel(...);
JScrollPane scrollPane = new JScrollPane( panel );
frame.add(scrollPane, BorderLayout.CENTER);
Set the frame to use grid layout. Gridlayout makes all of the components in the frame use up the entire size.
Container contentPane = frame.getContentPane();
contentPane.setLayout(new GridLayout(1,1));
I've got a BorderLayout going on, and am working on the North panel. Inside the North panel, I'd like to have 3 components: a picture that is on the left, and two buttons that split the remaining width of the Frame. Right now I'm attempting to accomplish this with another BorderLayout.
The Frame is resizable.
The picture is assigned to BorderLayout.WEST, and with the following code I attempt to add another panel that contains only buttons. The panel is then added to the CENTER of the Frame's NORTH layout component.
//create panel to hold buttons
JPanel btnPanel = new JPanel();
btnPanel.setLayout(new BorderLayout());
JButton btnMatrix = new JButton("Matrix View");
btnPanel.add(btnMatrix);
JButton btnList = new JButton("List View");
btnPanel.add(btnList);
add(btnPanel);
however, the buttons both try to take up the entire panel. If I leave it to a flow layout (I don't use btnPanel.setLayout(new BorderLayout()); in the above code), the buttons sit nicely in the center, but do not expand and share the btnPanel.
Thoughts? I'm new enough to Java that I could be going about this the wrong way from the start.
btnPanel.setLayout(new BorderLayout());
You didn't specify a constraint when you added the buttons to the panel. So both buttons are added to the CENTER. However, only one component can be added to the CENTER, so only the last one added is displayed.
You can try a different layout:
btnPanel.setLayout( new GridLayout(0, 2));
Then each button will be the same size and both buttons will fill the space available.
I am making swing application. And there is too much height of my jPanel. So I want to make this panel as scrollable.:
Following is my description of my requirement.
I have four jpanel in one jpanel I mean:
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel p3=new JPanel();
JPanel p4=new JPanel();
I am adding p2, p3, p4 inside p1 like following output:
like above showing panel has more height than computer screen height. So I want to display all content of my panel on computer screen by scrolling.
I searched here and found the following questions:
How to make a JPanel scrollable?
How do i get vertical scrolling to JPanel?
However, the answers did not solve myproblem.
Without seeing your code, my guess is that you don't have a JScrollpane to provide the scrollable behaviour you want.
JPanel mainPanel = new JPanel(); //This would be the base panel of your UI
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel p3=new JPanel();
JPanel p4=new JPanel();
JPanel newPanel = new JPanel();
newPanel.add(p1);
newPanel.add(p2);
newPanel.add(p3);
newPanel.add(p4);
JScrollPane pane = new JScrollPane(newPanel);
mainPanel.add(pane);
Since you use NetBeans, add a JScrollpane from the palette in which you'll add a panel to contain the 4 others. I think you could also just add the 4 panel into the JScrollpane.
Add your panel to a JScrollPane. Assumed that you want vertical scrolling only:
JScrollPane scrollPane=new JScrollPane(panel,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
For fine-tuning the scroll amounts, you can optionally implement the Scrollable interface.
See also How to Use Scroll Panes (The Java Tutorial)
It is easy to design scroll pane using Netbeans IDE. Below given are the steps I followed to add a scroll pane:
1. In Netbeans GUI editor, select all panels which requires scroll pane using CTRL+left click
2. Right click on the hilighted panels, select the option 'Enclose in' -> Scroll Pane. This will add a scroll pane for the selected panels.
3. If there are other elements than Panel(say JTree), select all the elements ->Enclose in ->Panel. Then enlose the new parent panel to scroll pane
4. Make sure that 'Auto Resizing' is turned on for the selected parent panel(Right click on panel -> Auto resizing -> Tick both Horizontal and vertical)
I want to add different buttons, vertically stacked, to a JPanel at run-time and use a JScrollPane so that all buttons will be visible (with some scrolling).
In order to do this, I have added my JPanel to a JScrollPane, after which I add buttons to my JPanel.
However, when I do this the vertical scrollbar does not allow me to see all images. For example when I add 7 buttons I can only scroll to see 5 full images and half of the 6 images.
Why doesn't my scrollbar allow me to display all 7 buttons?
Create the panel and scrollpane like:
JPanel panel = new JPanel();
JScrollPane scrollPane = new JScrollPane( panel );
When you add buttons to the panel at run time the code should be:
panel.add( button );
panel.revalidate();
As long as you are using a layout manager the preferred size will be recalculated and the scrollbar will appear.
Make scroll pane a wrapper over your panel - new JScrollPane (myPanel) and add it instead of naked panel in your panel's container.
You also may want to play with its setPreferredSize() method.