Hi,
Please review below example of Scrollbar class:
Code:
import java.awt.BorderLayout;
java.awt.Scrollbar;
import java.awt.event.AdjustmentListener;
public class ScrollbarDM extends
{ JLabel label;
public SwingScrollBarExample() {
super(true);
label = new JLabel();
setLayout(new BorderLayout());
JScrollBar hbar1 = new JScrollBar(JScrollBar.HORIZONTAL, 30, 20, 0, 300);
hbar1.setUnitIncrement(2);
hbar1.setBlockIncrement(1);
hbar1.addAdjustmentListener(new MyAdjustmentListener());
add(hbar1, BorderLayout.SOUTH);
}
class MyAdjustmentListener implements AdjustmentListener {
public void adjustmentValueChanged(AdjustmentEvent e) {
label.setText(" New Value is " + e.getValue() + " ");
repaint();
} }
public static void main(String sgt[]) {
JFrame frameC = new JFrame("Scroll Bar Example");
frameC.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frameC.setSize(300, 200);
frameC.setVisible(true);
}
}
Bookmarks