because your users interact with view, not with model.
create something like this:
public class JTableInternalFrame extends JInternalFrame {
JTable table;
public JTableInternalFrame (JTable table) {
this.table = table;
getContentPane().add(new JScrollPane(this.table));
addInternalFrameListener(new InternalFrameAdapter() {
public void internalFrameActivated(InternalFrameEvent e) {
//add row here
}
}
}
}
____________
http://reader.imagero.com the best java image reader.
> > In line with the MVC architecture of Swing, it appears as if I am
> > trying to get to the model through the view rather than keeping the
[quoted text clipped - 17 lines]
> }
> }
Thanks for the response but from the documentation it appears that the
internalFrameActivated method is fired whenever the frame becomes
active rather than in response to the user selecting "add a new
element". I.e. the desired sequence of events is:
1. User selects menu option or presses accelerator key.
2. the user then answers a series of questions about the contents of
the new row.
3. the new row is created.
4. The new row is to be appended to the currently highlighted table.
In the interim I have a solution that uses a HashMap to associate
between the instance of the JTable and its enclosing JInternalFrame at
the time of their creation. When the user wishes to add a row the
currently selected frame is used to recover the table from the
HashMap.
The relevant code is:
void addNewTableToDesktop(JTable table, String name)
{
JScrollPane scrollPane = new JScrollPane(table);
JInternalFrame frame = new JInternalFrame(name, true, true,
true, true);
frame.setContentPane(scrollPane);
frame.setSize(600, 200);
frame.setLocation(0,0);
frame.setVisible(true);
desktop.add(frame);
try {
frame.setSelected(true);
} catch (java.beans.PropertyVetoException e) {}
// Now add to our hash
tableToFrameMap.put(frame, table);
}
// Adds a new row of data to the table.
void addRowToTable(Object[] row)
{
JInternalFrame curFrame = desktop.getSelectedFrame();
JTable curTable = (JTable)tableToFrameMap.get(curFrame);
if(null != curTable)
{
DefaultTableModel model =
(DefaultTableModel)curTable.getModel();
model.addRow(row);
}
}
I'm not sure if this is the most elegant or preferred way to do things
but for the moment: it works :-)
Bob
ak - 01 Jan 2004 22:37 GMT
> Thanks for the response but from the documentation it appears that the
> internalFrameActivated method is fired whenever the frame becomes
[quoted text clipped - 50 lines]
> I'm not sure if this is the most elegant or preferred way to do things
> but for the moment: it works :-)
hmm, ok I misunderstood you.
public class JTableInternalFrame extends JInternalFrame {
JTable table;
public JTableInternalFrame (JTable table) {
this.table = table;
getContentPane().add(new JScrollPane(this.table));
}
//more constructors here
public JTable getTable() {
return table;
}
}
void addNewTableToDesktop(JTable table, String name) {
JScrollPane scrollPane = new JScrollPane(table);
JTableInternalFrame frame = new JTableInternalFrame(name, true, true,
true, true);
// rest of addNewTableToDesktop code here
}
void addRowToTable(Object[] row) {
JInternalFrame curFrame = desktop.getSelectedFrame();
if(curFrame instanceof JTableInternalFrame) {
JTable curTable = ((JTableInternalFrame)curFrame).getTable();
DefaultTableModel model = (DefaultTableModel)curTable.getModel();
model.addRow(row);
}
}
this is a little bit better then search in HashMap.
____________
http://reader.imagero.com the best java image reader.
Bob Wightman - 02 Jan 2004 09:33 GMT
[snip]
> this is a little bit better then search in HashMap.
Thanks for that. I'll give it a go when I get a bit of time.
Bob