Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.gui > #3996 > unrolled thread
| Started by | "thufir" <thufir@THRWHITE.remove-dii-this> |
|---|---|
| First post | 2011-04-27 15:48 +0000 |
| Last post | 2011-04-27 15:48 +0000 |
| Articles | 3 — 2 participants |
Back to article view | Back to comp.lang.java.gui
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
| From | "thufir" <thufir@THRWHITE.remove-dii-this> |
|---|---|
| Date | 2011-04-27 15:48 +0000 |
| Subject | Observer for Swing JPanel |
| Message-ID | <RMapk.184982$gc5.84518@pd7urf2no> |
To: comp.lang.java.gui,comp.l
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?
No, this, intentionally, isn't a single class. By definition, working on
multiple files:
thufir@arrakis:~/code$ 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);
}
});*/
//table.addPropertyChangeListener(this);
getContentPane().add(table, java.awt.BorderLayout.CENTER);
pack();
}// </editor-fold>//GEN-END:initComponents
/**
* @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:~/code$
thufir@arrakis:~/code$ cat beans/src/SourceTableBean.java
import java.beans.PropertyChangeSupport;
public class SourceTableBean extends javax.swing.JPanel {
//pcs = new java.beans.PropertyChangeSupport(this);
//PropertyChangeSupport pcs = new PropertyChangeSupport(this);
public SourceTableBean() {
initComponents();
}
// <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
System.out.println("\nSourceTableBean.tableMouseClicked()\t");
int row = table.getSelectedRow();
int col = table.getSelectedColumn();
String val = table.getValueAt(row, col).toString();
System.out.println("how to send " + val + " to main?");
}//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:~/code$
thanks,
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
[toc] | [next] | [standalone]
| From | "Mark Space" <mark.space@THRWHITE.remove-dii-this> |
|---|---|
| Date | 2011-04-27 15:48 +0000 |
| Subject | Re: Observer for Swing JP |
| Message-ID | <fiqpk.16914$LG4.12530@nlpi065.nbdc.sbc.com> |
| In reply to | #3996 |
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
[toc] | [prev] | [next] | [standalone]
| From | "thufir" <thufir@THRWHITE.remove-dii-this> |
|---|---|
| Date | 2011-04-27 15:48 +0000 |
| Subject | Re: Observer for Swing JP |
| Message-ID | <aFSpk.13964$hx.215@pd7urf3no> |
| In reply to | #4001 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.gui
csiph-web