Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #4001

Re: Observer for Swing JP

From "Mark Space" <mark.space@THRWHITE.remove-dii-this>
Subject Re: Observer for Swing JP
Message-ID <fiqpk.16914$LG4.12530@nlpi065.nbdc.sbc.com> (permalink)
Newsgroups comp.lang.java.gui
References <RMapk.184982$gc5.84518@pd7urf2no>
Date 2011-04-27 15:48 +0000
Organization TDS.net

Show all headers | View raw


  To: comp.lang.java.gui
thufir wrote:
> Follow up to comp.lang.java.gui
> 
> Given that SourceTableBean extends JPanel, how can SourceTableBean send 
> messages to Main?  Or, to turn that question around, how can Main listen 
> for events which SourceTableBean sends?  How does the JFrame observe the 
> JPanel?

Well, one way to do this would be to add the bean you made to the GUI 
builder palette.

<http://java.sun.com/docs/books/tutorial/javabeans/nb/index.html>


Another would be just to added the bean to the class in the constructor, 
like below.

> public class Main extends javax.swing.JFrame /*implements 
> PropertyChangeListener*/ {

   private SourceTableBean stb;
> 
>     public Main() {
>         initComponents();

           stb = new SourceTableBean();
           stb.addPropertyChangeListener( this );//Main must implement
                                                 //PropertyChangeListener
                                                 //for this to work.
           add( stb );  // or some way to add to layout
           pack();      // repack since we've changed layout
>     }



Another would be to make a third class that's responsible for starting 
up the GUI.  This class has a method to bind the Main instance to the 
SourceTableBean instance. Note that you really should be creating the 
GUI on the EDT, it's the only safe way to do it. Consider making the 
constructor of Main package private to enforce this.

public class StartHere {

   public static void createAndShowGUI() {

     java.awt.EventQueue.invokeAndWait( new Runnable() {
        @Override
        public void run() {
          Main m = new Main();
          SourceTableBean stb = new SourceTableBean();
           stb.addPropertyChangeListener( m); // Main must implement
                                              // PropertyChangeListener
                                              // for this to work.
           m.add( stb );  // or some way to add to layout
           m.pack();      // repack since we've changed layout
           m.setVisible( true );
         }
     } );
   }
}


Not compiled, beware of unbalanced braces.  Also, I have no idea why you 
want to use PropertyChangeListeners.  I think there might be a better 
way of doing this.  Look at the type of events PropertyChangeLister. 
Look at the types of events they handle for a JPanel (it's under 
java.awt.Container, I think) and they're really low-level GUI type 
events.  Possibly you want something higher level.

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

Back to comp.lang.java.gui | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Observer for Swing JPanel "thufir" <thufir@THRWHITE.remove-dii-this> - 2011-04-27 15:48 +0000
  Re: Observer for Swing JP "Mark Space" <mark.space@THRWHITE.remove-dii-this> - 2011-04-27 15:48 +0000
    Re: Observer for Swing JP "thufir" <thufir@THRWHITE.remove-dii-this> - 2011-04-27 15:48 +0000

csiph-web