How to autoscroll JScrollPane with JTextArea inside to the left - java

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

Related

how do I add a horizontal and vertical scroll bar to my JTextArea (java)

Can someone help me add a scroll bar, both vertical and horizontal, in the JTextArea?
textarea1 = new JTextArea();
textarea1.setBounds(17,183,208,100);
textarea1.setBackground(new Color(40,40,40));
textarea1.setForeground(new Color(225,228,0));
textarea1.setEnabled(true);
textarea1.setFont(new Font("sansserif",0,12));
textarea1.setText(chat1);
textarea1.setBorder(BorderFactory.createBevelBorder(1));
textarea1.setVisible(true);
Thanks :)
The following is how it could be achieved:
JTextArea ta = new JTextArea();
JScrollPane sp = new JScrollPane(ta); // JTextArea is placed in a JScrollPane.
Once the JTextArea is included in the JScrollPane, the JScrollPane should be added to where the text area should be. In the following example, the text area with the scroll bars is added to a JFrame:
JFrame f = new JFrame();
f.getContentPane().add(sp);

JScrollpane does not notice another JScrollpane

I have a JFrame which contains a JSplitPane in a JScrollPane (so the user can scroll if the window is to big). The JSplitPane contains a JTabbedPane as the top component and graphics as the bottom component.
Now i want to read a .csv und display it in my JTabbedPane. I can scroll through the list with a second JScrollPane. Here comes the problem, when i import the .csv in my programm, the first JScrollPane seems not to notice that there is a second JScrollPane for scrolling the list and then my window gets a lot of free space to scroll.
JFrame frame = new JFrame();
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP, JTabbedPane.WRAP_TAB_LAYOUT);
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
splitPane.setTopComponent(tabbedPane);
splitPane.setBottomComponent(graphics());
JScrollPane scrollPane = new JScrollPane(splitPane);
frame.add(scrollPane);
frame.setVisible(true);
When i import the .csv I add a new JPanel to the tabbedPane. The JPanel contains a list from the data from the .csv
JPanel panel = new JPanel();
JScrollPane scrollPane = new JScrollPane(panel);
// here comes the code for the list
tabbedPane.add(scrollPane);
I hope you understand my problem, it is hard to explain.
Edit: Pictures from before and after importing the .csv may help you to understand.
Get rid of the first scroll pane. Add the split pane directly to the CENTER of the BorderLayout used by the frame. As the frame resizes all the space will be allocated to the split pane.
You can then use:
splitPane.setResizeWeight(1.0);
Now all the extra space will go to the second component as the frame is resized. Therefore the scrollbar for that component will appear/disappear as required.

How to make a JTextArea scrollable in 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.

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...

scrollpane blocks buttons in BorderLayout?

I have a really weird problem with a JScrollPane and a BorderLayout. For short explaination: i have a JTable which is inside the JScrollPane and this is with a JPanel and the JTableHeader on a JTabbedPane. Very Simple Layout. If i add just the JTable to my JPanel, the buttons are working. If i add the JScrollPane, the Buttons are not working anymore, so i cant click them! The ActionLister is never reached and i cant see the click-animation.
Some Sample code to explain:
d_pane = new JPanel();
d_button = new JPanel();
d_pane.add(table.getTableHeader(), BorderLayout.PAGE_START);
dl_scroll = new JScrollPane(table);
d_pane.add(dl_scroll, BorderLayout.CENTER);
// d_button is ridLayouted with 3 Buttons in there
d_pane.add(d_button, BorderLayout.PAGE_END);
1) The JScrollPane takes care of the table header itself. Don't add it to the pane.
2) the button does not seem to get the mouse events, probably because another component is above it - do you have other components/code in the setup?

Categories