Home arrow static arrow Java Programming [Archive] - Event handling across classes
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Event handling across classes
This topic has 5 replies on 1 page.

Posts:39
Registered: 7/16/04
Event handling across classes  
Jul 30, 2004 12:42 PM



 
I have a (newbie?) question regarding event handling:

I have a database class which has an update method and invokes a databaseGUI class to display the data:
/// databaseClass instantiated as DatabaseClass dataConn = new DatabaseClass()public class DatabaseClass {...     Public void updateData {     // write values to database     }      Public void showData {        thisDBGUI = new databaseGUI();        // populate screen with data from database through database GUI class        ...        // add a button to the interface via addUpdateButton method of databaseGUI class        thisDBGUI.addUpdateButton();      }} /// databaseGUI classpublic class databaseGUI extends JFrame implements ActionListener {     ...     public void addUpdateButton() {        buttonPanel = new JPanel();        updateButton = new JButton ("Udpate Record");        updateButton.addActionListener(this);        buttonPanel.add(okUpdate);     }...     public void actionPerformed(ActionEvent e) {         // need to call dataConn.updateData method      }}


Please forgive the incomplete snippets; everything displays perfectly and the code executes fine EXCEPT I do not know to use the actionPerformed method to execute the updateDatabase method on the dataConn instance. Any help is greatly appreciated!

David Erickson
 

Posts:2,206
Registered: 8/15/02
Re: Event handling across classes  
Jul 30, 2004 12:47 PM (reply 1 of 5)



 
Personally, I would move the actionlistener into the DatabaseClass. So in your show data, you would need something like
public void showData() {    thisDBGUI = new databaseGUI();    thisDBGUI.addUpdateButton();    hisDBGUI._updateButton.addActionListener(this);}


and either make the updateButton a static class parameter in the GUI, or add a getter method.
 

Posts:105
Registered: 6/8/04
Re: Event handling across classes  
Jul 30, 2004 1:23 PM (reply 2 of 5)



 
Personally, I would move the actionlistener into the DatabaseClass.

I agree, but I would do it a different way (just due to personal preference):

public class databaseGUI extends JFrame {         ...     public void addUpdateButton(DatabaseClass dbc) {          buttonPanel = new JPanel();          updateButton = new JButton ("Udpate Record");          updateButton.addActionListener(dbc);          buttonPanel.add(okUpdate);          }


Then, just pass a reference to "this" when you call databaseGUI's constructor.

I just like this method better because the GUI object is still setting up the entire GUI on its own. Personal preference, no more.
 

Posts:57
Registered: 10/18/00
Re: Event handling across classes  
Jul 30, 2004 3:59 PM (reply 3 of 5)



 
I don't see the reason for having the database class control the GUI or have any references to the GUI at all, I'm a little suspicious of that. Your dataBaseGUI class needs a reference to the Database class. The your method would look like

     public void actionPerformed(ActionEvent e) {         myDbClass.showData();     }
 

Posts:39
Registered: 7/16/04
Re: Event handling across classes  
Aug 2, 2004 12:13 PM (reply 4 of 5)



 
Thanks to everyone for your posts! I appreciate the quick response.
 

Posts:21,718
Registered: 98-02-20
Re: Event handling across classes  
Aug 2, 2004 12:19 PM (reply 5 of 5)



 
I'd say that your DatabaseClass should neither know nor care about the fact that you happen to be using a Swing UI. I wouldn't have any AWT Listener code inside that class.

I'd prefer making DatabaseClass a real JavaBean, using the java.util.EventListener interface and java.util.EventObject class to propagate changes to interested parties. You don't need AWT events for that.

A properly layered design would separate view from persistence. The twain should never meet.

%
 
This topic has 5 replies on 1 page.