0

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 by Nick Evan: Added code-tags, removed custom line-number, tried to format is somewhat...

4
Contributors
10
Replies
11
Views
7 Years
Discussion Span
Last Post by NormR1
0

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

Edited by mike_2000_17: Fixed formatting

0

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 by Umar Ali: n/a

0

Your code looks better with the code tags.

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

0

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

I see a blank frame... no explicit errors

0

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...

0

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 by NormR1: n/a

0

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.

0

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

0

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 topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.