Moe
Moe

Reputation: 470

Unable to get data into JTable

I'm trying to get the data from a JTextField to a JTable when the button Add

ActionListener:

public void actionPerformed(ActionEvent evt) {
    Object src = evt.getSource();
    if (src == AddBtn) {
        System.out.println("HELLOO");
        String nextRowId = Integer.toString(model.getRowCount());
        model.addRow(new Object[] { nextRowId, LatText.getText(),
                LongText.getText(), EvlText.getText() });

    } else if (src == NextBtn) {

    } else if (src == perviousBtn) {

    }
}

JTable:

String columns[] = { "ID", "Name", "Age", "Gender" };
Object data[][] = { { "0", "Tom", new Integer(20), "Male" },
        { "1", "Tina", new Integer(18), "Female" },
        { "2", "Raj", new Integer(19), "Male" },
        { "3", "Tina", new Integer(18), "Female" },
        { "4", "Raj", new Integer(19), "Male" },
        { "5", "Tina", new Integer(18), "Female" }

};
DefaultTableModel model = new DefaultTableModel(data, columns);

Full code: http://pastebin.com/9R9QyXdw?

Nothing happens when Add is clicked.

EDIT

I've corrected it to this:

 JTable dataTable = new JTable(model); 

but still no data gets added. Thank you.

Upvotes: 1

Views: 423

Answers (4)

vels4j
vels4j

Reputation: 11298

When I look your code, you haven't used the TableModel which you are updating into the Table.

If you want to notify your JTable about changes of your data, use tableModel.fireTableDataChanged()

Please refer http://docs.oracle.com/javase/7/docs/api/javax/swing/table/AbstractTableModel.html#fireTableDataChanged%28%29

Updated here, Check it now http://pastebin.com/bb00PzjD

Upvotes: 0

MadProgrammer
MadProgrammer

Reputation: 347204

There are two problems with the code.

The first has already been pointed out JTable table = new JTable(data, columns) is basically constructing it's own model, so when you try and add something your model in the action listener it's not the same model as that being used by the table.

The other is you declare the model twice.

You declare model as a class global field DefaultTableModel model = new DefaultTableModel(data, columns) and then re-declare it again in your addComponents methdod...

    public void addComponentsToPane(final Container pane) {
            DefaultTableModel model = new DefaultTableModel(data, columns);

So, even if you declared your table as JTable table = new JTable(model) it would still be using the wrong model..

Change the declaration of the table as described and remove the declaration of the DefaultTableModel model within the addComponentsToPane method and it should work fine...

Upvotes: 1

Robin
Robin

Reputation: 36611

The reason that nothing happens is that you update a TableModel which is not set on any JTable.

You use

JTable table = new JTable(data, columns);

to construct the table, instead of

JTable table = new JTable( model );

Upvotes: 2

Laf
Laf

Reputation: 8195

I haven't tested your code, but when you create your JTable (as read from your pastebin link), you use the following line:

JTable table = new JTable(data, columns);

You should specify your model instead of the raw data, the modifications mades to the model will show in your table:

JTable table = new JTable (model);

Upvotes: 1

Related Questions