Question

I have a JFrame where I construct a JTable.

The JTable is made in two steps:

  1. I make a function initTable() where I make my table

  2. I make another function initDataintoTable() which ads my data from an arraylist to my table.

Everything works fine until now. The problem comes when I try to update my JTable. In fact, I open a JDialog and I try to a new item on the list. I get to the point of modifying the Arraylist with the new item, but I can't get my table updated after, I don't really know where to put the fireTableDataChanged() for example and how to call it when I want it.

In the initDataintoTable I added a TableModelListener to my DefaultTableModel tablemodel.

protected void initDataIntoTable() {
    tablemodel.setRowCount(0);

    for (//every item on my list) {
    String[] row = {dataitem, dataitem2};
        tablemodel.addRow(row);
    }

    TableModelListener myjtable_listener = new TableModelListener() {
        public void tableChanged(TableModelEvent e) {
                            // I have to write something here to update my table ?
                            // I don't know if it's here where I have to update my table and if it is, how should I call this listener from the place I want to update ?
            tablemodel.fireTableDataChanged(); // ?
        }
    };
    table.getModel().addTableModelListener(jtable_listener);
    //tablemodel.addTableModelListener(myjtable_listener);
    }
}

In my jDialog, I have an ActionListener on the Ok button:

class OKButton implements ActionListener {
     public void actionPerformed(ActionEvent e) {
    // Here I manage to get my updated list of objects
            // What should I call here to update my jtable ?    
    }
}                   
okbutton.addActionListener(new OKButton());

I already saw a few questions on this website, but nothing I tried actually worked with my program. Thank you for your help and sorry for my english.

Was it helpful?

Solution

You never have to call fireTableDataChanged method explicitly since it's up to the TableModel implementation to notify the view when data has been changed (insert/update/delete).

In your case calling tablemodel.addRow(...) is enough: the table model will notify the view that data has changed and this last one will be updated.

okButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        ...
        ((DefaultTableModel)table.getModel()).addRow(...);
        ...
    }
};

Edit

You state in a comment:

I can't do jframe.getTable() without having getTable static, and I can't get getTable() static without making table static. After doing this, my program works just like I wanted to.

If you have to add static modifier to make it work then you have a design problem. You probably have variables' scope issues in your code. Please see the example below: InputDialog is a different class that holds a reference to the table model without any static variable.

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;

public class Demo {

    private void createAndShowGUI() {

        DefaultTableModel model = new DefaultTableModel(new Object[]{"TimeStamp", "Value"}, 0) {
            @Override
            public Class<?> getColumnClass(int columnIndex) {
                switch(columnIndex) {
                    case 0: return Date.class;
                    default:  return super.getColumnClass(columnIndex);
                }
            }
        };

        final JTable table = new JTable(model);

        JButton button = new JButton("Add entry...");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new InputDialog((DefaultTableModel)table.getModel()).createAndShowGUI();
            }
        });

        JFrame frame = new JFrame("Demo");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.add(new JScrollPane(table), BorderLayout.CENTER);
        frame.add(button, BorderLayout.SOUTH);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    class InputDialog {

        private final DefaultTableModel model;

        public InputDialog(DefaultTableModel model) {
            this.model = model;
        }

        public void createAndShowGUI() {

            final JTextField textField = new JTextField(10);
            textField.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    model.addRow(new Object[]{new Date(), textField.getText()});
                    Window window = SwingUtilities.windowForComponent((Component)e.getSource());
                    window.dispatchEvent(new WindowEvent(window, WindowEvent.WINDOW_CLOSING));
                }
            });

            JPanel panel = new JPanel();
            panel.add(new JLabel("Please input a value and press ENTER key:"));
            panel.add(textField);

            JDialog dialog = new JDialog();
            dialog.setModal(true);
            dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            dialog.add(panel);
            dialog.pack();
            dialog.setLocationRelativeTo(null);
            dialog.setVisible(true);
        }

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {                
                new Demo().createAndShowGUI();
            }
        });
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top