Hello Guys

I need help with adding new row in a JTable.
I have created the JTable using Netbeans IDE under Swing Controls. Then I removed all the columns, so that I would add them manually.
Now, the function GetFlightInfo() returns an ArrayList with n numbers of rows with 6 columns. I tried each and every way to put the columns and data in JTable (labeled as FlightInformation in my code), but it never worked.
Please help me out, my project is stuck just because of this problem. And I need to use JTable at multiple places, so it's very important for me...
Here's the code

MyDB db= new MyDB();
        db.connect();
        ArrayList<Flight> f = new ArrayList<Flight>();
        f = db.GetFlightInfo();
        String[] colNames = {"FlightNo", "Airline", "Origin", "Destination", "Dept Time", "Arr Time"};
        int length = f.size();

        String dataValues[][] = new String[length][6];

        for(int i=0; i<length; i++){
            for(int j=0; j<6; j++){
                if(j==0){
                    dataValues[i][j] = ((Flight)f.get(i)).getFlightNo();
                }
                else if(j==1){
                    dataValues[i][j] = ((Flight)f.get(i)).getAirline();
                }
                else if(j==2){
                    dataValues[i][j] = ((Flight)f.get(i)).getSourceCity();
                }
                else if(j==3){
                    dataValues[i][j] = ((Flight)f.get(i)).getDestinationCity();
                }
                else if(j==4){
                    dataValues[i][j] = ((Flight)f.get(i)).getDepartureTime();
                }
                else if(j==5){
                    dataValues[i][j] = ((Flight)f.get(i)).getArrivalTime();
                }
            }
            ((DefaultTableModel)FlightInformation.getModel()).addRow(dataValues[i]);
        }

Thanks alot

Recommended Answers

All 4 Replies

Didi you tried google? It gives a lot of answers some are here below:

http://www.coderanch.com/t/345311/GUI/java/Adding-rows-Jtable
http://www.exampledepot.com/egs/javax.swing.table/AppendRow.html
http://www.roseindia.net/java/example/java/swing/InsertRows.shtml
http://www.velocityreviews.com/forums/t146928-jtable-add-row.html

Man!! I tried these but that doesn't work :(
Don't know why, please help with the code, I've seen alot of examples but of no use
Thanks

why are you doing a lot of type casting?
the posted code isn't expressive. Can you post the whole code (relevant for the question)? Have you tried all called classes are working?

Man!! I'm freaked out, I'm not able to do it :(
I also wanted to add button before the table but that's doesn't work even...
PLease help me...

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package GUIS;

import Classes.Flight;
import Classes.MyDB;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
/**
 *
 * @author Umar Ali
 */
public class JTableComponent extends javax.swing.JFrame{
  public static void main(String[] args)
{
    new JTableComponent();
  }

  public JTableComponent() {
    JFrame frame = new JFrame("Creating JTable Component Example!");
    JPanel panel = new JPanel();

    MyDB db= new MyDB();
    db.connect();
    ArrayList<Flight> f = new ArrayList<Flight>();
    f = db.GetFlightInfo();
    int length = f.size();

    String[] colNames = {"FlighNo", "Airline", "Origin", "Destination", "Dept Time", "Arr Time"};

    String dataValues[][] = new String[length][6];

    for(int i=0; i<length; i++){
        for(int j=0; j<6; j++){
                if(j==0){
                    dataValues[i][j] = ((Flight)f.get(i)).getFlightNo();
                }
                else if(j==1){
                    dataValues[i][j] = ((Flight)f.get(i)).getAirline();
                }
                else if(j==2){
                    dataValues[i][j] = ((Flight)f.get(i)).getSourceCity();
                }
                else if(j==3){
                    dataValues[i][j] = ((Flight)f.get(i)).getDestinationCity();
                }
                else if(j==4){
                    dataValues[i][j] = ((Flight)f.get(i)).getDepartureTime();
                }
                else if(j==5){
                    dataValues[i][j] = ((Flight)f.get(i)).getArrivalTime();
                }
            }
        }

    for(int h=0; h<length; h++){
        for(int m=0; m<6; m++)
            System.out.print(dataValues[h][m] + " ");
        System.out.println();
    }

    //String data[][] = {{"vinod","BCA","A"},{"Raju","MCA","b"},
     //{"Ranjan","MBA","c"},{"Rinku","BCA","d"}};

    //String col[] = {"Name","Course","Grade"};
    JTable table = new JTable(dataValues, colNames);
    panel.add(table,BorderLayout.CENTER);

    this.setSize(500, 400);
    this.setVisible(true);
  }
}

Thanks in advance

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.