Event on Slide bar In Java

In this section, you will learn about the event handling for a swing component named Slide bar. This section provides a complete solution for the appropriate functionality by providing a program.

Event on Slide bar In Java

In this section, you will learn about the event handling for a swing component named Slide bar. This section provides a complete solution for the appropriate functionality by providing a program.

Event on Slide bar In Java

Event on Slide bar In Java

     

Introduction

In this section, you will learn about the event handling for a swing component named Slide bar. This section provides a complete solution for the appropriate functionality by providing a program.

Program Description:

This program gives you two slide bar and a image inside the frame. One slide bar is arranged as horizontal while another one is as vertical. And the image is stayed in between both slide bar. When you drag the horizontal slide bar then the image will be increased or decreased from left and right according to the value of the slide bar while when you drag the vertical slide bar then the image will be increased or creased from top and bottom according to the value of the slide bar.

Code Description:

In this program, you will see that the complete code has been written under the DynamicIconText class. This class has a main method in which there are two slide bars have been created by using JSlider class and it's constructor. Constructor of JSlider class is has four parameters as follows:

  • First is used for orientation of the slide bar. Here a field JSlider.HORIZONTAL has been used for the arranging the slide bar horizontal on the frame. It's default value is 0. And the value of JSlider.VERTICAL field is 1 that is used for arranging slide bar vertical on the frame.
  • Second argument is also a integer value that is used for the minimum limitation of the slide bar.
  • Third one is used for the maximum limitation of the slide bar component.
  • And last one is for the value of the slide bar. This value is set for slide bar component when the frame is opened.

There are some APIs are explained below these are used in the following program:

getValue(): This method is used for getting value of the slide bar. This method returns a integer value.

Here is the code of this program:  

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

public class DynamicIconText{
  public static void main(String[] args) {
  final JSlider width = new JSlider(JSlider.HORIZONTAL, 5150125);
  final JSlider height = new JSlider(JSlider.VERTICAL, 5150125);

  class DynamicIcon implements Icon {
  public int getIconWidth() {
  return width.getValue();
  }

  public int getIconHeight(){
  return height.getValue();
  }

  public void paintIcon(Component c, Graphics g, int x, int y) {
  g.fill3DRect(x, y, getIconWidth(), getIconHeight()true);
  }
  }
  
  Icon icon = new DynamicIcon();
  final JLabel jl = new JLabel(icon);

  class Updater implements ChangeListener {
  public void stateChanged(ChangeEvent e) {
  jl.repaint();
  }
  };

  Updater ud = new Updater();
  width.addChangeListener(ud);
  height.addChangeListener(ud);
  JFrame fm = new JFrame();
  fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  Container ca = fm.getContentPane();
  ca.setLayout(new BorderLayout());
  ca.add(width, BorderLayout.NORTH);
  ca.add(height, BorderLayout.WEST);
  ca.add(jl, BorderLayout.CENTER);
  fm.setSize(250,250);
  fm.setVisible(true);
  }
}

Output this program:

Download this program.