NetBeans JTable without Scroll Pane, Keep Header

942 Views Asked by At

I'm attempting to add a JTable with NetBeans GUI builder. The table is inside a panel which already has a scroll bar. Netbeans automatically creates all JTables inside of a JScrollPane.

However, I want the table to scroll as part of a larger page. I do not need two scroll bars.

My problem is: if I get rid of the scroll pane, I lose the header.

Is there a way to have a table with a header inside the Netbeans GUI builder?

2

There are 2 best solutions below

0
On

My problem is: if I get rid of the scroll pane, I lose the header.

  • JTableHeader is (automatically) visible in the case that JTable is inside JScrollPane

  • you have to get JTableHeader from JTable and place this Object programatically by using LayoutManager to the container, I'm strongly recommend to use BorderLayout or GridBagLayout for this container

0
On

If you add JTabel directly to container(not to JScrollPane) you need to add JTableHeader by yourself(programatically ), try next example:

public static void main(String[] args) {
    JTable t = new JTable(new Object[][]{{1,2,3}},new Object[]{"1","2","3"});
    JFrame frame = new JFrame();
    frame.add(t.getTableHeader(),BorderLayout.NORTH);
    frame.add(t);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}

enter image description here