Question

Explanation

I've created an example of my current project, but in SSCCE form.

CODE WAS UPDATED:

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.WindowConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class Test2 extends JFrame{
    private Panels panel1, panel2;
    
    public Test2(){
        init();
    }
    
    private void init(){
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLayout(new GridLayout(0,1));
        panel1 = new Panels("Test1");
        panel2 = new Panels("Test2");
        add(panel1.getPanel());
        add(panel2.getPanel());
        pack();
        setVisible(true);
        
    }
    public class Panels {
        private JSlider slider;
        private JPanel panel;
        private JLabel label;
        private ChangeListener changeListener;
        private PropertyChangeSupport changeSup;


        public Panels(String text){
            slider = new JSlider();
            slider.setMajorTickSpacing(50);
            slider.setMaximum(255);
            slider.setMinorTickSpacing(10);
            slider.setPaintLabels(true);
            slider.setPaintTicks(true);
            setSliderValue(0);
            label = new JLabel();
            label.setText("0");


            changeListener = new ChangeListener(){

                @Override
                public void stateChanged(ChangeEvent e) {
                    setLabelText(String.valueOf(getSliderValue()));
                }           
            }; 
            slider.addChangeListener(changeListener);
            
            
            panel = new JPanel();
            panel.add(label);
            panel.add(slider);
        }

        public final int getSliderValue() {
            return slider.getValue();
        }

        public final void setSliderValue(int value) {
            slider.setValue(0);
        }

        public final String getLabelText() {
            return label.getText();
        }

        public final void setLabelText(String text) {
            String oldLabelText = getLabelText();
            label.setText(text);
            changeSup.firePropertyChange("value", oldLabelText, getLabelText());
        }
        
        public void
        addPropertyChangeListener(PropertyChangeListener listener){
            changeSup.addPropertyChangeListener("value", listener);
        }
        
        public void
        removePropertyChangeListener(PropertyChangeListener listener){
            changeSup.removePropertyChangeListener("value", listener);
        }
               
        public final JPanel getPanel(){
            return panel;
        }
        
        
        
    }  
    
    public static void main(String[] args) {
        try {
            for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } 
        catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
            Logger.getLogger(Test2.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Test2();
            }
        });
    }
   
    
    
}

Note again this is an SSCCE, extremely barebones. The idea is there though.

Basically the above code creates a JFrame with 2 JSliders that update a JLabel with a Listener. What I'm trying to get working is some way of updating another component based on the two values from the JSliders.


What Should I Do

I'm thinking I should be creating a Bound Property, but am unfamiliar in that area of Java.

If this isn't a good way to go about it, I'm open to suggestions.

Was it helpful?

Solution

You never initialize or use your PropertyChangeSupport object. You should

  • Change your PropertyChangeSupport variable to a SwingPropertyChangeSupport variable since this is Swing that you're working with, and you thus want all notifications done on the EDT, the Swing event thread.
  • Initialize PropertyChangeSupport object and pass into its constructor this, the current object.
  • Give your class both an addPropertyChangeListener(PropertyChangeListener listener) and a removePropertyChangeListener(PropertyChangeListener listener) method, so that other classes can add listeners and listen for changes.
  • In the above methods, add or remove the listener to your support object.
  • You should fire the support object in one or more setXXX(...) methods to notify the listeners of the changes, passing in the appropriate property name and old and new values.
  • Note that if your class extends a Swing component, it already has PropertyChange support. Check the API, including the addPropertyChangeListener and removePropertyChangeListener methods.
  • Having said that, you will almost never want to extend JFrame.
  • You should read the tutorials on this for they are to be found with just a little searching (the link above was the first hit on a PropertyChangeListener tutorial Google search).

For example, please have a look at the code here:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top