Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.gui > #4009
| From | "thufir" <thufir@THRWHITE.remove-dii-this> |
|---|---|
| Subject | Re: Observer for Swing JP |
| Message-ID | <aFSpk.13964$hx.215@pd7urf3no> (permalink) |
| Newsgroups | comp.lang.java.gui |
| References | <RMapk.184982$gc5.84518@pd7urf2no><fiqpk.16914$LG4.12530@nlpi065.nbdc.sbc.com> |
| Date | 2011-04-27 15:48 +0000 |
| Organization | TDS.net |
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 | Next — Previous in thread | Find similar
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