2

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.

Community
  • 1
  • 1
Eric
  • 373
  • 1
  • 3
  • 14
  • How do you define "*Bound Property*"? – PM 77-1 Apr 10 '14 at 00:43
  • @PM77-1 From the Java Doc found [here](http://docs.oracle.com/javase/tutorial/javabeans/writing/properties.html), under 'bound properties'. – Eric Apr 10 '14 at 00:45
  • And do you actually have a `JavaBean`? – PM 77-1 Apr 10 '14 at 00:45
  • @PM77-1 Yes, though in the SSCCE I didn't double check on that, so the posted code currently no. I'll fix that now though. – Eric Apr 10 '14 at 00:48
  • @PM77-1 My SSCCE is a bean now (unless I missed a method). I created the `PropertyChangeEvent` in there too, unsure of how to continue though. – Eric Apr 10 '14 at 01:01

1 Answers1

3

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:

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Thank you for your answer! I apologize I had to take a break and only just got back. I shall implement your advice. And to clarify my code looks a lot better, I just hastily scrapped together that SSCCE to just get the essence of it. – Eric Apr 10 '14 at 03:24