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
Related
I use a JTextArea for the chat of my simple chat program, and I added a caret so it will autoscroll when text is appended to it. This spawned an issue of the user not being able to scroll when the chat is being used a lot.
I've tried looking through options for the caret, and I looked into the scroll pane options, but there's nothing on not scrolling down when the user is scrolling.
I intend to be able to allow the user to scroll but not immediately get scrolled down because someone sent a message. What would be ideal is it it worked like Discord, where it only scrolls down when the user scrolls all the way down or something like that.
What would be ideal is it it worked like Discord, where it only scrolls down when the user scrolls all the way down or something like that.
Check out Smart Scrolling. It adds an AdjustmentListener to the scroll bar to control scrolling:
When the scroll bar is at the bottom it will continue to automatically scroll.
When it is not at the bottom it won't scroll. The user would need to scroll to the bottom to reactivate automatic scrolling.
My Recommendation is similar to Camickr's. Add a changelistener to the scrollbar and enable/disable caret movement based on whether or not the user is scrolled to the bottom. If the scrollbar is at the maximum, enable caret movement. If it is not, then keep it disabled.
YourJScrollPane.getVerticalScrollBar().getModel().addChangeListener()
{
//Override stateChanged(ChangeEvent e) With Caret Movement Switch
}
Detect and compare current scrollbar position by calling the extent, value, and maximum extent from the Model (Which is a BoundedRangeModel).
https://docs.oracle.com/javase/7/docs/api/javax/swing/BoundedRangeModel.html
/*Where to find the needed scrollbar position values*/
YourJScrollPane.getVerticalScrollBar().getModel().getExtent()
YourJScrollPane.getVerticalScrollBar().getModel().getValue()
YourJScrollPane.getVerticalScrollBar().getModel().getMaximum() //Bottom Position
/*Pseudocode*/
if (Value + Extent == Maximum)
{
/*Enable Caret Movement - User is at bottom of page*/
}
else
{
/*Disable Caret Movement - User is not at bottom of page*/
}
Comment Below with any improvements or recommendations.
I used the following code to auto scroll the JTextArea while the program is running.
private javax.swing.JTextArea outLog;
...
DefaultCaret caret = (DefaultCaret)outLog.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
While, I want to auto scroll the lines (vertical scroll), it actually auto scrolls the columns (horizontal scroll) as well. I don't want that because by updating the columns, it stick to the last columns and I am not able to see the subsequent lines which has less columns.
How to fix that?
Check out Smart Scrolling
It managers the scrolling without using the caret policy. So you are in better control of the scrolling functionality.
If uses an AdjustmentListener on the vertical scrollbar to determine when scrolling should be done.
I'm creating a java chat application using jFrame. I'm using a JScrollPane to scroll the text area. All the new messages are added at the bottom, but the scroll bar starts at the top. How do I make it start at the bottom?
You can force it to scroll to the bottom after adding a message by scrolling its vertical scrollbar directly, e.g.:
JScrollBar vScrollBar = myScrollPane.getVerticalScrollBar();
vScrollBar.setValue(vScrollBar.getMaximum());
In my application, I have two swing components on top of each other, that look something like this picture. The problem is that the orange component needs a vertical scrollbar, but I want the right edges of the components to stay exactly aligned (and the width can vary as the user makes the app wider or narrower). If I use something like a grid layout, the scrollbar takes up space and then the scrollbar lines up with the right edge of the red component.
I'm thinking I might need to use a scrollbar component and add that separately and use it to control the orange component. Is there a way to attach a scrollbar to another component like that? I would think it would be difficult without using a scrollpane.
I'm also open to any suggestions on how else to approach this.
It shouldn't be too hard to implement the approach you suggested. Wrap the orange component in a JScrollPane, but configure the scroll pane to hide both scrollbars. Then, set the scroll model for the vertical scroll bar in the scroll pane to the scrolling model from the standalone scrollbar. Even though the scroll pane scroll bar is hidden, it will still scroll if the models are linked. See my answer in this question for some code - it's a different application but similar principle.
Alternatively, you could use a JScrollPane with a visible vertical scrollbar, and add a spacing component next to the red component to keep it aligned. I'm thinking you could use a GridBagLayout with two columns. The first row holds the red component and the spacer, and the second row holds the scroll pane with the orange component, which spans both columns. Then, you just have to get the width of the scroll bar component from the JScrollPane and set the preferred width of the spacer to the same value. A drawback with this strategy would be that it could be difficult to keep the spacer size updated if the scrollbar width changes (due to a UI change, for example).
I have a TextArea in LWUIT that I am having an issue manipulating. I have the following two issues:
Even though I call setIsScrollVisible(true), I do not seem to have a
scrollbar and cannot scroll when the output goes below the visible
area of the container.
How do I tell the TextArea to automatically scroll to the bottom
programmatically?
My code for initializing the TextArea looks like this:
myTextArea = new TextArea(20, Display.getInstance().getDisplayWidth());
myTextArea.setEditable(false);
myTextArea.setEnabled(true);
myTextArea.setIsScrollVisible(true);
myTextArea.setGrowByContent(false);
System.out.println(myTextArea.isScrollableY());
isScrollableY() returns true. Any ideas? Is there something I am missing? There doesn't seem to be anything visible in the API that lets me explicitly enable or disable scrolling. Thanks in advance for any assistance on this.
The width of the text area is in columns NOT pixels as you have in your code.
Setting the scroll to visible won't cause it to appear since LWUIT scrollbars are always "as needed" which means a scrollbar will only appear when necessary, setting this value to false would just hide the scrollbar regardless of necessity.
To have the text area grab the entire width just place it within a box layout Y container/form and the layout manager will stretch it on the X axis.
You can use scrollRectToVisible() to scroll the text area to the bottom or alternatively you can derive text area and use setScrollY(int) with the appropriate value (see the source code of text area for how this is used to scroll the text area.
Try a simple textArea.setFocusable(false). This worked for me.