JTable won't show column headers
Asked Answered
C

4

87

I have the following code to instantiate a JTable: the table comes up with the right number of rows and columns, but there is no sign of the titles atop the columns.

public Panel1()
{
    int  nmbrRows;

    setLayout(null);
    setBackground(Color.magenta);
    Vector colHdrs;

    //create column headers

    colHdrs = new Vector(10);
    colHdrs.addElement(new String("Ticker"));

    // more statements like the above to establish all col. titles       

    nmbrRows = 25;
    DefaultTableModel tblModel = new DefaultTableModel(nmbrRows, colHdrs.size());
    tblModel.setColumnIdentifiers(colHdrs);

    scrTbl = new JTable(tblModel);
    scrTbl.setBounds(25, 50, 950, 600);
    scrTbl.setBackground(Color.gray);
    scrTbl.setRowHeight(23);    
    add(scrTbl);

//rest of constructor
...

}

Comparing this to other table-making code, I don't see any missing steps, but something must be absent.

Crompton answered 23/2, 2010 at 18:47 Comment(0)
D
189

Put your JTable inside a JScrollPane. Try this:

add(new JScrollPane(scrTbl));
Donnelly answered 23/2, 2010 at 19:4 Comment(6)
I've discovered (and used) this solution before, but I'm curious if anyone knows why this works.Indiscernible
The table header it put into the JScrollPane top component ( or top view or something like that is named ) When there is no top component the JTable appears headless. This is by design.Hygrophilous
I have found this link, it might be helpful understaning this issue: blog.danieldee.com/2009/07/…Donnelly
Also from Javadoc of "configureEnclosingScrollPane" in JTable class: If this JTable is the viewportView of an enclosing JScrollPane(the usual situation),configure this ScrollPane by,amongst other things, installing the table's tableHeader as the columnHeaderView of the scroll pane. When a JTable is added to a JScrollPane in the usual way, using new JScrollPane(myTable), addNotify is called in the JTable (when the table is added to the viewport).JTable's addNotify method in turn calls this method, which is protected so that this default installation procedure can be overridden by a subclass.Linehan
The column headers are displayed by a separate component, a JTableHeader. If the JTable is put inside a JScrollPane, it adds the header automatically as column header of the JScrollPane. If you don't put it into a JScrollPane, you have to add the header by hand (it's probably the best to use a BorderLayout).Breunig
Doesn't seem to work for me. Maybe because im using TableModel instead of DefaultTableModel.Melanymelaphyre
M
27

The main difference between this answer and the accepted answer is the use of setViewportView() instead of add().

How to put JTable in JScrollPane using Eclipse IDE:

  1. Create JScrollPane container via Design tab.
  2. Stretch JScrollPane to desired size (applies to Absolute Layout).
  3. Drag and drop JTable component on top of JScrollPane (Viewport area).

In Structure > Components, table should be a child of scrollPane. enter image description here

The generated code would be something like this:

JScrollPane scrollPane = new JScrollPane();
...

JTable table = new JTable();
scrollPane.setViewportView(table);
Milla answered 10/3, 2014 at 14:42 Comment(0)
H
14

As said in previous answers the 'normal' way is to add it to a JScrollPane, but sometimes you don't want it to scroll (don't ask me when:)). Then you can add the TableHeader yourself. Like this:

JPanel tablePanel = new JPanel(new BorderLayout());
JTable table = new JTable();
tablePanel.add(table, BorderLayout.CENTER);
tablePanel.add(table.getTableHeader(), BorderLayout.NORTH);
Humfried answered 30/6, 2015 at 12:1 Comment(1)
The time when you don't want it to scroll is when your table contains a large amount of data both vertically and horizontally, where scrolling arbitrarily away from the (0,0) position will leave the end-user clueless as to what column they are staring at for a piece of data.Armageddon
Q
1
    public table2() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 485, 218);
    setTitle("jtable");
    getContentPane().setLayout(null);

    String data[][] = { { "Row1/1", "Row1/2", "Row1/3" },
            { "Row2/1", "Row2/2", "Row2/3" },
            { "Row3/1", "Row3/2", "Row3/3" },
            { "Row4/1", "Row4/2", "Row4/3" }, };

    String header[] = { "Column 1", "Column 2", "Column 3" };

    // Table
    JTable table = new JTable(data,header);


    // ScrollPane
    JScrollPane scrollPane = new JScrollPane(table);
    scrollPane.setBounds(36, 37, 407, 79);
    getContentPane().add(scrollPane);

   }

}

try this!!

Quickwitted answered 17/6, 2017 at 10:34 Comment(2)
getContentPane().add(table); does not make sense here. Apart from that: This answer does not add anything compared to the existing answers.Nata
@Quickwitted you should provide more description why you do this.Grantland

© 2022 - 2024 — McMap. All rights reserved.