Search code examples
javaswingjtablejtableheader

No titles in JTable


Please have a look at the following code

import java.awt.*;

import javax.swing.JTable;
import javax.swing.*;

public class Table extends JFrame
{
    private JTable table;

    public Table()
    {

        String[] columnNames = {"first name","last name","address"};
        Object[][]data = {{"John","Kane","NY"},{"Nayomi","Writz","NY"}};

        table = new JTable(data, columnNames);

        getContentPane().add(table);
        this.pack();
        this.setVisible(true);


    }

    public static void main(String[]args)
    {
        new Table();
    }
}

I haven't used JTable before, so this is my first attempt. In here, it is not showing the column names, just showing the data. Why is that? Please help!


Solution

  • You need to put it in a JScrollPane or similar.

    See the top of the API docs for JTable, and JScrollPane.