R
R
roman38472014-04-24 21:08:32
Java
roman3847, 2014-04-24 21:08:32

Why are column names not displayed in JTable?

static String[] headers = { "Name", "Surname", "Telephone" };
   
    static String[][] data = {
        { "John", "Smith", "1112221" },
        { "Ivan", "Black", "2221111" },
        { "George", "White", "3334444" },
        { "Bolvan", "Black", "2235111" },
        { "Serg", "Black", "2221511" },
        { "Pussy", "Black", "2221111" },
        { "Tonya", "Red", "2121111" },
        { "Elise", "Green", "2321111" },
    };
    
    static JTable myTable;

myTable = new JTable(data, headers);
frame.add(myTable);


Why is the content of headers not displayed, and in the table only data from data?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Vershinin, 2014-04-25
@roman3847

Add

myTable.setFillsViewportHeight(true);
JScrollPane scrollPane = new JScrollPane(myTable);

D
Dm13y, 2014-04-24
@Dm13y

public class MyTable {
    public static String[] headers = { "Name", "Surname", "Telephone" };

    public static Object[][] data = {
            { "John", "Smith", "1112221" },
            { "Ivan", "Black", "2221111" },
            { "George", "White", "3334444" },
            { "Bolvan", "Black", "2235111" },
            { "Serg", "Black", "2221511" },
            { "Pussy", "Black", "2221111" },
            { "Tonya", "Red", "2121111" },
            { "Elise", "Green", "2321111" },
    };

    public static JTable myTable;


    public MyTable() {


        myTable = new JTable(data, headers);
        myTable.setFillsViewportHeight(true);
        JScrollPane scrollPane = new JScrollPane(myTable);
        JFrame frame = new JFrame();
        frame.add(scrollPane);
        frame.pack();
        frame.setVisible(true);
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question