I have TextArea in my Java app and I append lot off text rows. I need to ScrollPane to scroll to last appended ( to bottom of TextArea ). How I can do that ?
You could do that by moving the caret position to the bottom, this would automatically scroll the TextArea:
textArea.setCaretPosition(textArea.getDocument().getLength());
Text Area Scrolling explains how scrolling works in a little more detail and provides an alternative solution which may be easier.
If you scroll to the middle of the page where it talks about the policy, you should find what your trying to achieve: http://download.oracle.com/javase/tutorial/uiswing/components/scrollpane.html
As Amjad Masad already said, you need to set the caret position to the last position of the document after inserting your text.
I want to add following note: if you use the JTextArea as some kind of output log (AKA a working thread fills it continually), I only would set the caret position at the end of the document, if the current caret position is already at the end (before the insertion). This allows the user to click somewhere inside the text and read it without having the application automatically scroll down. If the user wants to see the latest and greatest, well, then (s)he has to press Ctrl+Down to put the caret at the end of the document.
You should check, if you are on the EDT, when you call the append. If you are outside of the EDT when calling append, the JTextArea does not scroll to the last appended line (see my post with a runnable example at: JScrollPane scroll at last added line).
Related
I want to know how in Java swing, it is possible to move the caret in the JTextArea to my desired position that I click.
If this is not possible then what could I do to make it possible?
(like IntelliJ virtual spaces).
and I want to do any time whether the text area is blank or not.
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'm working on a simple java program (using NetBeans' swing GUI builder). You're supposed to select a randomly placed item from the JComboBox as fast as you can, then it will tell you your time and save it (keeping statistics and whatnot).
Problem is, once you select an item and go back to find a different item (in a new random position in the JComboBox), the scrollbar seems to have remembered it's previous position.
Since this game is supposed to be competitive, it wouldn't really be fair for the scrollbar to be in the previous player's position.
Is there any way to set the position of the scrollbar in a JComboBox?
P.S. it's a vertical scrollbar. Also, I did try to find the answer online, but couldn't find it after over two hours of searching. Any help appreciated!
Select the first item so the scrollbar goes back to the top, then set the selection to -1.
comboBox.setSelectedIndex(0);
comboBox.setSelectedIndex(-1);
I used textarea in javafx 2.0 but i need to add it scrolpane.how can i do that?
Scrolllpane s = new Scrollpane();
s.setnode(textarea);
but when i click on scroll pnane it has doesn't move.
what is problem?
setNode() is the right method to call to set the node that the ScrollPane will scroll over. I've used ScrollPane extensively in my 2.0 app, but I have not tried it on Text Area. Based on the API documentation for TextArea (http://download.oracle.com/javafx/2.0/api/com/javafx/preview/control/TextArea.html) it sounds like it has its own built in scroll bars? I would try setting the width/height of the TextArea, and also set the max width/height, and see if you can trigger scrollbars to appear automatically when the lines in the text area exceed the available space.
If you still want to put it in a ScrollPane, perhaps with some other nodes, you should use a container node such as VBox or something to wrap the TextArea, then set the VBox to be your scroll node on ScrollPane.
Also, bear in mind that TextArea is not a committed control for FX 2.0 yet and is therefore less hardened than the other FX controls.
When we create a text area, scroll bar automatically appears when it goes beyond t
I have a JTextPane, into which I need to insert a JComponent. I'm using
JTextPane.insertComponent(Component)
The item is indeed inserted, but the vertical positioning is too high. Instead of having the bottom of the component aligned with the baseline of the current line of text, the component is way above that position, blocking out/over-painting lines of text appearing above.
I have tried calling setAlignmentY(float) with various values, on both the inserted component and the JTextPane, but it doesn't affect the behavior at all.
My guess: there seems to be some state inside my JTextPane or its Document that I need to be changing. But I don't know what it is.
Have you tried calling setSize(width, height) on the JComponent before you insert it into the JTextPane? It should work for most components.
I know, this is a pretty old question, but your approach of using setAlignmentY is totally correct. Don't know what code caused it to not work, but the javadoc of JTextPane.insertComponent(Component) says the following about the alignment:
The component is placed relative to the text baseline according to the
value returned by Component.getAlignmentY. For Swing components this
value can be conveniently set using the method
JComponent.setAlignmentY. For example, setting a value of 0.75 will
cause 75 percent of the component to be above the baseline, and 25
percent of the component to be below the baseline
So using textPane.setAlignmentY(1.0f) has the desired effect.
I ran into the same problem and could not find a solution using JTextPane or JEditorPane. But I was able to use JavaFX/WebView/WebEngine/JFXPanel. You will need to update to Java 8 (JDK 1.8). I made my own class HTMLPaneType, an extension of JFXPanel, and use HTMLPaneType in place of a JTextPane.
A JTextPane requires adding a HyperlinkListener if you want to respond to href clicks. HTMLPaneType requires adding a listener if you want to NOT respond to href clicks or to respond differently. In my case, I wanted to launch an external browser on an href click. I was able to do that with both the JTextPane and the extended JFXPanel. See also
http://blogs.kiyut.com/tonny/2013/07/30/javafx-webview-addhyperlinklistener/#.VK-JIHsueWN