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


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

Observer for Swing JPanel

From "thufir" <thufir@THRWHITE.remove-dii-this>
Subject Observer for Swing JPanel
Message-ID <RMapk.184982$gc5.84518@pd7urf2no> (permalink)
Newsgroups comp.lang.java.gui
Date 2011-04-27 15:48 +0000
Organization TDS.net

Show all headers | View raw


  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

Back to comp.lang.java.gui | Previous | NextNext 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