Accessing the associated dataModel when a particular frame is selected  
Author Message
aqvi55





PostPosted: 2003-12-31 23:44:00 Top

java-programmer, Accessing the associated dataModel when a particular frame is selected I have JDesktop containing one or more JInternalFrames. Each of these
frames contains a JScrollPane which in turn contains a JTable, each
with its own tableModel. I wish to add a row to the table in the
currently selected frame. How do I recover the tableModel to do this?
I can do it if I only have a single table using:

table.getModel().addRow(item);

but it would be easier for my end users if they could have multiple
tables open. The tables all have the same structure but have differing
numbers of rows. I also may need to edit existing items in the tables.

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
model separate. This implies that I should keep a list (collection?)
of each model as I create it in my application. The difficulty then
becomes associating the currently selected view with the appropriate
model so that the editing is done in the appropriate table.

Any ideas or suggestions?

Oh, and a happy new year to everyone :-)

Bob
 
ak





PostPosted: 2004-1-1 0:42:00 Top

java-programmer >> Accessing the associated dataModel when a particular frame is selected > 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
> model separate.
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.


 
aqvi55





PostPosted: 2004-1-2 4:50:00 Top

java-programmer >> Accessing the associated dataModel when a particular frame is selected "ak" <email***@***.com> wrote in message news:<bsuu8c$d99$email***@***.com>...
> > 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
> > model separate.
> 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
> }
> }
> }
> }

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





PostPosted: 2004-1-2 6:37:00 Top

java-programmer >> Accessing the associated dataModel when a particular frame is selected > 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 :-)
>

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.


 
 
aqvi55





PostPosted: 2004-1-2 17:34:00 Top

java-programmer >> Accessing the associated dataModel when a particular frame is selected "ak" <email***@***.com> wrote in message news:<bt27f1$ufh$email***@***.com>...
>
[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