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


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

Re: Observer for Swing JP

Path csiph.com!x330-a1.tempe.blueboxinc.net!feeder1.hal-mli.net!feeder.news-service.com!feeder.xsnews.nl!feed.xsnews.nl!border-1.ams.xsnews.nl!206.55.64.80.MISMATCH!newspump.sol.net!news-out.readnews.com!transit3.readnews.com!news-out.news.tds.net!newsreading01.news.tds.net!86597e80!not-for-mail
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)
X-Comment-To comp.lang.java.gui
Newsgroups comp.lang.java.gui
In-Reply-To <RMapk.184982$gc5.84518@pd7urf2no>
References <RMapk.184982$gc5.84518@pd7urf2no>
Content-Type text/plain; charset=IBM437
Content-Transfer-Encoding 8bit
X-Gateway time.synchro.net [Synchronet 3.15a-Win32 NewsLink 1.92]
Lines 74
Date Wed, 27 Apr 2011 15:48:08 GMT
NNTP-Posting-Host 96.60.20.240
X-Complaints-To news@tds.net
X-Trace newsreading01.news.tds.net 1303919288 96.60.20.240 (Wed, 27 Apr 2011 10:48:08 CDT)
NNTP-Posting-Date Wed, 27 Apr 2011 10:48:08 CDT
Organization TDS.net
Xref x330-a1.tempe.blueboxinc.net comp.lang.java.gui:4001

Show key headers only | 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 | Unroll thread


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