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?
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]);
I'm attempting to build a custom JScrollPane to be more specific as indicated in the design image below.
I'm having problems with the following tasks:
Customising the vertical scrollbar background, slider and arrows:
I'm having problems in customizing the scrollbar - to be more specific:
Changing the vertical scrollbar background to yellow
Changing the scrollbar slider color to another color
Creating the JScrollPane border to contain radius edges on the border
Changing the scrollbar arrows color to another color
For the border I was successful with my attempts to change it to yellow. Here's the code:
class MyScrollPane extends JScrollPane {
public MyScrollPane(Component view) {
super(view, VERTICAL_SCROLLBAR_ALWAYS, HORIZONTAL_SCROLLBAR_ALWAYS);
this.setLayout(new MyLayout());
setBackground(Color.red);
}
}
.
class MyLayout extends ScrollPaneLayout {
public MyLayout() {
super();
}
public void layoutContainer(Container parent) {
super.layoutContainer(parent);
vsb.setSize(vsb.getWidth() , vsb.getHeight()); // drift
vsb.setBorder(new LineBorder(Color.YELLOW));
}
}
Regarding the background I was unsuccessful - for example the following doesn't work:
vsb.setBackground(Color.red);
Any ideas/thoughts would be very helpful as I've been stuck for several days.
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
}
}
});
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.
I have strange problem in Java. I have JScrollPane
paneScroll=new JScrollPane(nav,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
where nav is JPanel
public class ScrollableNavigationPanelZones extends JPanel {
private ButtonGroup buttonGroup = new ButtonGroup();
private static final long serialVersionUID = -455651438039139284L;
protected JViewport viewport;
private JPanel panel;
private int offset = 100;
public ScrollableNavigationPanelZones() {
super();
setLayout(new BorderLayout());
panel = new JPanel();
panel.setPreferredSize(new Dimension(0,160));
FlowLayout fl = new FlowLayout();
fl.setVgap(5);
fl.setAlignment(FlowLayout.LEFT);
panel.setLayout(fl);
add(panel,BorderLayout.CENTER);
}
}
and it shows scroll on right side but doesn't have that small box which to move ( I have arrows on top and bottom of scroll, but when I press I cannot scroll ). When I change in ScrollableNavigationPanelZones panel.setPreferredSize(new Dimension(0,16000)); it works but shows empty space. Why JScrollPane doesn't read real height of panel ? What is mistake ?
Can anybody help please ?
The scroll box is displayed only if there is something to scroll, i.e. if the preferred height of the panel exceeds the actual height of the scroll pane. Of course, if you set the preferred height to 16000 it will show much of empty space if the panel doesn't fill all the 16000 pixels with controls.
If the panel fits in the scroll pane, the scroll bar is by default hidden. The flag VERTICAL_SCROLLBAR_ALWAYS implies that the scroll bar is visible always, even if not needed. If you set the panel height to 160 pixels and the scroll pane is larger, you see the scroll bar (because it's ALWAYS) but no scroll box (because there is actually nothing to scroll).
It's a bit strange that the up and down buttons on the scroll bar are clickable even if there is nothing to scroll. Usually they should be greyed out in this case. Maybe there is some additional parameter in JScrollPane for this...