如何将列名添加到我的java表中? (来自数据库)

时间:2016-07-05 10:44:20

标签: java swing postgresql

嗨所以我想知道为什么我的Jtable没有任何列标题,但它确实有来自我的postgres db的数据 我希望能够为每个数据选择列标题

public class aaaaa extends JFrame {

private JPanel contentPane;
private static JTable table;

/**
 * Launch the application.
 */
public static void main(String[] args) {
 Connection c = null;
 Statement stmt = null;
 try{
  Class.forName("org.postgresql.Driver");
  c = DriverManager
     .getConnection("jdbc:postgresql://localhost:5432/MovieDatabase",
     "postgres", "password");
    c.setAutoCommit(false);
    System.out.println("Opened database successfully");

     stmt = c.createStatement();
    ResultSet rs = stmt.executeQuery( "SELECT * FROM film" );
    table = new JTable(buildTableModel(rs));

     while ( rs.next() ) {
     int filmid = rs.getInt("filmid");
     String  filmtitle = rs.getString("filmtitle");
     int filmyear  = rs.getInt("filmyear");
     String  filmgenre = rs.getString("filmgenre");

     System.out.println( "ID = " + filmid );
     System.out.println( "TITLE = " + filmtitle );
     System.out.println( "YEAR = " + filmyear );
     System.out.println( "GENRE = " + filmgenre );

     System.out.println();
 }
  rs.close();
  stmt.close();
  c.close();
 }catch ( Exception e ) {
  System.err.println( e.getClass().getName()+": "+ e.getMessage() );
  System.exit(0);
}

    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                aaaaa frame = new aaaaa();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public aaaaa() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 411);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);


    table.setBounds(10, 11, 414, 350);
    contentPane.add(table);

    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setBounds(10, 11, 414, 350);
    contentPane.add(scrollPane);
}

public static DefaultTableModel buildTableModel(ResultSet rs)
        throws SQLException {

    ResultSetMetaData metaData = rs.getMetaData();

    // names of columns
    Vector<String> columnNames = new Vector<String>();
    int columnCount = metaData.getColumnCount();
    for (int column = 1; column <= columnCount; column++) {
        columnNames.add(metaData.getColumnName(column));
    }

    // data of the table
    Vector<Vector<Object>> data = new Vector<Vector<Object>>();
    while (rs.next()) {
        Vector<Object> vector = new Vector<Object>();
        for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
            vector.add(rs.getObject(columnIndex));
        }
        data.add(vector);
    }

    return new DefaultTableModel(data, columnNames);

}
}

enter image description here

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您在滚动窗格之外使用您的桌子。如果您真的希望在独立视图中使用JTable,可以使用getTableHeader()获取标题:

JTableHeader header = table.getTableHeader();

然后将header添加到您想要的位置。

另一种更优选的方法是简单地将表格添加到滚动窗格中:

contentPane.add(new JScrollPane(table));

见这里:

https://docs.oracle.com/javase/7/docs/api/javax/swing/JTable.html

  

请注意,如果您希望在独立视图中使用JTable(在JScrollPane之外)并希望显示标题,则可以使用getTableHeader()获取它并单独显示它。