Basic Table


Overview Basic version Model Cell Renderer Editor Column widths Tooltips Controls in cells

This is the most basic approach to having a JTable. There is a JTable constructor which takes two arguments - the column headings and the cell data. The data is in the form of a 2D Object array, and the coumn headings are a simple array of Strings. The following constructs such a table, constructs a scroll pane from that, and adds it to a JDialog:

 
public class TestDlg extends JDialog {

    private JTable table;
    public Object[][] data = {
        {"UK", "London"},
        {"France", "Paris"},
        {"Spain", "Madrid"},
        {"Italy", "Rome"}
    };

    TestDlg(JFrame owner, String title, boolean modal) {
        super(owner, title, modal);
        setBounds(50, 50, 400, 200);
        initComponents();
        setVisible(true);
    }

    private void initComponents() {
        Object[] columns = {"Country", "Capital"};
        table = new JTable(data, columns);
        JScrollPane sp = new JScrollPane(table);
        add(sp);
        sp.setBounds(0, 0, 400, 400);
    }
}

which outputs
This version is very simple, but has significant limitations. All cells are editable, all cell entries are treated as Strings, the data must be in an array, (or Vector), and if the data is altered, the table is not re-displayed

We can get more flexibility by setting up our own model

Comments

0 responses so far

Add a new comment

Your name

Your email (will not be published)

Your comment (no HTML)

Are you a robot? Please type the two words: