How do I add ScrollPane to a panel in Java? - java

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.

Related

Filling a JPanel

I have a few items in a Jpanel which is then pushed to the top and used as a toolbar for a basic search engine. I'm having an issue where my last combobox isn't displaying as there isn't enough room. However, there's a lot of empty space on the left side and I need everything to move across to fill the JPanel so then this can display. So my question is how would I make these items start from the far left and go to right, thanks.
//Labels for combo boxes
JLabel Bookmarklbl = new JLabel("Bookmarks:");
JLabel Historylbl = new JLabel("History:");
FlowLayout flowLayout = new FlowLayout();
MainBrowser.toolBar.setLayout(flowLayout);
//Adding items to Panel
MainBrowser.toolBar.add(Bookmarklbl);
MainBrowser.toolBar.add(BookmarkList);
MainBrowser.toolBar.add(bookmarkbtn);
MainBrowser.toolBar.add(back);
MainBrowser.toolBar.add(forward);
MainBrowser.toolBar.add(MainBrowser.addressbar);
MainBrowser.toolBar.add(home);
MainBrowser.toolBar.add(reload);
MainBrowser.toolBar.add(Historylbl);
MainBrowser.toolBar.add(historyList);
//Set the things added from left to right
MainBrowser.main.setComponentOrientation(
ComponentOrientation.LEFT_TO_RIGHT);
//Add Panel to main frame
MainBrowser.main.add(MainBrowser.toolBar,BorderLayout.NORTH);
How the bar looks:http://postimg.org/image/l314iw6eh/
Assuming toolbar is JPanel and is using FlowLayout, this code might help you,
JPanel panel = new JPanel(); // your toolbar panel
FlowLayout flowLayout = (FlowLayout) panel.getLayout(); // flowlayout
flowLayout.setAlignment(FlowLayout.LEFT); // alignment to left
contentPane.add(panel, BorderLayout.NORTH); // adding this panel to original frame
Hope this helps
The default for a FlowLayout is CENTER. If there is not enough space to display all the components, then the components are wrapped to the next line. Changing the alignment to LEFT won't fix this problem (just the default alignment of components).
showing the combobox bar is very large is there anyway I can limit the width?
You can limit the width of the combo box by using:
comboBox.setPrototypeDisplayValue( "XXXXXXXXXX" );
This will limit the preferred size of the combo box so it can display on your toolbar.
However, you will still want to see the full text of the items when the popup is displayed. For this you can use the Combo Box Popup.
You can try to use BoxLayout, like:
toolBar.setLayout(new BoxLayout(toolBar,BoxLayout.X_AXIS)).
Maybe this will work.
MainBrowser.toolbar.set(new FlowLayout(FlowLayout.LEFT));
EDIT
Sorry it is MainBrowser.toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));

Scroll Pane Activating When Frame Is Large

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

How to make scrollable to jPanel

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)

Display Form in ScrollPane

I created a form. Actually it is 10 JLabels with each JLabel having a text field next to it.
consider,
JLabel_called_Name JTextField_to_obtain_name
JLabel_called_Phone JTextField_to_obtain_phone_number
and so on..
I usually position this in a JPanel and display it in a frame. But my panel and frame have height smaller than the size required to hold 10 of these Labels and Textfields.
So I wish to add them to a JScrollPane.
But in every question I only obtained information of how to add Jlabels to a scroll pane using a Box,
or adding JLabels to a JList.
However I would like to represent it in the format I showed above. A Jlabel beside a JTextField.
How can one acheive this?
But in every question I only obtained information of how to add Jlabels to a scroll pane using a Box, or adding JLabels to a JList.
You can add any component to a JScrollPane:
JPanel = new JPanel();
panel.add( label1 );
panel.add( textField1 );
JScrollPane scrollPane = new JScrollPane( panel );
The trick is choosing the correct layout manager for you panel. Read the Swing tutorial on Layout Managers to help you decide how to design the panel. You can also nest panels to get your desired layout.

Scrollable flow panel

I need to create a panel where I can put some rectangles and it automatically reorder just inserting a scrollbar and growing up vertically. Also this panel can be resizable and again the rectangles must to be reordered to correctly be displayed inside the panel.
If I understand the question you want components to wrap to the next line so that the panel grows vertically while the width remains fixed.
If so then check out the WrapLayout
Note: the FlowLayout already supports the wrapping of components to a new row on the panel. This issue is that the preferred size calculation assumes all components are placed on a single row. The WrapLayout overrides the preferred size calculation to support the wrapping of components on a new row.
Use a JScrollPane. If you never want a horizontal scroll bar you can add the following:
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
(By default the scroll pane will add horizontal and vertical scroll bars when required.)
The scroll pane itself will only be resizeable if you add it to a Container with the appropriate layout manager; e.g.
JFrame frm = new JFrame();
frm.setLayout(new BorderLayout());
JScrollPane sp = new JScrollPane();
frm.add(sp, BorderLayout.CENTER); // Adding a component to the CENTER will cause the component to grow as the frame is resized.

Categories