Advertisement
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 - |
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 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);
}
}
Advertisement
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.
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement