< Prev
Next >



JScrollBar class




JScrollBar is used to create a horizontal and a vertical scrollbar. A JScrollBar can be added to a top-level container like JFrame or a component like JPanel. JScrollBar is another lightweight component which extends JComponent class.




Difference between JScrollBar and JScrollPane?


By creating an object of JScrollBar, we are only provided with a non-working scrollbar, which doesn't scroll across the display by default. In order to scroll the display using JScrollBar, a lot more work is required, whereas, JScrollPane provides a working scrollbar that scrolls across the display by default. Hence, JScrollPane is preferred over JScrollBar.




Constructors of JScrollBar


Constructor Description
public JScrollBar(int orientation, int value int extent, int min, int max) Creates a JScrollBar 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 - Third 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 JScrollBar 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 JScrollBar


import javax.swing.*;
import java.awt.*;

public class Scroll2
{
public static void main(String... ar)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
new A();
}
});

}//Closing the main method
}//Closing the class Combo


class A //implements ActionListener
{
Object [] index;
JFrame jf;
JPanel jp; 
JLabel label1, label2;

A()
{

 
jf = new JFrame("JScrollBar");
jp = new JPanel();

label1 = new JLabel("Displaying a picture ",JLabel.CENTER);
	


ImageIcon image = new ImageIcon("nature3.png");
JLabel label = new JLabel(image, JLabel.CENTER);
jp = new JPanel(new BorderLayout());
jp.add( label, BorderLayout.CENTER );



JScrollBar scrollB1 = new JScrollBar(JScrollBar.HORIZONTAL, 10, 40, 0, 100);
JScrollBar scrollB2 = new JScrollBar(JScrollBar.VERTICAL, 10, 60, 0, 100);



jf.add(label1,BorderLayout.NORTH);
jf.add(jp,BorderLayout.CENTER);
jf.add(scrollB2,BorderLayout.EAST);
jf.add(scrollB1,BorderLayout.SOUTH);


jf.setSize(350,270);
jf.setVisible(true);
}

}

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

Figure 1





Handling JScrollBar dragging events with AdjustmentListener.


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Scroll3
{
public static void main(String... ar)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
new A();
}
});

}//Closing the main method
}//Closing the class Combo


class A implements AdjustmentListener
{
Object [] index;
JFrame jf;
JPanel jp, jp2; 
JLabel frameLabel1;

A()
{

 
jf = new JFrame("JScrollBar");

//Creating the first JPanel and adding two JLabels to it
jp = new JPanel();


	
ImageIcon image = new ImageIcon("nature3.png");

//Creating the first JLabel 
JLabel panelLabel1 = new JLabel("Displaying a picture", JLabel.CENTER);

//Creating the second JLabel 
JLabel panelLabel2 = new JLabel(image, JLabel.CENTER);
jp = new JPanel(new BorderLayout());

//Adding the first JLabel to NORTH of the JPanel
jp.add(panelLabel1,BorderLayout.NORTH);

//Adding the second JLabel to CENTER of the JPanel
jp.add(panelLabel2,BorderLayout.CENTER);

//Creating the horizontal JScrollBar
JScrollBar scrollBHorizontal = new JScrollBar(JScrollBar.HORIZONTAL, 10, 40, 0, 100);

//Creating the vertical JScrollBar
JScrollBar scrollBVertical = new JScrollBar(JScrollBar.VERTICAL, 10, 60, 0, 100); 

//Adding the horizontal JScrollBar to SOUTH of JPanel
jp.add(scrollBHorizontal,BorderLayout.SOUTH);

//Adding the vetical JScrollBar 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 JLabel(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());
}

}

When you run the code, you are presented a window that shows an image, with two JScrollBar, i.e. a horizontal and a vertical scrollbar beneath the horizontal JScrollBar, you are shown the number that shows the current position value of JScrollBar.

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 -

Facebook Google Pinterest Reddit Tumblr Twitter





< Prev
Next >
< JScrollPane
JFileChooser >
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
-
C++ and Python

Ad2