Solved

How to add rows to a JTable dynamically?

Posted on 2004-10-19
6
368 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:chenwei
Comment Utility
To zzynx:

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

Expert Comment

by:zzynx
Comment Utility
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
Comment Utility
Thanks for accepting
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to implement Singleton Design Pattern in Java.

744 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

16 Experts available now in Live!

Get 1:1 Help Now