invisible JTable headers

M

mehafi

Hi,
Does someone know why headers of this table doesn't appears?

import java.awt.*;
import javax.swing.*;

public class TestTable {
public static void main(String[] argum) {

String[] Headers = new String[] {"Number", "New?", "Origin",
"Destination", "Produced", "Weight" };
Object[][] Data = new Object[][]
{
{ "100420", Boolean.FALSE, "Des Moines IA", "Spokane WA",
"02/06/2000", new Float(450) },
{ "202174", Boolean.TRUE, "Basking Ridge NJ", "Princeton NJ",
"05/20/2000", new Float(1250) },
{ "450877", Boolean.TRUE, "St. Paul MN", "Austin TX",
"03/20/2000",
new Float(1745) },
{ "101891", Boolean.FALSE, "Boston MA", "Albany NY",
"04/04/2000", new
Float(88) }
};

JTable Tab = new JTable(Data, Headers);

JFrame Frame = new JFrame("TestTable");
Frame.getContentPane().add( Tab );
Frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
Frame.setSize(500, 200);
Frame.setVisible(true);
}
}

Interesting, that when I put this table into JScrollPane, headers
appears:

Frame.getContentPane().add( new JScrollPane( Tab ) );

But it doesn't works in simple JPanel. Is it necesary putting table
into
JScrollPane?

thanks in advane
 
J

Joshua Cranmer

Hi,
Does someone know why headers of this table doesn't appears?

By default, the headers of a JTable only show when put into a
JScrollPane; there is a method somewhere that can be used to get the
header part of the table.
Interesting, that when I put this table into JScrollPane, headers
appears:

Frame.getContentPane().add( new JScrollPane( Tab ) );

But it doesn't works in simple JPanel. Is it necesary putting table
into
JScrollPane?

Typically, it works several times easier to put into a scroll pane; most
people expect large tables to scroll; this won't happen without a scroll
pane.

If you want to use a JTable without a JScrollPane, then you can use
getTableHeader().
 
L

Lew

By pretty much universal convention, Java method, instance and local variable
names should begin with a lower-case letter and be written in camelCase
thereafter, class and interface names (including annotations) begin with an
upper-case letter and CamelCase thereafter, either way without underscores,
and public static final compile-time constants are written in all upper-case
letters, with underscores separating the compound word parts.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top