Solved

How to add rows to a JTable dynamically?

Posted on 2004-10-19
6
379 Views
Last Modified: 2010-03-31
In my program I will add rows to a JTable dynamically. My codes look like follows:

...
...
private javax.swing.JTable jTable = null;
...
private javax.swing.JTable getJTable() {
  if(jTable == null) {
    jTable = new javax.swing.JTable();
  }
  return jTable;
}
...
...
for(int i= 0; i<5; i++)
  jTable.addColumn(new javax.swing.table.TableColumn(i,15));  // the table has 5 columns
...
...
// now I will read data from a file and insert into the table.
try{
  BufferedReader in = new BufferedReader(new FileReader(tableFileName));
  String line;
  while((line = in.readLine()) != null)
  {
     // here a row will be added to jtable;
  }
...
...
            
0
Comment
Question by:chenwei
  • 4
6 Comments
 
LVL 37

Accepted Solution

by:
zzynx earned 30 total points
ID: 12347237
your table uses the DefaultTableModel (by default), so:

       DefaultTableModel model = (DefaultTableModel) whateverObject.getTable().getModel();
On that model you can call

 void addRow(Object[] rowData)
or
 void addRow(Vector rowData)

they both add a row to the end of the model
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12347266
Assuming that your file is a CSV file containing the column data:
E.g.  column1Data,column2 Data,Data3,Data4,Data5

while((line = in.readLine()) != null) {
     model.addRow( line.split(",") );
}
0
 
LVL 7

Expert Comment

by:rohanbairat3
ID: 12350629
You can use DefaultTableModel or write your own model. It is very easy. These models identify the change by itself and would update the JTable automatically. You dont have to worry abt forcing the updates
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:chenwei
ID: 12356602
To zzynx:

Thanks, it works. But How can I set the table cell non-editable?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12356684
That's another Q isn't it?

>>How can I set the table cell non-editable?
Use your own TableModel (extending DefaultTableModel).

and overwrite the function

  boolean isCellEditable(int row, int column) {
      if (column==1)
         return false;   // makes the 2nd column non-editable
      return true;
  }
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12357037
Thanks for accepting
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now