Text area can't scroll properly with caret - java

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.

Related

auto scroll jtextarea only for lines

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.

Scrolling a scrollbar to the bottom when its at the bottom

I am trying to make a chatbox application, im trying to code the side bar so that it will stay at the bottom but can also scroll up to view history. Is there any way i can do this?
pane.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {
public void adjustmentValueChanged(AdjustmentEvent e) {
e.getAdjustable().setValue(e.getAdjustable().getMaximum());
}
});
, im trying to code the side bar so that it will stay at the bottom but can also scroll up to view history
Check out the Smart Scrolling. When the scrollbar is at the bottom it will stay there as next text is added. If you scroll somewhere else, it will stay there (even when new text is added) until you move the scrollbar back to the bottom.

Table control with sliding row headers in JavaFX

The album view in iTunes has a slick effect where the album title and cover art stay in view at all times. If you slide down the screen they stay pinned to the top of the screen until they bump into the next album, then they slide away.
Notice how the top album is still fully visible even though the user has scrolled down a ways.
What is this control or effect called? I'm coming up with blanks trying to Google for it.
How can I do this in JavaFX? I want to mimic this in my Java-based GUI. Can TableView do this, or maybe some third-party control?
The easiest way to do this is with a ScrollPane. Inside your ScrollPane you define your rows and their layouts (probably each row is an HBox containing an ImageView and a TableView which is set to the height of the ImageView). Then, the TableViews inside your ScrollPane need to let the ScrollPane override their scrolling - that is, their onScroll bubbles up to the ScrollPane.
Then you override the onScroll behavior for your ScrollPane. The algorithm for the scrolling could go like this:
There are two modes.
1) Scrolling IN an album scrolls the TableView in that row. If the scrolling goes beyond the boundaries of the TableView's scrollHeight (the range between 0 and scrollHeight), then the mode switches to scrolling TO an album.
2) Scrolling TO an album scrolls the ScrollPane an amount up to the height of the current row. Scrolling an amount greater than the current row's height moves to the next album and switches the mode back to Scrolling IN that album.
3) Edge Cases: Scrolling within the ScrollPane beyond the boundaries of the ScrollPane's scrollHeight (the range between 0 and scrollHeight) immediately moves to the next album and switches the mode back to Scrolling IN that album.
I'd give a code example, but I've never actually seen anybody try to do this. I just know you CAN do it.
I would recommend you to take a look to the SpreadsheetView in ControlsFX
The SpreadsheetView allow you to fix at the top of the screen any number of line. So you would have the first part of your behavior.
Regarding the fact of bumping into another, it would be more difficult but not impossible with the SpreadsheetView.
Anyway, if you want to implement that behavior in the TableView, you will find very useful tricks in the SpreadsheetView code.
This is how a section title acts as default in a UITableView.
https://developer.apple.com/Library/ios/documentation/UIKit/Reference/UITableView_Class/index.html
This should help you on how to create and use UITableView

JScrollbar scroll per 50 px

I'm working on a program that includes a scrollbar.
In the program I got a scrollable field of 500px and I want the scrollbar to scroll per 50px.
So I get 10 pages.
I dont want to be able to show 50% of a page I only want to be able to show 1 page at a time.
So how do I change the scroll dimensions?
Or is there a better way to accomplish this?
Greets,
And thanks in advance
Bram
You can make the scrollable field implement the Scrollable interface, which declares two methods the scroll field will use to decide how far it scrolls, getScrollableUnitIncrement (scroll-wheel, clicking the scroll arrow) and getScrollableBlockIncrement (clicking the scrollbar's track).
If you're really only ever going to want it to be 50, you can use
jScrollPane.getVerticalScrollBar().setUnitIncrement(50);
jScrollPane.getVerticalScrollBar().setBlockIncrement(50);
However, if the user then drags the scrollbar until half a page is shown (I don't think this behavior can be easily changed), scrolling will still jump 50 pixels and show half of the next page... If you implement Scrollable, you can base the amount scrolled on what is currently visible.
Try this with you JScrollPane
scrollPane.getVerticalScrollBar().setUnitIncrement(50);
This helps when you scroll with JScrollPane buttons up/down but you be able show a half of page by moving knob with mouse.
As alternative I can propose to watch knob position with AdjustmentListener and shift scrollPane value manually.
scrollPane.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener()
{
public void adjustmentValueChanged(AdjustmentEvent e)
{
scrollPane.getVerticalScrollBar().setValue(e.getValue() / 50 * 50);
}
});
But I think it isn't good idea. May be somebody will propose better way.

stop horizontal scrolling in JTextArea

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

Categories

Resources