How to make a JTextArea scrollable in java? - java

Hi I have made a JTextArea but it is not scrolling, can someone please tell me why?
JTextArea textArea = new JTextArea(2, 0);
textArea.setText("sdsdsd \n dfdfdf \n dsdsdsdsd \n dsdsdsd \n sdsdsdsd");
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);
panel.add(textArea);
Also, I would like it to auto scroll down, when new content gets added to, show it only shows the last 2 lines automatically, if possible.
Thanks.

Use,
panel.add(scrollPane);
not
panel.add(textArea);

Add the JScrollPane to the JPanel, not the JTextArea.
To Scroll to the bottom, see this answer.

Related

How to autoscroll JScrollPane with JTextArea inside to the left

I have private JTextArea opisDiagnoza; I insert it into JScrollPane jsp, like this:
opisDiagnoza = new JTextArea("Opis diagnozy:\n");
JScrollPane jsp = new JScrollPane(opisDiagnoza, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
Later on, when I do:
opisDiagnoza.setText(REALLY_LONG_TEXT);
the JScrollPane autorscrolls to right, so to read the content I need to manually(by clicking horizontal scrollbar) scroll it to the left.
How to autoscroll JScrollPane to the left after inserting a text into JTextArea inside of it?
Example:
http://i.stack.imgur.com/fxc4x.png
I can't add image explicitly because of low reputation.
Try this
DefaultCaret caret = (DefaultCaret)textArea.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);

JTextField becomes HUGE in a HorizontalBox

Please consider this Java code fragment:
Box buttonBox = Box.createHorizontalBox();
buttonBox.add(new JCheckBox("Select all"));
buttonBox.add(Box.createHorizontalGlue());
buttonBox.add(new JLabel("Filter: "));
buttonBox.add(new JTextField());
Box paneBox = Box.createVerticalBox();
paneBox.add(buttonBox);
paneBox.add(new JScrollPane(jList));
For some reason i don't get, the JTextField takes up most of the screen. The jList isn't even visible anymore. I would like to know why and how to fix it.
When i comment the line with the JTextField, it looks fine (except no JTextField, of course). Why does the JCheckBox and the JLabel not get ridiculously big? What is the purpose of a one-line JTextField being able to take up almost the whole screen?
Most people suggest to set the size of the JTextField field to my needs. However, i read that i should not call these methods, but let the LayoutManager take care of it. Now what is the most elegant solution to prevent the JTextField from becoming so big?
Try using the setPreferredSize method like this:
textFieldName.setPreferredSize(new Dimension(x, y));
Obviously input the values for x and y that you wish the JTextField size to be
You can avoid this problem by using a simple JPanel() instead of a Box for the horinzontal one. And after just set the prefered size to your JTextField.
//Box buttonBox = Box.createHorizontalBox();
JPanel buttonBox = new JPanel();
buttonBox.add(new JCheckBox("Select all"));
buttonBox.add(Box.createHorizontalGlue());
buttonBox.add(new JLabel("Filter: "));
JTextField jt = new JTextField();
jt.setPreferredSize(new Dimension(100,25));
buttonBox.add(jt);
EDIT
Btw the defaut Layout of a JPanel is the FlowLayout which place components from left to right and with centered alignement by default. You can still change the alignement.
For example:
JPanel buttonBox = new JPanel(new FlowLayout(FlowLayout.LEFT));
You can also change the gap between each components. There is some other Layout like the BorderLayout or GridLayout / GridBagLayout. I let you search on Google for more information about them.
See in yellow the JPanel, in red the VerticalBox
Box box = Box.CreateVerticalBox();
Use box.setMaximumSize(Dimension(160,80))
and box.setMinimumSize(Dimension(160,80))
then it will work :D
If you need a strut for your layout, put it in between boxes.

How to immediately show scroll bar in the JScrollPane

I added JScrollPane to my JPanel but I wanted to make scroll bar visible at the start of the program not only when the text is going out of the screen is that possible?
jpMiddle = new JPanel();
JTextArea ta = new JTextArea(20,43);
JScrollPane sp = new JScrollPane(ta);
jpMiddle.add(sp);
JScrollPane sp = new JScrollPane(ta,
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
That's for the scrollbars to be active always, you can substitute the ALWAYS for AS_NEEDED to ensure they appear only when the contents of ths scrollpane overflows.
The API exists for one reason, though...

No text area in JOptionPane window

Is there anyway to have a JOptionPane window without the text area but with only a scroll bar,
JTextArea listBox = new JTextArea(aLineFromFile);
JScrollPane scroll = new JScrollPane(listBox);
listBox.setLineWrap(true);
listBox.setWrapStyleWord(true);
scroll.setPreferredSize(new Dimension(200, 400));
JOptionPane.showMessageDialog(null, scroll, "Dictionary enteries", JOptionPane.PLAIN_MESSAGE);
I don't want the white background behind the text I just want the scroll bar.
Or anyway to make the text in the text area uneditable.
"Or anyway to make the text in the text area uneditable."...
Try:
listBox.setEditable(false);
Also, you might be better off using a JLabel rather than a JTextArea.

Text goes outside JTextArea

I am trying to do something in Java which requires me to have a JTextArea in a ScrollPane.
Here is how I have defined them:
private JTextArea longestparagraph = new JTextArea();
....
JScrollPane scrollpanedreapta = new JScrollPane(longestparagraph,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollpanedreapta.setBorder(BorderFactory.createTitledBorder("Cel mai lung paragraf:"));
The problem I'm encountering is that the text doesn't start on a new line when it reaches the right border of the TextArea but it continues. Got any ideas how to solve that? Here's a picture to make my statement a little bit clearer.
Found the answer. Just had to this:
longestparagraph.setLineWrap(true);
longestparagraph.setWrapStyleWord(true);

Categories