I've been trying to create a a scroll pane that automatically keeps scrolling down as the user adds more lines of text to the window. I implemented this:
pane.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {
public void adjustmentValueChanged(AdjustmentEvent e) {
e.getAdjustable().setValue(e.getAdjustable().getMaximum());
}
});
"pane" is a JScrollPane that is holding a JList.
Unfortunately, the code above sticks the scroll bar to the bottom of the pane (which is what I wanted) but then I am unable to move the scroll bar from its position (so now it's stuck at the bottom). Is there a simple way to have the scroll bar stick to the bottom, but still be able to be scrolled elsewhere?
Thanks!
When you add an item to the JList you can use the method:
list.ensureIndexIsVisible(...)
where the index is the index of the last item in the list.
Related
I have a jFrame along with jScrollPane in this jScrollPane there are few other component such as jTextField jTextArea jTable etc.. (this form I have designed with NetBeans form designer)
jScrollPane (the Parent Component) has vertical scroll bar and it scroll with the mouse wheel. But when mouse move on a jTextArea or a jTable (they have there own scroll bars) Mouse Wheel focus goes to them and scrolling them instead of scrolling jScrollPane. I want to keep scroll focus on jScrollPane without going it to any other component in it.
found a solution
scrollPane = new JScrollPane() {
#Override
protected void processMouseWheelEvent(MouseWheelEvent e) {
if (!isWheelScrollingEnabled()) {
if (getParent() != null)
getParent().dispatchEvent(
SwingUtilities.convertMouseEvent(this, e, getParent()));
return;
}
super.processMouseWheelEvent(e);
}
};
scrollPane.setWheelScrollingEnabled(false);
I did this in my panel and it worked...
jScrollPane1.removeMouseWheelListener(jScrollPane1.getMouseWheelListeners()[0]);
In my JavaFX project I was using 2 TextFlows to display some text. I used vvalueProperty of ScrollPanes which are holding the TextFlows to scroll both TextFlow at same time
scrolPane1.vvalueProperty().bindBidirectional(scrolPane2.vvalueProperty());
But since TextFlow is only support in Java 8, Im trying to replace them with ListView.
How can I scroll 2 ListViews at same time? Since ListView contains a inner ScrollPane my approach that worked with TextFlow doesn't work here.
Simply I want to scroll 2 ListViews at same time.
Try something like
Platform.runLater(new Runnable() {
#Override
public void run() {
Node n = listView1.lookup(".scroll-bar");
if (n instanceof ScrollBar) {
final ScrollBar bar = (ScrollBar) n;
if (bar.getOrientation().equals(Orientation.VERTICAL)) {
// get the second scrollbar of another listview and bind values of them
}
}
}
});
I have a JScrollPane (myListScroll) that is added to a JPanel (which in turn is added to another JPanel before being added to a JFrame). This JScrollPane (myListScroll) consists of a list of strings. I want to be able to handle mouse events when clicking on the different items in this list.
In the code below I want to try if something happens if I click i the JScrollpane but nothing happens. What is wrong? Why is "test" not written?
JScrollPane myListScrol = new JScrollPane(myList);
myListScrol.getViewport().addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent mouseEvent) {
System.out.println("test");
}
});
I should have added the listener to myList and not to myListScrol.
Is it possible to specify the step size of a JScrollPane when dragging it?
I'm aware of setUnitIncrement and setBlockIncrement, but these do not work when dragging the scrollbar.
Edit: AdjustmentListener
final AdjustmentListener myAdjustmentListener = new AdjustmentListener() {
#Override
public void adjustmentValueChanged(AdjustmentEvent e) {
if (e.getAdjustmentType() == AdjustmentEvent.TRACK){
e.getAdjustable().setUnitIncrement(500);
e.getAdjustable().setBlockIncrement(500);
}
}
};
Setting setUnitIncrement and setBlockIncrement values only changes the scroll speed when scrolling with a mouse wheel, the arrow keys, and the scroll bar arrows. These values do not change the scroll speed when the scroll bars are dragged.
However the AdjustmentListener can be added to a scrollbar to listen to the scrollbar movements.
An AdjustmentListener should do the magic for you. Did you look at this:
How to make JScrollPane scroll 1 line per mouse wheel step?
I've been unable to find anything related to my question on the Internet.
I have a JScrollPane which adds a JPanel, and the JPanel gets populated with JLabels.
Currently, the user scrolls to the bottom of the JScrollPane, and then clicks the 'Next' button.
What I would like to do is to detect when the user is at/near the bottom of the JScrollPane, so that I can then trigger the 'Next' behaviour without any input from the user.
Is this even possible?
Thanks in advance everyone.
You could use an AdjustmentListener registered with your the vertical scrollbar in your JScrollPane and take action based on the event value.
Here is an example.
JScrollBar uses a BoundedRangeModel, and this will accept a ChangeListener -- so you can listen for changes to the scrollbar and respond accordingly.
Here is an example showing how to trigger an event when the scrollpane is near the bottom (90% of the height):
final JScrollPane scrollPane = new JScrollPane(panel);
scrollPane.getViewport().addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent event) {
JScrollBar scrollbar = scrollPane.getVerticalScrollBar();
if (scrollbar.getValue() + scrollbar.getVisibleAmount() > scrollbar.getMaximum() * 0.9) {
// trigger the 'Next' behaviour here
}
}
});
Alternatively, the listener can also be added to the model:
final BoundedRangeModel model = scrollPane.getVerticalScrollBar().getModel();
model.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent event) {
if (model.getValue() + model.getExtent() > model.getMaximum() * 0.9) {
// trigger the 'Next' behaviour here
}
}
});