Java JScrollBar Example
Java JScrollBar is used to add vertical or horizontal scroll bars.ScrollBar inherits methods from JComponent class.
How to Declare Java JScrollBar
[class JScrollBar extends JComponent implements Adjustable, Accessible]
Java JScrollBar used constructors
- JScrollBar()-creates vertical scrollbar with initial values.
- JScrollBar(int orientation)-creates scrollbar with a certain orientation and initial values.
- JScrollBar(int orientation,int v,int extent,int max,int min)-creates JScrollBar with specified values.
Java JScrollBar Example
[
import javax.swing.JFrame; import javax.swing.JScrollBar; public class JavaJScrollBarExample extends JFrame { JavaJScrollBarExample () { super("JavaJScrollBarExample "); JScrollBar scroll=new JScrollBar(); scroll.setBounds(50,50, 50,100); add(scroll); setSize(400,400); setLayout(null); setLocationRelativeTo(null); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); } public static void main(String args[]) { new JavaJScrollBarExample (); } }
]
Code Explanation
[ super("JavaJScrollBarExample ");] -this code calls the JFrame constructor which has been extended in this class.
[ JScrollBar scroll=new JScrollBar();]-Creates JScrollbar object.
[setDefaultCloseOperation(EXIT_ON_CLOSE);]-exits application when the close button is clicked.
The user scrolls the JScrollBar action is picked which invokes [ JOptionPane.showMessageDialog()] method.JOptionPane pop ups showing value of vertical scrollbar position.
Java JScrollBar Example with Adjustment Listener
[
import javax.swing.JFrame; import javax.swing.JScrollBar; import java.awt.event.AdjustmentEvent; import java.awt.event.AdjustmentListener; import javax.swing.JOptionPane; public class JavaJScrollBarExample extends JFrame implements AdjustmentListener { JavaJScrollBarExample () { super("JavaJScrollBarExample "); JScrollBar scroll=new JScrollBar(); scroll.setBounds(50,50, 50,100); add(scroll); scroll.addAdjustmentListener(this); setSize(400,400); setLayout(null); setLocationRelativeTo(null); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); } public void adjustmentValueChanged(AdjustmentEvent evt) { JOptionPane.showMessageDialog(null, "Vertical scrollbar "
+evt.getValue(),"JScrollBar Example", JOptionPane.INFORMATION_MESSAGE); } public static void main(String args[]) { new JavaJScrollBarExample (); } }
]
Output
Code Explanation
Above class extends JFrame and implement AdjustmentListener interface [public class JavaJScrollBarExample extends JFrame
implements AdjustmentListener ]
If a non abstract class implements a certain interface,the class is required to override all interface abstract methods.For example AdjustmentListener abstract method is [public void adjustmentValueChanged(AdjustmentEvent evt)
Above class extends JFrame and implement AdjustmentListener interface [public class JavaJScrollBarExample extends JFrame
implements AdjustmentListener ]
If a non abstract class implements a certain interface,the class is required to override all interface abstract methods.For example AdjustmentListener abstract method is [public void adjustmentValueChanged(AdjustmentEvent evt)
{ JOptionPane.showMessageDialog(null, "Vertical scrollbar "
+evt.getValue(),"JScrollBar Example", JOptionPane.INFORMATION_MESSAGE); }]
The user scrolls the JScrollBar action is picked which invokes [ JOptionPane.showMessageDialog()] method.JOptionPane pop ups showing value of vertical scrollbar position.
Other methods used in this program
[setBounds(50,50, 50,100);]-positions java.swing components in JFrame or JPanel.
[setDefaultCloseOperation(EXIT_ON_CLOSE);]-exits application when close button is clicked.
[ setLocationRelativeTo(null);]-positions JFrame at the center of the screen.