I am trying to scroll JTextPane to specific line. For doing that, i put my text pane into the JScrollPane as JViewport. I determine the x and y coordinates of the line with :
JViewport port = (JViewport)getParent();
JScrollPane pane = (JScrollPane)port.getParent();
float calculated = (float)i/(float)lines.size() * 100;
pane.getVerticalScrollBar().setMaximum(100);
pane.getVerticalScrollBar().setValue((int)calculated);
With this code block, i can scroll the scrollbar but i can not scroll the context of text area. Only scrollbar moves, but displaying text seems the same as before. I need to display the line which is calculated.(By the way calculated variable is between 0-100)
Check out the Text Utilities class.
You can use the gotoStartOfLine(...) method to position the caret at the beginning of a line. This will cause the text area to scroll if required.
Or maybe you can use the centerLineInScrollPane(...) method.
Related
How do I make a certain line of text in a jEditorPane visible when it is in a JScrollPane?
private JEditorPane myEditorPane = new JEditorPane();
private JScrollPane myScrollPane = new JScrollPane(myEditorPane);
myEditorPane.setContentType("text/html");
myEditorPane.setText("<html>" + getMyString(x) + "</html>");
myEditorPane.repaint();
getMyString gets a long String with many lines, separated by \n.
The programme has 2 panels. The programme then goes down the lines in the above panel and for each one underlines the text in that line and displays a related image in another panel. Each is viewed for 1 second then moves on to underline the next line of text and show the next image. I've got it going down and underlining them in turn, displaying the relevant images for each underlined line of text. But the scrollpane jumps to the start every time.
I think I've got to use scrollRectToVisible on the viewport, but how do I find out what the rectangle is for part of the string in the JEditorPAne?
I've been able to manipulate scroll bar position by directly calling setValue on the scroll bar.
myScrollPane.getVerticalScrollBar().setValue()
The question is, what value to set? Can you assume that the lines of text are the same? If they wrap, things are going to get complicated. I'm going to assume they don't, and that the height of each line is the same because the font and size is the same. Then you'll need to know what index the current displayed line is, and the total count of lines.
Once you've got that, you convert the line index to editor Y position by multiplying the line index by editor height over line count and set accordingly.
myScrollPane.getVerticalScrollBar().setValue( (int) indexOfCurrentLine * myEditorPane.getHeight() / countOfLines );
Obviously haven't been able to test any of this without an SSCCE.
I wish to obtain and modify the number of pixels between the text contained inside the text area and the border of the text area. To provide a more descriptive visual:
where the length of the blue line is what I desired. When I received both the padding and margins of the text area in my application, I got 0px. I assume that this is the padding/margin of the TextArea relative to the outside, not the inside relative to the Ttext area.
Many thanks.
In java to set the insets of a textarea you can use setMargin().
public void setMargin(Insets m)
Sets margin space between the text
component's border and its text. The text component's default Border
object will use this value to create the proper margin. However, if a
non-default border is set on the text component, it is that Border
object's responsibility to create the appropriate margin space (else
this property will effectively be ignored). This causes a redraw of
the component. A PropertyChange event ("margin") is sent to all
listeners.
Parameters: m - the space between the border and the text
for example:
JTextArea txtArea = new JTextArea("Hello world!");
txtArea.setMargin( new Insets(15,15,15,15) );
more on insets and setMargin().
Or another approach would be to add a compound border and then setting the insets on it like this:
JTextArea txtArea = new JTextArea("Hello world!");
Border border = BorderFactory.createLineBorder(Color.RED);
txtArea.setBorder(BorderFactory.createCompoundBorder(
border, BorderFactory.createEmptyBorder(15, 15, 15, 15))
);
see this answer.
I need to make a fixed sized for a GridLayout with 100 buttons located in the center portion of a BorderLayout. On the east portion of the border layout is another Gridlayout that keeps shrinking the center component whenever the text is longer then the size of the current JTextAreas located in the east. The JFrame is not resizable also.
Is there a way to get a fixed size for the center component while allowing the JTextArea to still expand?
"I need to make a fixed sized for a GridLayout with 100 buttons located in the center portion of a BorderLayout".
Sorry, but that's not going to work. BorderLayout doesn't work like that. You can nest JPanel containers with different Layout managers to get your desired effect.
"Gridlayout that keeps shrinking the center component whenever the text is longer then the size of the current JTextAreas located in the east."
You should wrap your text area in a JScrollPane, and setLineWrap(true) and setWrapStyleWord(true) on you text area. The last two will set it, so that the line typed wraps when it is reaching the right edge of the text area. Also If you are setting the size to the text area, don't. Instead, use the following constructor to set its size
JTextArea jta = new JTextArea(20, 50); <--- rows, and character columns
jta.setLineWrap(true);
jta.setWrapStyleWord(true);
JScrollPane scroll = new JScrollPane(jta);
container.add(scroll); <--- make sure you don add jta anywhere else
Without more context to your querstion, these are really the only valid suggestions I can make.
I want to add a JTextArea to an application. Normally that textarea contains large content and both horizontal and vertical ScrollBars appear when running that application. I want to remove horizontal scrolling; I have found that it is possible with
HORIZONTAL_SCROLLBAR_NEVER field but then it does not show the complete contents (it doesn't wrapped horizontally and move content to next row). how to overcome this. I want to stop horizontal scrolling and put contents in next row without scrolling it on a row.
Try this:
yourJTextArea.setLineWrap(true);
yourJTextArea.setWrapStyleWord(true);
You can change the scroll bar policy:
JScrollPane.setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_NEVER);
This will disable the horizontal scroll bar
I have a large JTree (many nodes) displaying in a JScrollPane. I would like to allow the last tree node to display at the top of the JScrollPane. Swing does not seems to allow this by default, showing the (fully expanded) JTree with the last node at the bottom of the JScrollPane. The JScrollBar thumb is touching the bottom of its track.
I am using the JViewport.setViewPosition() method to set the upper-left location of the tree node to the upper-left position in the JViewport, but it refuses to do so if that would cause it to display "whitespace" at the bottom of the JTree.
I have tried manipulating the JViewport by setting the size (.setViewSize to its old value + an extra height of .getExtentSize()), but it still refuses to allow the thumb to move down, even though it is now not at the bottom of its track. I am adjusting the JViewport size in an event handler (.addChangeListener).
I suppose you could override the preferred size of the JTree to be the default preferred size plus the extent size of the viewport minus the height of one row of the JTree.
What if you put your JTree inside a VBox with empty space at the bottom, and then wrap the VBox with the JScrollPane?