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


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

Re: Observer for Swing JP

Path csiph.com!x330-a1.tempe.blueboxinc.net!feeder1.hal-mli.net!news.glorb.com!news-out.readnews.com!news-xxxfer.readnews.com!news-out.news.tds.net!newsreading01.news.tds.net!86597e80!not-for-mail
From "thufir" <thufir@THRWHITE.remove-dii-this>
Subject Re: Observer for Swing JP
Message-ID <aFSpk.13964$hx.215@pd7urf3no> (permalink)
X-Comment-To comp.lang.java.gui
Newsgroups comp.lang.java.gui
In-Reply-To <RMapk.184982$gc5.84518@pd7urf2no><fiqpk.16914$LG4.12530@nlpi065.nbdc.sbc.com>
References <RMapk.184982$gc5.84518@pd7urf2no><fiqpk.16914$LG4.12530@nlpi065.nbdc.sbc.com>
Content-Type text/plain; charset=IBM437
Content-Transfer-Encoding 8bit
X-Gateway time.synchro.net [Synchronet 3.15a-Win32 NewsLink 1.92]
Lines 159
Date Wed, 27 Apr 2011 15:48:11 GMT
NNTP-Posting-Host 96.60.20.240
X-Complaints-To news@tds.net
X-Trace newsreading01.news.tds.net 1303919291 96.60.20.240 (Wed, 27 Apr 2011 10:48:11 CDT)
NNTP-Posting-Date Wed, 27 Apr 2011 10:48:11 CDT
Organization TDS.net
Xref x330-a1.tempe.blueboxinc.net comp.lang.java.gui:4009

Show key headers only | View raw


  To: comp.lang.java.gui
On Fri, 15 Aug 2008 18:44:30 -0700, Mark Space wrote:


> 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.

What I came up with, which works but is, to me, black magic:


thufir@arrakis:~/beans$ 
thufir@arrakis:~/beans$ cat beans/src/SourceTableBean.java 

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

public class SourceTableBean extends javax.swing.JPanel /*implements 
PropertyChangeListener*/ {

    int currentRow = 1;
    //private final PropertyChangeSupport pcs = new PropertyChangeSupport
(this);

    public SourceTableBean() {
        initComponents();
    }

    public void setCurrentRow(int newRow) {
        int oldRow = currentRow;
        currentRow = newRow;
        //pcs.firePropertyChange("row", oldRow, newRow);
        this.firePropertyChange("row", oldRow, newRow);
    }

    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-
BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        table = new javax.swing.JTable();

        setLayout(new java.awt.BorderLayout());

        table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"a0", "a1", "a2", "a3"},
                {"b0", "b1", "b2", "b3"}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        table.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(table);

        add(jScrollPane1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents
    private void tableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-
FIRST:event_tableMouseClicked
        int row = table.getSelectedRow();
        setCurrentRow(row);        
    }//GEN-LAST:event_tableMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable table;
    // End of variables declaration//GEN-END:variables
}
thufir@arrakis:~/beans$ 
thufir@arrakis:~/beans$ cat beans/src/Main.java 

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

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

    public Main() {
        initComponents();
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-
BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        text = new javax.swing.JTextArea();
        table = new SourceTableBean();

        setDefaultCloseOperation
(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        text.setColumns(20);
        text.setRows(5);
        jScrollPane1.setViewportView(text);

        getContentPane().add(jScrollPane1, java.awt.BorderLayout.NORTH);

        table.addPropertyChangeListener(new 
java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent 
evt) {
                tablePropertyChange(evt);
            }
        });
        getContentPane().add(table, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void tablePropertyChange(java.beans.PropertyChangeEvent evt) 
{//GEN-FIRST:event_tablePropertyChange
        System.out.println("\nproperty changed\t" + evt.getPropertyName() 
+ "\t" + evt.getNewValue().toString());
    }//GEN-LAST:event_tablePropertyChange

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Main().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private SourceTableBean table;
    private javax.swing.JTextArea text;
    // End of variables declaration//GEN-END:variables
}
thufir@arrakis:~/beans$ 



I'm not totally sure that it's reliable, there seems to be a delay in 
registering user clicks -- but I'll live with that.  I think that a 
PropertyChangeSupport object would be useful for more complex situations, 
such as if the SourceBean had multiple buttons.

Please do critique!  I just need to bang out some code for now, but would 
like to know if there's a better way.  I didn't get a chance to look at 
your solution wrt to constructors, yet...



-Thufir

---
 * 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 | 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