Results 1 to 4 of 4
  1. #1
    Gatts79 is offline Member
    Join Date
    Aug 2008
    Posts
    16
    Rep Power
    0

    Default How to send messages / events ??

    Hi All-

    So I've been making various widgets in netbeans for reusability reasons, and one problem I have encountered is that when one widget performs something or finishes executing something, how do I get my other widgets or frames to respond?

    For example, I made a browser window that queries for model files and displays information about these files in a JTable. You can select things by row only and there is a button to load load the file. This widget is part of a parent Frame which has a canvas to display the model. Now everything works just fine, it load straight from the server and all, but I have subsequent frames that hold other information about the model such as animations and transform information that I need to update once the model is loaded.

    The main frame/window contains objects of all these widgets. I thought maybe I could do firepropertychange(string, object, object) and add a propertychangelistener that would respond accordingly. It gets fired off but it is never recieved, I'm guessing because it's not really a propertychange.

    What I am trying to do is that once the model is loaded by widget1, a message or event is sent and then it's parent can call it's reset function to update all the other widgets. I hope that makes sence.

    Thanks In Advance,

    Gatts.

    PS.: Just some more clarification just in case... these widgets are comprised of panels, comboboxes, buttons, radio buttons, checkboxes, etc... I made these seperate widget components for grouping purposes and to keep the code clean with each widget only concerned with what it needs to function.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I've often kept Listener Lists of one sort or another in my widget code, as well as an addXxxListener and removeXxxListener public methods. Then I fire the listeners when a sentinel event occurs. It seems to work well for me. Often the simplest to use is a ChangeListener. Then when they fire all you have to do is package the ChangeEvent with the current class, pass it to all of the stateChanged methods for each ChangeListener in your list. In this situation, the observer is responsible for querying the observable for data after receiving notification of the change event.

  3. #3
    Gatts79 is offline Member
    Join Date
    Aug 2008
    Posts
    16
    Rep Power
    0

    Default

    Hey Fubarable-

    I believe I understand most of what you're saying. I've read this in a couple of posts I found using google. If you could give me an example I would appreciate it. I'm a visual learner and find it easier to understand if I can walk through a basic example of sorts.

    Thanks In Advance

    Gatts

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Here's a simple example of what I mean.
    The ColorSlider produces a JPanel with a JSlider, JLabel and JTextField on it. Sliding the JSlider will cause the text to change to show the number. It also allows for the addition of ChangeListeners that react to a change in the slider's value. This does not yet implement text input into the jtextfield.

    I then have a ColorSliderPanel that holds 3 ColorSlider JPanels, add a ChangeListener to all 3 of the colorsliders and on state change queries the colorsliders for the slider's values. Nothing earth-shaking here.

    Corrections and improvements welcome:

    ColorSlider.java
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Font;
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JComponent;
    import javax.swing.JSlider;
    import javax.swing.JTextField;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;
    
    public class ColorSlider
    {
      private static final int TITLE_SIZE = 32;
      private JPanel mainPanel = new JPanel();
      private JSlider slider = new JSlider(0, 255, 128);
      private JTextField textField = new JTextField(5);
      
      //!! here's my List of changeListeners:
      private List<ChangeListener> changeListeners = new ArrayList<ChangeListener>();
      private String title;
    
      public ColorSlider(String title)
      {
        this.title = title;
        textField.setText(String.valueOf(slider.getValue()));
        
        //!! here I have jslider events call the class method, sliderStateChanged.
        slider.addChangeListener(new ChangeListener()
        {
          public void stateChanged(ChangeEvent e)
          {
            sliderStateChanged(e);
          }
        });
        
        JPanel centerPanel = new JPanel(new BorderLayout());
        centerPanel.add(slider, BorderLayout.CENTER);
        JPanel tfPanel = new JPanel();
        tfPanel.add(textField);
        centerPanel.add(tfPanel, BorderLayout.EAST);
        
        JLabel titleLabel = new JLabel(title);
        titleLabel.setFont(new Font(Font.DIALOG, Font.BOLD, TITLE_SIZE));
        
        mainPanel.setLayout(new BorderLayout());
        mainPanel.add(titleLabel, BorderLayout.NORTH);
        mainPanel.add(centerPanel, BorderLayout.CENTER);
      }
      
      //!! methods for the addition and removal of listeners to the list
      public void addChangeListener(ChangeListener cl)
      {
        changeListeners.add(cl);
      }
      
      public boolean removeChangeListener(ChangeListener cl)
      {
        return changeListeners.remove(cl);
      }
      
      public String getTitle()
      {
        return title;
      }
    
      public JComponent getComponent()
      {
        return mainPanel;
      }
    
      //!! method gets triggered by any changeevents from the jslider
      private void sliderStateChanged(ChangeEvent e)
      {
        textField.setText(String.valueOf(slider.getValue()));
        //!! broadcast these changes to all of the changelisteners in the list
        for (ChangeListener cl : changeListeners)
        {
          cl.stateChanged(e);
        }
      }
    
      //!! the observers can then call this public method when they have been notified of a change event
      public int getValue()
      {
        return slider.getValue();
      }
    }
    ColorSliderPanel.java
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.GridLayout;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JComponent;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;
    
    public class ColorSliderPanel
    {
      private static final Dimension COLOR_PANEL_SIZE = new Dimension(400, 300);
      private JPanel mainPanel = new JPanel();
      private JPanel colorPanel = new JPanel();
      private Color panelColor = new Color(128, 128, 128);
      private ColorSlider[] colorSliders = 
      {
        new ColorSlider("Red"),
        new ColorSlider("Green"),
        new ColorSlider("Blue")
      };
    
      public ColorSliderPanel()
      {
        colorPanel.setPreferredSize(COLOR_PANEL_SIZE);
        JPanel sliderPanel = new JPanel(new GridLayout(0, 1));
        
        for (ColorSlider cSlider : colorSliders)
        {
          sliderPanel.add(cSlider.getComponent());
          //!! add a listener to each ColorSlider object
          cSlider.addChangeListener(new ChangeListener()
          {
            public void stateChanged(ChangeEvent e)
            {
              //!! when notified of a change of state, call t his method
              myStateChanged();
            }
          });
        }
        
        mainPanel.setLayout(new BorderLayout());
        mainPanel.add(colorPanel, BorderLayout.CENTER);
        mainPanel.add(sliderPanel, BorderLayout.EAST);
        myStateChanged();
      }
    
      //!! triggered by a change in any slider's state.  
      //!! query the sliders for there values and set panel color
      private void myStateChanged()
      {
        int red = colorSliders[0].getValue();
        int green = colorSliders[1].getValue();
        int blue = colorSliders[2].getValue();
        panelColor = new Color(red, green, blue);
        colorPanel.setBackground(panelColor);
      }
    
      public JComponent getComponent()
      {
        return mainPanel;
      }
    
      private static void createAndShowUI()
      {
        JFrame frame = new JFrame("ColorSliders");
        frame.getContentPane().add(new ColorSliderPanel().getComponent());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      public static void main(String[] args)
      {
        java.awt.EventQueue.invokeLater(new Runnable()
        {
          public void run()
          {
            createAndShowUI();
          }
        });
      }
    }
    Sorry if I'm overly simplifying your question. I'm not sure at this point.
    Last edited by Fubarable; 11-13-2008 at 06:59 PM.

Similar Threads

  1. getting messages from MessageResources file
    By sireesha in forum Web Frameworks
    Replies: 3
    Last Post: 09-12-2008, 09:17 AM
  2. How to extend Messages Component
    By Jothi in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 05-20-2008, 09:50 AM
  3. how can server send messages every 5 min?
    By dim_ath in forum Networking
    Replies: 7
    Last Post: 01-10-2008, 04:59 PM
  4. Log messages are not appearing in my log file
    By indu_raj in forum Java Servlet
    Replies: 1
    Last Post: 08-09-2007, 12:59 AM
  5. Jsf Error Messages - Retain
    By vaswin in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 08-05-2007, 09:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •