JTextArea forces the parent JScrollPane to scroll down - java

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.

Related

How to use scrollbar for a JTextField in JAVA using eclipse GUI drag and drop tool?

I am using the eclipse GUI drag and drop tool. I have a JTextField in my program which shows content from a line in a textfile. Now, I want to add a horizontal scrollbar underneath the JTextField so that the user can view the content no matter how long it is.
I have the scrollbar and JTextField ready in my JFrame. Can someone provide me with guidance on how to link them please?
Don't try to use a JTextField and a JScrollBar.
Instead, use a JTextArea which is designed to work with a JScrollPane:
JTextArea ta = new JTextArea(1, 10);
ta.getDocument().putProperty("filterNewlines", Boolean.TRUE);
JScrollPane sp = new JScrollPane( ta );
sp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
You need to reserve space for the scrollbar otherwise it will paint over top of the text. The scrollbar will be activated when required.
This "filterNewLines" property just removes newline characters from the text so only a single line of text can be displayed in the text area.

Enable ScrollPane when components exceed inside a text area

Before anything else there might be some people who already asked this question. However, i am certain that I couldn't google it. Anyway, I have a scrollPane which has a viewPortView of textArea. My question is I would like to show my scrollpane when i insert numerous components inside my textArea. How am i supposed to do this? I have no idea and I'm not that expert with Javax Swing.
Code goes like this:
textArea = new JTextArea();
scrollPane = new JScrollPane();
textArea.setBounds(0,50,520,550);
textArea.setBackground(Color.DARK_GRAY);
scrollPane.setBounds(textArea.getBounds());
scrollPane.setViewportView(textArea);
thanks for the help!
My question is I would like to show my scrollpane when i insert numerous components inside my textArea.
A text area displays text, not components. The scrollbars will appear automatically when you actually add text to the text area.
textArea.setBounds(0,50,520,550);
Don't use setBounds. Swing was designed to be used with layout managers. In particular a JScrollPane will only work properly when you use layout managers.
//textArea = new JTextArea();
textArea = new JTextArea(5, 20);
When you create a JtextArea use code like the above. This will allow the text area to determine its own preferred size. Then scrollbars will appear once you add more than 5 rows of text.
Read the section from the Swing tutorial on How to Use Text Areas for more information and working examples. Keep a link to the tutorial handy for all Swing basics.
Just for information,
If you have multiple lines in your text area, the scroll bar is by default scrolled to the end of the text area. To keep the lines in the text area wrapped and scroll bar to the top of the text area, following code would help
textArea .setWrapStyleWord(true);
textArea .setLineWrap(true);
DefaultCaret caret = (DefaultCaret) textArea .getCaret();
caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);

JTextArea with JScrollPane isn't scrolling once full

I'm trying to make my JTextArea scrollable when it fills up with text but when I add a JScrollPane it just adds a scrollbar that doesn't do anything. When I add more text than my JTextArea can display it doesn't change and doesn't append any more text.
Container window = getContentPane();
window.setLayout(new FlowLayout());
display = new JTextArea(TEXT_AREA_ROWS, TEXT_AREA_COLUMNS);
display.setLineWrap(true);
display.setPreferredSize(TEXT_AREA_DIMENSIONS);
display.setBackground(TEXT_BG_COLOR);
display.setForeground(TEXT_COLOR);
display.setEditable(false);
display.setFont(TEXT_FONT);
window.add(display);
scroll = new JScrollPane(display);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scroll.setPreferredSize(display.getPreferredSize());
window.add(scroll);
Thanks in Advance
EDIT: Realised my mistake, I was setting the preferred size of the textArea instead of the scrollPane.
This is solved by removing display.setPreferredSize(TEXT_AREA_DIMENSIONS); and adding a scroll.setPreferredSize(new Dimension(width, height)); Silly me.
but if you append text to it with display.append(string) then the text gets added to the bottom which may or not be on screen at the time.
Well, you didn't state that you were using the append(...) method in your original question. That is why you should always post a proper SSCCE that demonstrates the problem so we don't have to guess what you are doing.
See Text Area Scrolling for the probable problem and a solution.

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.

JScrollPane in a popup window does not scroll up programmatically

I have a popup menu that displays dynamically created custom JPanel objects in a JPanel in a JScrollPane. The popup menu displays recommendations to the user and the topmost element is the most relevant recommendation. I am using JPopupMenu to display the window:
JPanelTemplatePopup jptep = new JPanelTemplatePopup();
JPopupMenu popup = new JPopupMenu();
popup.add(jptep);
popup.show(this, 500, 100);
The problem is, I can't make the JScrollPane scroll to the topmost element to display it first. I have tried:
.getViewPort().setViewPosition(new
Point(0,0));
.scrollRectToVisible(firstelement.getBounds());
before and after validate()s. No matter what I do, when the window pops up, the scroll pane always stays at the same place.
I have even suspected that the operations that took place before displaying the window were ignored, so I created and called a public method from the class to make the window scroll it up after being displayed. Nothing changed.
Please help,
Emre
Yeah, well, found a workaround for this.
I have narrowed the problem down to a JEditorPane that was in the custom JPanel objects. Its contents were dynamically updated by the program. While generating the objects, I was setting JEditorPanes' contents by their setText methods. Updating the caret position by setting the text to a string forced the scroll pane to scroll below.
I just inserted this to the constructor and the problem was fixed:
DefaultCaret caret = (DefaultCaret) jEditorPaneContents.getCaret();
caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);

Categories