Re: JButton Action Listen
To: comp.lang.java.gui
Sriram wrote:
Hi all
I have a question regarding action events... Is it possible for a
What you might want to consider is breaking things up a bit. For
example, the JButton should probably be a part of some larger GUI
construction.
public class MyFrame extends JFrame {
JButton button;
public MyFrame() {
buttonPanel = ...
button = new JButton( "Add Row" );
buttonPanel.add( button );
}
public addAddRowListener( ActionListener a ) {
button.addActionListener( a );
}
}
Now you have a higher level object -- one that represents your specific
window -- which also has a "hook" which allows you to configure objects
in side of the MyFrame object. This is hand for when you do want to
have both the window, and some other data.
public class EntryPoint {
private MyFrame = new MyFrame();
private File dataFile;
private List<..> rows = new ArrayList<..>();
private Model model;
private Table table; // etc.
public static void main(Strings... args) {
// Start up...
}
private void createAndShowGui() {
// ...
mainFrame.addAddRowListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
addRow();
}
} );
//...
}
private void addRow() {
model.addRow( createRow() );
int row = table.getRowCount() - 1;
table.getColumnModel().getColumn(4).setCellEditor(new
DefaultCellEditor(comboBox));
table.changeSelection(row, 0, false, false);
table.requestFocusInWindow();
}
}
private createRow() {
// ... add something to "rows" here
}
}
Now you've got something where the program code is seperate from the GUI
code. This generally makes for much easier testing, not to mention
design, debugging and maintenance. One thing I haven't show is making
interfaces so that you have a relatively easy set of calls to implement
in a test harness. GUI components can be complex and substituting your
own interface that does what you want can make the testing easier.
---
* 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