Advertisement



< Prev
Next >



Scrollbar class





Scrollbar class is used to create a horizontal and vertical Scrollbar. A Scrollbar can be added to a top-level container like Frame or a component like Panel. Scrollbar class is another component in the AWT package which extends the Component class.




Constructors of Scrollbar


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 -

orientation - This parameter specifies the Scrollbar to be a horizontal or a vertical Scrollbar.
value - This parameter specifies the starting position of the knob of Scrollbar on the track of a Scrollbar.
extent - This parameter specified the width of the knob of Scrollbar.
min - This parameter specifies the minimum width of the track on which Scrollbar moves.
max - This parameter specifies the maximum width of the track.




A few methods of Scrollbar class


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.





An example of Scrollbar


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();
}


}

When you run the code, you are presented a window that shows a Frame with a horizontal and a vertical scroll bar.

Figure 1



Advertisement




Handling the Scrollbar dragging events with AdjustmentListener.


In the upcoming code, we are going to handle scrollbar events by implementing the AdjustmentListener interface.

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.

Figure 2

When you drag any of the scrollbar(horizontal or vertical), you may see the change in the current position value of the scrollbar which you are dragging.



Please share this article -





< Prev
Next >
< List
Swing >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement

Notifications



Please check our latest addition

PYTHON


Advertisement