3

JSlider with "ChangeListener" interface and handle"ChangeEvent" that will change JLabel background color when slider value is change.please help me i couldn't do it. thanks in advance.i couldn't apply JLabel

              package org.kodejava.example.swing;
              import javax.swing.*;
               import javax.swing.event.ChangeEvent;
               import javax.swing.event.ChangeListener;
              import java.awt.*;
           public class JSliderDemo extends JPanel implements ChangeListener {
private JTextField field;

public JSliderDemo() {
    initializeUI();
}

private void initializeUI() {
    setLayout(new BorderLayout());
    setPreferredSize(new Dimension(400, 100));

    //
    // Creates an instance of JSlider with a horizontal
    // orientation. Define 0 as the minimal value and
    // 50 as the maximum value. The initial value is set
    // to 10.
    //
    JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 50, 10);

    slider.setPaintTicks(true);
    slider.setPaintLabels(true);
    slider.setMinorTickSpacing(1);
    slider.setMajorTickSpacing(10);

    slider.addChangeListener(this);

    JLabel label = new JLabel("The Value:");
    field = new JTextField(5);

    JPanel panel = new JPanel();
    panel.setLayout(new FlowLayout());
    panel.add(label);
    panel.add(field);

    add(slider, BorderLayout.NORTH);
    add(panel, BorderLayout.SOUTH);
}

public void stateChanged(ChangeEvent e) {
    JSlider slider = (JSlider) e.getSource();

    //
    // Get the selection value of JSlider
    //
    field.setText(String.valueOf(slider.getValue()));
}

public static void showFrame() {
    JPanel panel = new JSliderDemo();
    panel.setOpaque(true);

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setTitle("Slider Example");
    frame.setContentPane(panel);
    frame.pack();
    frame.setVisible(true);
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            JSliderDemo.showFrame();
        }
    });
}
}
Suma
  • 123
  • 1
  • 4
  • 11
  • i tried this but couldnt add JLabel inside it – Suma Dec 29 '12 at 19:49
  • 1
    Here is an example of JSlider with ChangeListener: http://stackoverflow.com/a/14005539/1133011 you would simply call setBackground(Color c) on JLabel instance within ChangeListener stateChanged(..) method – David Kroukamp Dec 29 '12 at 20:10
  • 1
    Don't forget, JLabel is transparent by default, you should make sure you've called JLabel#setOpaque after you created it. You may also require a call to JLabel#repaint to encourage the repaint manager to update it – MadProgrammer Dec 29 '12 at 20:14
  • @MadProgrammer +1, ah completely forget about `JLabel` being non opaque. BTW `setBackgound(..)` calls `repaint()` itself so no need for the redundancy. – David Kroukamp Dec 29 '12 at 20:29

2 Answers2

6
  1. JLabel is transparent, then have to change its opacity JLabel.setOpaque(true)

  2. read Oracle tutorial How to Use Sliders

mKorbel
  • 109,525
  • 20
  • 134
  • 319
0

JLabel is transparent, cannot change it's background color directly.

you may extend from JLabel and override the paint method

or change the bgcolor of panel. You're putting JLabel on panel and when the slider moves, just change the bgcolor of that panel. Since JLabel is transparent, you will see the change.

As for code try adding something like this inside your statechange method

panel.setBackground(new Color(int,int,int));//ints could be slider.getValue();
Xiaoerge
  • 101
  • 1
  • 1