Man!! I'm freaked out, I'm not able to do it
Actually I add dataValues 2d array and colNames array in new created jtable but it doesn't work. Don't know why :S 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);
  
      }
  
      }

/* * 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

Edited 6 Years Ago by Nick Evan: Added code-tags, removed custom line-number, tried to format is somewhat...

What you posted is unreadable. Use code tags. Click the button: (code) and place your code inside

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Sorry guys... here you go

/*
 * 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;
import javax.swing.table.DefaultTableModel;
/**
 *
 * @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();
//    DefaultTableModel model = new DefaultTableModel();
    JTable table;

    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];

  //  model.addColumn("FlightNo");

//    table = new JTable(model);
//    Object[] rowdata = {"123"};
//    model.insertRow(0, rowdata);

    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"};
    table = new JTable(dataValues, colNames);
    //panel.add(table,BorderLayout.CENTER);

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

and here's the function in MyDB.java which returns an array list..

public ArrayList<Flight> GetFlightInfo(){
        ArrayList<Flight> af = new ArrayList<Flight>();
        try {
            Statement st = con.createStatement();
            st.execute("select * from flight");
            ResultSet rs = st.getResultSet();

            int i = 0;
            while(rs.next()){
                Flight fl = new Flight();
                fl.setFlightNo(rs.getString("FlightNo"));
                fl.setAirline(rs.getString("Airline"));
                fl.setSourceCity(rs.getString("SourceCity"));
                fl.setDestinationCity(rs.getString("DestinationCity"));
                fl.setDepartureTime(rs.getString("DepartureTime"));
                fl.setArrivalTime(rs.getString("ArrivalTime"));
                af.add(fl);
            }
        } catch (SQLException e) {
            System.err.println("Exception: " + e.getMessage());
        }
        return af;

    }

Please help

Edited 6 Years Ago by Umar Ali: n/a

Your code looks better with the code tags.

please explain and show error messages or describe what's not working.

please explain and show error messages or describe what's not working.

I see a blank frame... no explicit errors

Your code looks better with the code tags.

please explain and show error messages or describe what's not working.

the code only works fine when i set hardcode values in arrays...

Where do you put any components in the frame? That's done with the add() method.

works fine when i set hardcode values

What's wrong with that? Many program have hardcoded values.
Please explain what "works fine" means.

Edited 6 Years Ago by NormR1: n/a

Yeah, well, I see where you create a JFRame, a JPanel, and a JTable, but no where do I see where you add the JTable to the JPanel, or the JPanel to the JFrame, or the JTable, to the JFrame, etc, etc, etc, so, yeah, you see a blank JFrame. (Except for the one line where you add the table to the panel, but that is commented out, and you don't do anything with the panel after that, anyway, which makes it useless anyhow.

stupid mistake... i didn't add the panel to the frame :S
but i'm not able to add button. it spread over the whole frame :(
thanks man

i'm not able to add button. it spread over the whole frame

That's the layout manager.
Try putting the button in a panel and adding the panel to the frame.

This article has been dead for over six months. Start a new discussion instead.