1

I have tried almost every method to get table headers but nothing is working.

Here is the code:

package co.za.gecko.inked.crm;

import java.awt.EventQueue;
import java.awt.ScrollPane;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;

public class Welcome extends JFrame {

private JPanel contentPane;
private JTable table;
static Object[][] databaseInfo;
static Object[] columns = {"first name", "last name", "cellphone", "time", "station"};
static ResultSet rows;
static ResultSetMetaData metaData;
static DefaultTableModel dTableModel = new DefaultTableModel(databaseInfo, columns)      {
//      public Class getColumnClass(int column){
//          Class returnValue;
//          if((column >= 0) && (column < getColumnCount())){
//              returnValue = getValueAt(0, column).getClass();
//          } else {
//              returnValue = Object.class;
//          }
//          return returnValue;
//      }
};
private JTable table_1;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Welcome frame = new Welcome();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public Welcome() {

    setResizable(false);
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setBounds(100, 100, 1024, 768);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(null);
    setContentPane(contentPane);

    deleteAllRows(dTableModel);

    Connection conn = null;
    try {
        Class.forName("com.mysql.jdbc.Driver");
        conn = DriverManager.getConnection("jdbc:mysql://localhost/crm", "root", "t00rt00r");
        Statement sqlState = conn.createStatement();
        String selectStuff = "SELECT `first_name`, `last_name`, `cellphone`, `time` FROM volunteers";
        rows = sqlState.executeQuery(selectStuff);

        Object[] tempRow;


        while(rows.next()){
            tempRow = new Object[]{rows.getString(1), rows.getString(2), rows.getString(3), rows.getString(4)};
            dTableModel.addRow(tempRow);
        }

        // get column name ?
//          metaData = rows.getMetaData();
//          int numOfCol = metaData.getColumnCount();
//          
//          columns = new String[numOfCol];
//          
//          for(int i=1; i<= numOfCol; i++){
//              columns[i] = metaData.getColumnName(i);
//          }

    } catch (ClassNotFoundException ex) {
        // TODO Auto-generated catch block
        System.out.println(ex.getMessage());
    } catch (SQLException ex) {
        // TODO Auto-generated catch block
        System.out.println(ex.getMessage());
    }

    table_1 = new JTable();
    table_1.setModel(dTableModel);
    table_1.setBounds(10, 11, 988, 707);
    contentPane.add(table_1);

    ScrollPane scrollPane = new ScrollPane();
    scrollPane.setBounds(0, 0, 1008, 729);
    contentPane.add(scrollPane);
}

public static void deleteAllRows(final DefaultTableModel model) {
    for( int i = model.getRowCount() - 1; i >= 0; i-- ) {
        model.removeRow(i);
    }
}
 }

I have tried using table_1.getTableHeader();, .add(new scrollpane(table_1.getTableHeader()); and more. any help would be greatly appreciated.

I got the code (the pulling from the database to the dTableModel from youtube tutorials.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • did you call `pack()` before `setVisible()` ? – nachokk Sep 17 '13 at 15:46
  • if I call pack, it opens, but it is closed up, ie: no bounds are set –  Sep 17 '13 at 15:59
  • @Josh, that code looks familiar. I see you moved the code to create the frame from the main() method to the constructor of the Welcome() class. I wonder where you got that idea from? Now all you need to do is listener to the suggestion to NOT use static variables. – camickr Sep 17 '13 at 16:52
  • haha thanks, but if I take static away, it gives me an error message saying that they must be static. so it works, it works. It is just a prototype, I will end up recoding the program better next week or so. But thanks ! –  Sep 17 '13 at 17:21
  • @Josh, still waiting for you to reply to the posting where the above suggestion was made. – camickr Sep 18 '13 at 15:15
  • @camickr, I found the issue. The methods were static, therefore they needed the static variables. Took the static away from both and it worked! Thanks :) –  Sep 19 '13 at 04:15
  • @Josh, and you still haven't replied to your original question (http://stackoverflow.com/questions/18829788/jframe-not-opening-correctly-or-showing-contents) where you initially got the help and the suggestion. – camickr Sep 19 '13 at 05:58

1 Answers1

2

you would need to:

  1. put JTable to JScrollPane e.g. JScrollPane scrollPane = new JScrollPane(table); (standard way)

  2. add separate JTable to JFrame.CENTER area and separeate JTableHeader e.g. JTable.getTableHeader() to JFrame.NORTH area

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • if I put jTable to scrollpane, it says I must cast table_1 to an int? –  Sep 17 '13 at 16:01
  • I see now that it was ScrollPane, and not JScrollPane. I changed it to JScrollPane and it worked! Thank you!!! –  Sep 17 '13 at 16:01