Solved

How to add rows to a JTable dynamically?

Posted on 2004-10-19
6
406 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 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
login jsp example 24 105
Detect Closed Loops (circles, figure-8s, etc) in PNG Images 6 96
Session in java desktop 5 38
Overriding a method 3 13
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

751 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