-1

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.

1 Answers1

0

I have tried every possible method I could find on Internet to resolve it

Expect all the answers which tell you, don't use null layouts!

scrollPane.setLayout(null);

is the key to your problem. The JScrollPane has it's own layout manager which managers, amongst other things, the column header view

I 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...

panel_4.add(scrollPane.add(table));

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 a JScrollPane should be used.

JScrollPane is an unusual container, in that you don't actually "add" anything to it, instead, you "wrap" the component in it.

JScrollPane

I prefer to use the constructor to pass the "view" to the JScrollPane, but you can also use...

scrollPane.setViewportView(table);
panel_4.add(scrollPane);

So, with something like...

Public MyPage(){
    JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
    tabbedPane.setBorder(new LineBorder(new Color(0, 0, 0), 2));
    contentPane.setLayout(new BorderLayout());
    contentPane.add(tabbedPane);

    // Seems pointless, but hay
    JPanel panel_4 = new JPanel(new BorderLayout());
    tabbedPane.addTab("Customers list", panel_4);

    table = new JTable();
    table.setGridColor(new Color(0, 0, 0));
    JScrollPane scrollPane = new JScrollPane(table);

    panel_4.add(scrollPane);

    conn = javaconnect.ConncrDB();
    Table(); 
}

99.9% of your problems should go away

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366