How to add scrollbar to panel? - java

I need help. I have one panel which can need to have width 1000px. I need to add lot of buttons with different size ( I add with flow layout and it works fine). Problem is that I have height on screen example 500px but when I add buttons panel has bigger size. How to add scrollbar to panel ?

Add your panel to scrollpane and add that pane where you are adding your panel instead of panel
JScrollPane jScrollPane = new JScrollPane(panel);

Related

JScrollpane not scrolling when collapsible panels are added

I have added some collapsible panels to my JScrollpane. When they are extended, the JScrollpane is not allowing me to scroll. Below is the code I'm using.
JScrollPane scrollPane = new JScrollPane(makeContentForPropertiesWindow());
propertiesTabDoc.add(scrollPane,BorderLayout.CENTER);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
The makeContentForPropertiesWindow method populates the collapsible panels. I've used GridLAyout there. I've also tried FlowLayout and BorderLAyout as well. All result in the same output. Currently below is how it looks.
What am I doing wrong here? Why is it not allowing me to scroll though the vertical area is full? Please advice.

Swing: how to add two JScrollPanes with TableLayout?

For my Java program I am actually using the simple library TableLayout as layout for my main JPanel body so that I can add any widget just by specifying its row and column index, for example"
body.add(new JLabel(
"Search by date"),
"1,8");
Now I would need to add two JScrollPane (one horizontal and one vertical) but they should include all the body and not just a single cell of the layout. Shall I add another JPanel? How can I do it?
Now I would need to add two JScrollPane (one horizontal and one
vertical) but they should include all the body and not just a single
cell of the layout. Shall I add another JPanel?
IMO, yes you should. Nesting Layouts is a common approach that could be applied in this way:
Create a new JScrollPane and set your panel as its viewport view.
Give the scroll pane a reasonable preferred size to enable the scroll bars if your panel's size exceeds this preferred size.
Have a wrapper panel with BorderLayout and add the scroll pane to its CENTER location.
In a nutshell:
JScrollPane scrollPane = new JScrollPane(yourPanel);
scrollPane.setPreferredSize(new Dimension(400, 300));
JPanel wrapperPanel = new JPanel(new BorderLayout());
wrapperPanel.add(scrollPane);
See also:
How to Use Scroll Panes

JScrollpane doesn't display properly

I have added a scroll pane to the main panel of my frame. But it doesn't display properly, here's what I get that appears on the right:
http://postimage.org/image/extp3ncql/
here is the code:
JScrollPane jScrollPane = new JScrollPane(area);
jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
jScrollPane.setViewportBorder(new LineBorder(Color.RED));
pane.add(jScrollPane, BorderLayout.EAST);
EDIT: Forgot to mention that area is a label.
1) Use another proper LayoutManager, I'd suggesst use Box or directly BoxLayout
or
2) all areas excluding BorderLayout.CENTER acepted PreferredSize came from JComponent
3) if your area is JTextArea the you can pretty to set JTextArea(int rows, int columns)

How does one make an applet scroll?

I have been trying to use JScrollPane with my applet, but it doesn't work. I have a JPanel to which I add 20 buttons, and I want to be able to scroll up and down this JPanel. Instead, the scrollbars do not appear. When I use setPreferredSize they still did not appear even though only about 3 of the buttons are being displayed and the rest are cut off. If I do not use setPreferredSize, there might as well not be any scrollbars because I have to make the window big enough to see all of the buttons. If I try to make the scrollbars always visible, they appear but do nothing. I tried the exact same code with JFrame instead of Applet, and it works fine, but I need it to be an applet. Is JScrollPane incompatible with applets? (Note: I tried to use an outer JPanel and add the scrollable panel to it, but it changed nothing). Changing the layouts also doesn't fix the problem. I have attached a simplified version of my code, but it displays the same errors.
Here is the code I have:
JPanel scrollPanel = new JPanel();
scrollPanel.setLayout(new BoxLayout(scrollPanel, BoxLayout.PAGE_AXIS));
JScrollPane scroll = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
for (int i = 0; i < 20; i++) scrollPanel.add(new JButton("Button " + i));
add(scrollPanel);
validate();
You never all the panel to the scroll pane
You never add the scroll pane to the applet
The basic code should be:
JScrollPane scrollPane = new JScrollPane(...);
scrollPane.setViewportView( scrollPanel );
add( scrollPane );
You are adding components to a Panel so you shouldn't expect to see a scroll pane wihout showing the scrollpane. What you want to do is then add that panel to a scrollpane which would be added to ur main container.
From your code, i think your problem is
add(scrollPanel);
your should be doing this
add(scroll);`
This is because you only added the panel to the frame which does not contain any scrollpane. Since you have added the panel unto the scrollpane, you should add the scrollpane and not the panel to the main container.
It sounds like you are using Swing components (JScrollPane, JPanel, ...) in an AWT container (Applet). Try using JApplet instead.

How do I make JScrollPane work properly with nested JPanels?

I'm building a Swing application in Java using NetBeans and I have a problem with layout. My main frame contains a JScrollPane which contains a JPanel called contentPanel which in turn contains a JPanel called listPanel. The listPanel is empty when the program starts, but when the user interacts with the program an unpredictable number of smaller JPanels are added to it. I've used the NetBeans GUI-builder to snap the top edge of listPanel to the top of contentPanel, and the same with the bottom edges.
The problem I have is that when more components are added to listPanel the vertical scrollbar doesen't appear on my scrollpane. The verticalScrollBarPolicy of my scrollpane is set to AS_NEEDED and its viewportView is set to contentPanel. What I think I need to do is to make contentPanel grow when more items are added to listPanel.
The problem I have is that when more components are added to listPanel the vertical scrollbar doesen't appear on my scrollpane.
The scrollbar will appear when the preferred size of the component added to the scrollpane is greater than the size of the scrollpane. When you add components dynamically you need to tell the scrollpane something has changed. So you basic code should be:
panel.add( subPanel );
panel.revalidate();
Or, because you are adding a panel to the sub panel, you may need to revalidate the scrollpane (I don't remember):
panel.add( subPanel );
scrollPane.revalidate();
The key is the revalidate() which tell the layout manager to recalculate its size.
Use a different LayoutManager. One that will allow for vertical growth like BoxLayout. Also remember that you can use multiple layouts and nest them inside of each other for different effects.

Categories

Resources