JTextArea with JScrollPane isn't scrolling once full - java

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.

Related

JTextArea forces the parent JScrollPane to scroll down

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.

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);

Resizing JTextArea while using lineWrap

I want to create three JTextArea in my swing application.
Each JTextArea has a different size.
The first JTextArea should have 8 columns
The second one should only have 1 column
And the last one should have 50 columns.
My initial problem is that:
Whenever I type something, the JTextArea will keep on re-sizing its width.
This has been fixed by JScrollPane, setLineWrap(true), and setWrapStyleWord(true).
So here's my problem.
Whenever I add setLineWrap() to a JTextArea, the JTextArea will be resized.
My first and second JTextArea have been resized to 12 columns.
I searched and found some solution but they use MigLayout.
Is there any way to add word and line wrap in JTextArea without resizing it (and ofcourse, without the use of MigLayout)?
What's the easiest way to set the columns of JTextArea with word and line wrap?
What's the easiest way to set the columns of JTextArea with word and line wrap?
You create the JTextArea with code like:
JTextArea textArea = new JTextArea(5, 50);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
JScrollPane scrollPane = new JScrollPane( textArea );
JPanel panel = new JPanel();
panel.add(scrollPane);
frame.add(panel, BorderLayout.PAGE_START);
By default a JPanel uses a FlowLayout which respects the size of any component added to it. The BorderLayout.PAGE_START will repect the height of any component added to it.
Scrollbars will appear as required what text is added to the text area. So the key is to use a layout manager (or combination of layout managers) that meet your requirement.

How can I set scroll tab layout to my JTabbedPane?

I have a JTabbedPane. I drag it from the palette and paste it in my form. I put 5 panel on it. It is OK but when I put 6, panel on it it does not seen on the form because the place is not enough width. I put this code
tab_group.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
But doesn't affect my code. How can I fix it?
try this it will help.
JScrollPane pane = new JScrollPane(panel,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
add(pane);
add Jscrollpane to panel. problem solve

Add a JScrollPane to a JLabel

I can't seem to find out how I can add a JScrollPane to a JLabel. The JLabel that I'm using is populated with a long formatted HTML string. Please help.
area = new JLabel();
JScrollPane scroller = new JScrollPane(area,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
panel.add(scroller);
Really not good idea to hold or display long Html formatted text in the JLabel, since is possible, better would be use JEditorPanes / JTextPanes, these JComponets support styled and html formatted text, Icons etc ... , examples for JTextPane and JEditorPane
Can you provide us your code? Are you setting the viewport view to the JLabel? Instantiate your JLabel and a JScrollPane. then set the JScrollPane viewport to the JLabel (setViewPortView(jlabel);) then add the JScrollPane to whatever component you want the scrolling JLabel to be on.
Hope this helps!
You can't add a JScrollPane to a JLabel , what you can do is to create a JScrollPane and add a JLabel.
See this: http://www.cs.cf.ac.uk/Dave/HCI/HCI_Handout_CALLER/node63.html
You need to set the JScrollPane's viewport view:
scroller.setViewPortView(area);
stick that line just before you go panel.add(scroller);
Let us know if that helps or not.

Categories