GeeCON Prague 2014*
The moose likes Swing / AWT / SWT and the fly likes Add a new row to JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Add a new row to JTable" Watch "Add a new row to JTable" New topic
Author

Add a new row to JTable

Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3479

Hi all.
I have extended the AbstractTableModel in my application and every thing
is ok.
my application is connected to a database and displayed the results using this table.
now, I want to add a button, when I clicked it, a new row should be added to the table (I mean to the JTable component), so I can enter some values how to perform this ??
clio katz
Ranch Hand

Joined: Apr 30, 2004
Posts: 101
without a code snippet, it's hard to tell how your component model is setup. Some people prefer to separate 'view', 'controller', and 'model' components, others mix the code inline.
If you have required fields, the button should popup a dialog window
allowing user to enter those fields. Once you validate what they
enter, you can add a new row to the underlying tablemodel, and fire
a TableDataChanged() event to trigger the class to update the view.
for example


(obviously untested but)
hth
Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3479

I want to add a row when pressing a jbutton (like the deploytool when you clicked on a jbutton, so a new row added and you entered some values)
clio katz
Ranch Hand

Joined: Apr 30, 2004
Posts: 101
you could do the same thing, just without a 'requiredField" feature.
you would attach a listener to the 'add' button. the listener would execute the method/code to add a new 'row/record' instance to the underlying datamodel and subsequently Fire TableChanged, or TableRowsInserted ...
your underlying data model will need to support initializing the row/record with no data, and your tablemodel should 'see' all the fields in the row as editable (i.e. isEditable method always returning true for these rows)
if you dig around, you can probably find the 'hooks' for adding your code in the existing table model/view code.
hth
Eddie Vanda
Ranch Hand

Joined: Mar 18, 2003
Posts: 281
The AbstractTableModel does not do any data storage for you, so you must have already added all that stuff to your implementation of AbstractTableModel. So a lot depends on what kind of data structures you have used, vectors or arrays?


The nice thing about Standards is that there are so many to choose from!
 
GeeCON Prague 2014
 
subject: Add a new row to JTable