Advertisement
Constructor | Description |
---|---|
public Scrollbar(int orientation, int value int extent, int min, int max) | Creates a Scrollbar with the specified orientation, value, extent, minimum, and maximum, where - |
Methods | Description |
---|---|
public addAdjustmentListener(AdjustmentListener al) | Adds an AdjustmentListener. |
public int getValue() | Gets the Scrollbar's current position value. |
public int setValue(int value) | Sets the Scrollbar's current position value. |
import java.awt.*;
import javax.swing.*;
public class ScrollEx1
{
Button b1, b2, b3, b4, b5;
Frame frame;
Panel panel;
Label label1, label2;
ScrollEx1()
{
frame = new Frame("Scrollbar");
panel = new Panel();
label1 = new Label("Displaying a horizontal and verticial Scrollbar in the Frame",Label.CENTER);
Scrollbar scrollB1 = new Scrollbar(Scrollbar.HORIZONTAL, 10, 40, 0, 100);
Scrollbar scrollB2 = new Scrollbar(Scrollbar.VERTICAL, 10, 60, 0, 100);
frame.add(label1,BorderLayout.NORTH);
frame.add(scrollB2,BorderLayout.EAST);
frame.add(scrollB1,BorderLayout.SOUTH);
frame.setSize(370,270);
frame.setVisible(true);
}
public static void main(String... ar)
{
new ScrollEx1();
}
}
Advertisement
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ScrollEx2 implements AdjustmentListener
{
Frame jf;
Panel jp, jp2;
Label frameLabel1;
ScrollEx2()
{
jf = new JFrame("Scrollbar");
//Creating the first JPanel and adding two JLabels to it
jp = new Panel();
ImageIcon image = new ImageIcon("nature3.png");
//Creating a Label
Label panelLabel1 = new Label("Handling a Scrollbar drag event", Label.CENTER);
jp = new Panel(new BorderLayout());
//Adding the Label to NORTH of the Panel
jp.add(panelLabel1,BorderLayout.NORTH);
//Creating the horizontal Scrollbar
Scrollbar scrollBHorizontal = new Scrollbar(Scrollbar.HORIZONTAL, 10, 40, 0, 100);
//Creating the vertical Scrollbar
Scrollbar scrollBVertical = new Scrollbar(Scrollbar.VERTICAL, 10, 60, 0, 100);
//Adding the horizontal Scrollbar to SOUTH of Panel
jp.add(scrollBHorizontal,BorderLayout.SOUTH);
//Adding the vetical Scrollbar to EAST of JPanel
jp.add(scrollBVertical, BorderLayout.EAST);
//Getting the current position value of horizontal scrollbar
Integer i = scrollBHorizontal.getValue();
//Creating a JLabel and setting its value to the current position value of horizontal scrollbar.
frameLabel1 = new Label(i.toString());
//Adding this JLabel to SOUTH of the JFrame
jf.add(frameLabel1, BorderLayout.SOUTH);
//Adding the first JPanel to the CENTER of JFrame
jf.add(jp,BorderLayout.CENTER);
scrollBHorizontal.addAdjustmentListener(this);
scrollBVertical.addAdjustmentListener(this);
jf.setSize(350,270);
jf.setVisible(true);
}
public void adjustmentValueChanged(AdjustmentEvent e)
{
Integer i =e.getValue();
frameLabel1.setText(i.toString());
}
public static void main(String... ar)
{
new ScrollEx2();
}
}
When you run the code, you are presented a window that shows an image, with two Scrollbar, i.e. a horizontal and a vertical scrollbar beneath the horizontal Scrollbar, you are shown the number that shows the current position value of Scrollbar.
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement