I am using a JScrollPane which contains inside it a JTextArea. The problem is when the JTextArea is populated with a lot of text, the JScrollPane automatically scrolls down. What property should I set on the JScrollPane to avoid this automatic scrolling down? Thanks.
If the JScrollPane is decorating a text component, it will automatically scroll to the bottom, but after it loads calling setCaretPosition(0) on your text component will cause it to scroll to the top.
However, if it's not a text component, you can also alter the viewport like this:
scrollPane.getViewport().setViewPosition(new Point(0,0));
Related
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.
I have a panel inside a JScrollPane and I dynamically populate the panel with components as the data gets received from the service. The panel uses GridBagLayout (but that should be irrelevant). For each record that comes back from the service, I create several components dynamically and append them to the bottom of the panel. Everything works fine, but the problem is that JTextArea that gets created for each record forces the main JScrollPane to scroll down and show the last added JTextArea, as shown here:
I tried to disable everything I could think of to dumb down the JTextArea, but still doesn't help
JTextArea descriptionArea = new JTextArea(project.getDescription().replace("<br>", "\n"));
descriptionArea.setEditable(false);
descriptionArea.setFont(thumbnailLabel.getFont());
descriptionArea.setLineWrap(true);
descriptionArea.setWrapStyleWord(true);
descriptionArea.setFocusable(false);
descriptionArea.setRequestFocusEnabled(false);
DefaultCaret caret = (DefaultCaret) descriptionArea.getCaret();
caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
How can I prevent it from moving the scrollbar? I tried replacing JTextArea with JLabel and that works, but I can't get the JLabel to word wrap the text as good. Any ideas would be highly appreciated.
You could do something like:
Point p = srcollPane.getViewport().getViewPostition();
// add the components to the panel in the viewport of the scrollpane
scrollpane.getViewport().setViewPosition( p );
Now the scrollpane should be reset to its original position before you added the components.
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 JScrollPane with a View. I'm doing a chat application , it's why i need to have my ScrollBar to the Maximum(). To get the right Maximum of the View i have to validate before. When I validate my ScrollPane an automatic repaint is doing. I don't want this repaint because it makes a double repaint when i set the ScrollBar to the Maximum : when the ScrollBar is at the top and a another when it is a the bottom.
my code :
Main.getWindow().getMainPanel().getScrollPaneCenter().validate();
scrollPaneCenter.getVerticalScrollBar().setValue(scrollPaneCenter.getVerticalScrollBar().getMaximum());
PS : I want to disable the repaint of my conponent or maybe you have a solution to have a reversed JScrollPane( to always have the ScrollBar to Bottom) .
I'm doing a chat application
I'm guessing your are using a a JTextArea or JTextPane.
it's why i need to have my ScrollBar to the Maximum().
You don't need to validate or set the scrollbar manually. You juat append the text to the bottom of the Document. See Text Area Scrolling for a couple of solutions.
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();