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 / September 2005

Tip: Looking for answers? Try searching our database.

JTable add row conditional...

Thread view: 
6e - 08 Sep 2005 22:08 GMT
Hi!

Im trying to have a new row added to a JTable when the previous row has
been filled in...

Ive tried to override
"public void tableChanged(TableModelEvent e)"

and add the row when the previous row all have values, however this
causes a stack overflow, as the addrow command is called seemingly
infinite (or infinitely enough to crash the app!).

So I figure there must be a better way to accomplish this...  Perhaps a
cell listener of some kind?  Does anyone know of something that may be
of help here?

Thanks!

I posted the coded to my overriden tablechanged function here just for
fun...

public void tableChanged(TableModelEvent e) {
       super.tableChanged(e);

          if (e.getType() == TableModelEvent.UPDATE){
              if (e.getFirstRow() == (this.getRowCount()-1)){
                  System.out.println("try row");
                  //then the last line was just editted
                  //check if role and name are entered, if they are add a row
                  if (this.getEditingColumn() == iName){
                      if (this.getValueAt((this.getRowCount()-1), iName) != null){
                          if (this.getValueAt((this.getRowCount()-1), iRole) !=
null){
                              System.out.println("added row");
                              addRow();
                          }
                      }
                  }
                 
              }
          }
              
   }
Vova Reznik - 08 Sep 2005 22:20 GMT
> I posted the coded to my overriden tablechanged function here just for
> fun...

Fun? (

Why don't you use DefaultTableModel?

If you want to use your own model - don't override super methods without
reason (just for fun)
Babu Kalakrishnan - 09 Sep 2005 19:22 GMT
>> I posted the coded to my overriden tablechanged function here just for
>> fun...
>
> Fun? (
>
> Why don't you use DefaultTableModel?

Hmm.. I'm no big fan of the DefaultTableModel. For any (non-tivial)
application that uses a JTable, you're better off using a custom model
that extends AbstractTableModel. The DefaultTableModel is in my opinion
one of the dumbest classes in the swing hierarchy - too inflexible -
probably fit for only JTable demos.

> If you want to use your own model - don't override super methods without
> reason (just for fun)

Concur with that view.

BK
Vova Reznik - 09 Sep 2005 19:53 GMT
> Hmm.. I'm no big fan of the DefaultTableModel. For any (non-tivial)

Nobody is big fun of the DefaultTableModel.
I've never used it in real life.
But it is simple and has convenient methods (add/remove)
Oliver Wong - 09 Sep 2005 15:14 GMT
> Im trying to have a new row added to a JTable when the previous row has
> been filled in...
[quoted text clipped - 5 lines]
> causes a stack overflow, as the addrow command is called seemingly
> infinite (or infinitely enough to crash the app!).

   I can make a guess as to what's going on. The user enters a row, and
finishes doing so. Your code sees that a new row has been added, so it adds
a new row. Then your code sees the row it just added, so it adds another
row. Then it sees THAT row was just added, so it adds another row. And so
on.

> So I figure there must be a better way to accomplish this...  Perhaps a
> cell listener of some kind?  Does anyone know of something that may be
> of help here?

   You need to find a way to tell the difference between user-added rows
and automatically added rows. Perhaps a hidden boolean value that the user
can't manually edit?

   - Oliver


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.