2

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!

mKorbel
  • 109,525
  • 20
  • 134
  • 319
PeakGen
  • 21,894
  • 86
  • 261
  • 463

1 Answers1

9

You need to put it in a JScrollPane or similar.

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

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305