I am trying to add a jTable in my class MyPage which has a tabbedPane in it. I am able to call my jTable in it and the table is perfectly visible but the column headers are not. I have even tried to add my table in a scrollPane but that also doesn't help me. Also, I am using Eclipse , I don't think that it is necessary to tell this but just to let y'all know. Here is my class MyPage and the table's code in it
Public MyPage(){
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
tabbedPane.setBorder(new LineBorder(new Color(0, 0, 0), 2));
tabbedPane.setBounds(10, 122, 744, 386);
contentPane.add(tabbedPane);
JPanel panel_4 = new JPanel();
tabbedPane.addTab("Customers list", null, panel_4, null);
panel_4.setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(0, 0, 735, 354);
scrollPane.setLayout(null);
table = new JTable();
table.setGridColor(new Color(0, 0, 0));
table.setBounds(10, 10, 715, 332);
panel_4.add(scrollPane.add(table));
conn = javaconnect.ConncrDB();
Table();
}
And here is the function through which I am calling the table entries of my database.
public void Table() {
try {
String sql = "select * From Balances";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
table.setModel(DbUtils.resultSetToTableModel(rs));
} catch (Exception e) {
JDialog dialog = new JDialog();
dialog.setAlwaysOnTop(true);
JOptionPane.showMessageDialog(dialog, e );
}finally {
try {
rs.close();
pst.close();
} catch (Exception e2) {
JDialog dialog = new JDialog();
dialog.setAlwaysOnTop(true);
JOptionPane.showMessageDialog(dialog, e2 );
}
}
}
I have tried every possible method I could find on Internet to resolve it but the table is not showing any column headers but when I open the model window in the properties of my jTable in windows builder design window the colomn headers are shown.
Expect all the answers which tell you, don't use
nulllayouts!is the key to your problem. The
JScrollPanehas it's own layout manager which managers, amongst other things, the column header viewI would strongly recommend that you take the time to read through Laying Out Components Within a Container, it will save you lot of time and help prevent a lot of other "unusual" problems
Next...
is not how you add a component to a
JScrollPane. If you take a closer look at How to Use Scroll Panes and How to Use Tables they will show how aJScrollPaneshould be used.JScrollPaneis an unusual container, in that you don't actually "add" anything to it, instead, you "wrap" the component in it.I prefer to use the constructor to pass the "view" to the
JScrollPane, but you can also use...So, with something like...
99.9% of your problems should go away