0 Replies Latest reply: Oct 16, 2006 7:58 AM by 843804 RSS

    NewBie help needed on Listener and Source Beans

    843804
      Hi all

      I am working on an assignment for college, trying to learn Java etc,

      Basically I need one object to react to events in another

      Code is as follows

      // Extra Panel


      package myPackage1;

      import java.awt.GridLayout;
      import java.beans.PropertyChangeEvent;

      import javax.swing.JPanel;

      public class ExtraPanel extends JPanel implements
                                                             java.beans.PropertyChangeListener,
                                                             java.io.Serializable
      {
           private static final long serialVersionUID = 1L;
           
           public JPanel thisPanel;
           
           public JPanel ExtraPanelMethod()
           {
                thisPanel = new JPanel();
                
                GridLayout grid = new GridLayout();
                
                grid.setColumns(1);
                grid.setHgap(10);
                grid.setRows(6);
                grid.setVgap(5);
                thisPanel.setLayout(grid);
                
                return thisPanel;
           }

           public void propertyChange(PropertyChangeEvent evt)
           {
           }
           
      }

      // The Slider Class


      package myPackage1;

      import java.awt.Color;
      import java.awt.Component;
      import java.awt.Font;
      import java.awt.GridLayout;

      import javax.swing.BorderFactory;
      import javax.swing.JFormattedTextField;
      import javax.swing.JLabel;
      import javax.swing.JPanel;
      import javax.swing.JSlider;
      import javax.swing.event.ChangeEvent;
      import javax.swing.event.ChangeListener;
      import javax.swing.text.NumberFormatter;


      public class TheSliderClass extends JPanel implements ChangeListener
                                                                            
      {

           private static final long serialVersionUID = 1L;
           
           //Add a formatted text field to supplement the slider.
           public JFormattedTextField textField,textField2;
           
           JFormattedTextField newValue = null;
           JFormattedTextField oldValue = null;
           
           public String publicSliderName = "";
           
           public int fps;

           //And here's the slider.
           public JSlider sliderParameterValue;
           JPanel labelAndTextField,allTogether;
           int TractiveEffort = 0;
           
           
           public JPanel TheSliderClassMethod(String sliderName, int minimumValue, int maximumValue)
           {
                
                
                JPanel allTogether = new JPanel();
                
                Font font = new Font("palatino linotype regular", Font.BOLD, 12);
                
                int initialValue = ((minimumValue+maximumValue)/2);
                int tickMarkValue = (maximumValue-minimumValue);
                publicSliderName = sliderName;
                
                //Create the label.
                JLabel sliderLabel = new JLabel(sliderName,JLabel.CENTER);
                sliderLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
                sliderLabel.setFont(font);
                sliderLabel.setForeground(Color.BLUE);
                
                //Create the formatted text field and its formatter.
                java.text.NumberFormat numberFormat =
                     java.text.NumberFormat.getIntegerInstance();
                NumberFormatter formatter = new NumberFormatter(numberFormat);
                formatter.setMinimum(new Integer(minimumValue));
                formatter.setMaximum(new Integer(maximumValue));
                textField = new JFormattedTextField(formatter);
                textField.setValue(new Integer(initialValue));
                textField.setColumns(3); //get some space
                textField.setEditable(false);
                textField.setForeground(Color.red);
                
                textField2 = new JFormattedTextField(formatter);
                textField2.setValue(new Integer(initialValue));
                textField2.setColumns(3); //get some space
                textField2.setEditable(false);
                textField2.setForeground(Color.red);

                
                //Create the slider.
                sliderParameterValue = new JSlider(JSlider.HORIZONTAL,
                minimumValue, maximumValue, initialValue);
                sliderParameterValue.addChangeListener(this);

                //Turn on labels at major tick marks.
                
                
                sliderParameterValue.setMajorTickSpacing(tickMarkValue);
                sliderParameterValue.setMinorTickSpacing(10);
                
                sliderParameterValue.setPaintTicks(true);
                sliderParameterValue.setPaintLabels(true);
                
                sliderParameterValue.setBorder(
                BorderFactory.createEmptyBorder(0,0,0,0));
                                                        sliderParameterValue.setBackground(Color.cyan);
                                                        
                
                //Create a subpanel for the label and text field.
                JPanel labelAndTextField = new JPanel(); //use FlowLayout
                labelAndTextField.setBackground(Color.cyan);
                labelAndTextField.add(sliderLabel);
                labelAndTextField.add(textField);

                //Put everything together.

                GridLayout gridThis = new GridLayout();
                gridThis.setColumns(1);
                gridThis.setRows(2);
                allTogether.setLayout(gridThis);

                allTogether.add(labelAndTextField);
                allTogether.add(sliderParameterValue);
                allTogether.setBorder(BorderFactory.createBevelBorder(1,Color.red,
                                                                                      Color.red));

                return allTogether;
           }

           /** Listen to the slider. */
           public void stateChanged(ChangeEvent e)
           {
                JSlider source = (JSlider)e.getSource();
                fps = (int)source.getValue();
                textField.setText(String.valueOf(fps));
                textField2.setText(String.valueOf(fps));
           }
      }

      How do I get Extra Panel to React to the slider in the Slider Class being moved?

      Thanks all