When i create 2 vectors (data, columnnames)and use them in a JTable to display rows with a column header it just displays the rows and not the column header.
In a JOptionPane they the column headers show up fine. I assume where bolded is where its going wrong as its not getting column names properly there.
public void displayLettingProperties() throws SQLException{
Connection conn = null;
try {
conn = DriverManager.getConnection(CONN_STRING, USERNAME, PASSWORD);
System.out.println("Connected to database.");
// The Connection is obtained
Statement Stmt = (Statement) conn.createStatement();
// Stmt.execute(createPropertyTable);
ResultSet rs = Stmt.executeQuery("select * from PropertyLettings ORDER BY propertyBedrooms ASC");
// It creates and displays the table
**JTable table = new JTable(buildTableModel(rs));**
// Set Column Widths
table.getColumnModel().getColumn(0).setPreferredWidth(100);
table.getColumnModel().getColumn(1).setPreferredWidth(50);
table.getColumnModel().getColumn(2).setPreferredWidth(350);
table.getColumnModel().getColumn(3).setPreferredWidth(100);
table.getColumnModel().getColumn(4).setPreferredWidth(100);
table.getColumnModel().getColumn(5).setPreferredWidth(350);
table.getColumnModel().getColumn(6).setPreferredWidth(100);
table.getColumnModel().getColumn(7).setPreferredWidth(130);
// JOptionPane.showMessageDialog(null, new JScrollPane(table));
final JPanel panelOne = new JPanel();
panelOne.setVisible(true);
panelOne.setBackground(Color.LIGHT_GRAY);
// JFRAME
final JFrame topFrame = new JFrame();
topFrame.setSize(1550, 300);
topFrame.setLocationRelativeTo ( null );
topFrame.setVisible(true);
topFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
// PUT TOGETHER
topFrame.add(panelOne);
panelOne.add(table);
panelOne.revalidate();
panelOne.repaint();
} catch (SQLException e) {
System.err.println("Cannot connect to database." + e);
} finally {
if(conn != null){
conn.close();
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…