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.
Related
I need to know which method gives you the panel panelA from my scrollpane.
For example, I have:
PanelExample panelA = new PanelExample();
JScrollPane scrollpane = new JScrollPane(panelA,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollpane.getPanel(); //???
scrollpane.getComponent(); //???
I guess it should be a get method, but I don't know.
You get the component from the JViewport of the JScrollPane:
Component c = scrollPane.getViewport().getView();
Read the section from the Swing tutorial on How to Use Scroll Panes for information on how the scroll pane works and the components involved in the layout of the scroll pane.
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)
I have a problem.
I have a JFrame with some JTextFields, JLabels, Jlists & JButtons now the contents of my frame is more than the screen area so I want to attach a JScrollBar to my JFrame but my srollbar does not work. So, can anyone please guide me on how I can scroll my JFrame using the JScrollbar?
Put all the components in one panel (instead of in the JFrame)
Add this panel to a JScrollPane
Add the JScrollPane to your frame.
I should be something like:
JPanel container = new JPanel();
container.add(panel1);
container.add(Panel2);
JScrollPane jsp = new JScrollPane(container);
frame.add(jsp);
It depends on the layout you are using with your JFrame. If you want to add working scrollbar to your panel you should look at the JScrollPane class and the Scrollable interface which must be implemented by scrollable components.
The How to use scroll panes chapter in the swing tutorial may also be an interesting reading: http://download.oracle.com/javase/tutorial/uiswing/components/scrollpane.html
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.
I have a JPanel that holds a JScrollPane that holds a JPanel as such:
masterPanel.add(buttonPanel, BorderLayout.NORTH);
inner.setLayout(new BorderLayout());
inner.add(infoPanel, BorderLayout.NORTH);
inner.add(writingPanel, BorderLayout.CENTER);
beholder = new JScrollPane(inner);
masterPanel.add(beholder, BorderLayout.CENTER);
I want beholder to be a JScrollPane so that the button panel will display at all times while scrolling the beholder (inner scroll pane). This all works fine, but the problem is the panels inside of beholder do not line wrap. So I have a long line of text in infoPanel that causes a very long, undesirable horizontal scroll.
If I just change the last line to add inner instead of beholder, it works fine (wraps correctly), but then the button panel won't stay at the top when the user scrolls down.
I'm totally stuck. Basically my question is how to get the panels inside of the JScrollPane to word wrap normally.
The problem is wrapping horizontally. Even if I set beholder.setPreferredSize() to a very low x and y dimension, it will ignore the x dimension entirely even though it seems to obey the y dimension normally.
I think the Scrollable Panel will help you out.
For some reason, this problem is resolved by calling:
inner.setPreferredSize(inner.getPreferredSize());
This will cause the wrap to occur.