invisible JTable headers

Discussion in 'Java' started by mehafi@gmail.com, Aug 23, 2007.

  1. Guest

    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
     
    , Aug 23, 2007
    #1
    1. Advertising

  2. wrote:
    > 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().
    >
    > thanks in advane
    >



    --
    Beware of bugs in the above code; I have only proved it correct, not
    tried it. -- Donald E. Knuth
     
    Joshua Cranmer, Aug 23, 2007
    #2
    1. Advertising

  3. Lew Guest

    wrote:
    >> Interesting, that when I put this table into JScrollPane, headers
    >> appears:
    >>
    >> Frame.getContentPane().add( new JScrollPane( Tab ) );


    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.

    --
    Lew
     
    Lew, Aug 23, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ike
    Replies:
    2
    Views:
    7,623
    Daniel Dyer
    Aug 22, 2003
  2. Tivo Escobar
    Replies:
    1
    Views:
    7,548
    manusa
    Apr 12, 2007
  3. Tintin92
    Replies:
    1
    Views:
    1,809
    Andrew Thompson
    Feb 14, 2007
  4. Andy B
    Replies:
    5
    Views:
    629
    Andy B
    May 29, 2008
  5. Haircuts Are Important
    Replies:
    3
    Views:
    414
    Haircuts Are Important
    Jun 4, 2013
Loading...

Share This Page