JTable标题没有显示使用任何方法

时间:2013-09-17 15:43:28

标签: java swing

我已经尝试了几乎所有方法来获取表头但没有任何工作。

以下是代码:

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);
    }
}
 }

我尝试过使用table_1.getTableHeader();.add(new scrollpane(table_1.getTableHeader());等。任何帮助将不胜感激。

我收到了代码(从数据库中提取到youtube教程中的dTableModel

1 个答案:

答案 0 :(得分:2)

你需要:

  1. JTable添加到JScrollPane,例如JScrollPane scrollPane = new JScrollPane(table);(标准方式)

  2. 将单独的JTable添加到JFrame.CENTER区域,然后分隔JTableHeader,例如JTable.getTableHeader()JFrame.NORTH区域