Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / January 2004

Tip: Looking for answers? Try searching our database.

Accessing the associated dataModel when a particular frame is selected

Thread view: 
Bob Wightman - 31 Dec 2003 15:43 GMT
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 - 31 Dec 2003 16:41 GMT
> 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.
Bob Wightman - 01 Jan 2004 20:49 GMT
> > 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.