I use in my app a JscrollPane with a BoxLayout panel inside. I it to never change value after repainting. How can I do this?
I tried
pane.getHorizontalScrollBar().setValue()
but it doesn't change anything.
You could try setting the Horizontal (and/or) Vertical scrollbar policies:
pane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
pane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
You could also mess around with the Viewport's size (max & mins), etc.
Found solution.
Now I repaint panel inside scrollpane instead of scrollpane and it works.
Related
I would like to scroll through the contents of my JFrame up and down, preferably with a scroll bar. I don't want to wrap the contents inside a JPanel or JScrollPane, because this causes some visual glitches with my application.
Any idea on how to do this?
JScrollPane would be the easiest way; you say there are glitches, but that probably indicates a problem in your code that will still be a problem even without using a JScrollPane.
If you're absolutely set on not using a JScrollPane, you should create a JPanel using BorderLayout, add a JPanel (call it 'center') with BorderLayout.CENTER and layout set to null. Add your content within 'center', and add another JScrollBar to BorderLayout.EAST, add an AdjustmentListener to the JScrollBar. When the adjustmentListener triggers, you need to move your content (Component.setLocation(...)) that's in center to the relative y offset of the JScrollBar and call repaint on 'center'
I have a JPanel in a JScrollPane. When JPanel changes (in my case, I draw an image inside), the scroll bars are not refreshing accordingly. I need to move them sligthly or resize the whole frame and then everything is fine. How to force JScrollPane to show correct scroll bars (ie. after loading the picture)?
You should include these methods when you want to modify the JScrollPane according to some changes:
JScrollPane.setViewportView(this);
JScrollPane.revalidate();
JScrollPane.repaint();
I am trying to set preferred width for jscrollpane in borderlayout(WEST).but it is not working
Please look into my sample code
JTree tree=new JTree(root);
JScrollPane jsp=newJScrollPane(tree);
jsp.setBounds(0,0,200,100);
Jframe.add(jsp,BorderLayout.WEST);
but it is showing default width.
Alternatively I tried to set preferred size for JTree , it is working fine but jscrollpane is not working properly.Please help me in this.
It is the layout manager who determines the position of the component, based on the sizes of the component (minimum/maximum/preferred). So your call to setBounds will be ignored and instead the BorderLayout will determine where it places your component.
Normally this mechanism works just fine. The problem with a JScrollPane is that its size hints might not be in sync with what you want as behavior (due to the fact the scroll pane can have scroll bars, so it can determine its own size).
Solution: just call setPreferredSize on the JScrollPane before adding it. This is also done in the official tutorial and about the only time I can think of that it is acceptable to call this method.
I have a panel with flow layout, and it can contain a variable number of items - from 1 to 2000. I want to put it inside a scroll pane, scrollable in vertical direction, and with fixed width. The problem is, when I set preferred size of panel to something like (800,600), some items are missing, and there is no scroll. If I set up preferred size of scroll pane, then all elements in flow pane are put on one very long line.
Setting maximum size on any element seems to do nothing at all - layout managers ignore it.
How can I fix this?
I want to put it inside a scroll pane, scrollable in vertical direction, and with fixed width
You can use the Wrap Layout for this.
Don't set the preferred size of the panel. But you can set the preferred size of the scroll pane so the frame.pack() method will work.
You could use BoxLayout to do this:
JPanel verticalPane = new JPanel();
verticalPane.setLayout(new BoxLayout(verticalPane, BoxLayout.Y_AXIS));
JScrollPane pane = new JScrollPane(verticalPane);
//add what you want to verticalPane
verticalPane.add(new JButton("foo"));
verticalPane.add(new JButton("bar"));
This of course will use the preferred size of each component added. If you want to modify the preferred size for example of a JPanel, extend it and override getPreferredSize:
class MyPanel extends JPanel(){
public Dimension getPreferredSize(){
return new Dimension(100,100);
}
}
A note: BoxLayout will take in consideration getPreferredSize, other LayoutManager may not.
Please criticize my answer, I'm not sure it's completely correct and I'm curious to hear objections in order to know if I understood the problem.
Guys, I need to put some buttons in a jscrollpanel, but the JScrollPane won't create a scroll vertically. I'm using a JPanel inside the JScrollPane which is using the simple FlowLayout layout. How can I make the JScrollPanel to scroll only in the vertical??
Problem:
Desired Solution:
Check out the Wrap Layout
The fact you use a JScrollPane changes quite a few things concerning the internal FlowLayout. indeed, when the FlowLayout tries to layout contained JButtons, it use for that the available space. In your case, you don't have limits to the space in the "scrollable client" of your JScrollPane. As a consequence, considering your FlowLayout has infinite space, it uses this space to display items according to it.
So the solution would be to change your scrollable client in order to limit its viewable area to the same than your JScrollPane's JViewport.
However, you would not even in this case have your line returns, as FlowLayout don't really well handle this case.
Were I to be you, I would of course choose an other layout. As GridLayout don't really well handles borders, i think the only reasonible standard layout you can use is GridBagLayout, althgough I fear your dynamic content constraints may require you something even more customizable.
JTextArea c = new JTextArea();
c.setLineWrap(true);
c.setWrapStyleWord(false);
This will wrap anything in a text area to the next line without creating a Horizontal Scroll.
Use the modified Flow Layout that I posted in this answer: How can I let JToolBars wrap to the next line (FlowLayout) without them being hidden ty the JPanel below them?
It will wrap to the next line and your scrollbar should scroll vertically.
scrollbar = new Scrollbar(Scrollbar.VERTICAL);
Or you could use a JList.
See this site for more info: http://docs.oracle.com/javase/tutorial/uiswing/components/list.html
the example class: ListDialog uses only a vertical scrollbar, when the window is resized or the elements don't fit the view.