?
Solved

How to add rows to a JTable dynamically?

Posted on 2004-10-19
6
Medium Priority
?
412 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 37

Accepted Solution

by:
zzynx earned 120 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

719 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