user1020024 user1020024 - 1 month ago 21
Java Question

Update JTable content based on ActionPerformed

I seem to be having trouble getting to grips with implementing a simple JTable that when a button is pressed on a GUI it simply updates the contents of the table

So far I seem to be getting mixed ideas of what type of table to implement to do this but from searching around it seems to be DefaultTable model.
So far I have declared the table

private JTable table;
static Object[] columnNames = new Object[]{"Column 1","Column 2"};
static Object[][] rowData = { {"1", "2"} };

public TestTables() {
DefaultTableModel tableModel;
tableModel = new DefaultTableModel(rowData, columnNames);
}


I'm not even sure if I have declared this correctly, but when I run the GUI it shows the table data correctly. I just dont understand how I can update it using an action performed

public void actionPerformed(ActionEvent e) {
if (e.getSource() == X1btn) {
// not sure how to set table to be {"3", "4"} instead
}

Answer

In your actionPerformed method, get a reference to the JTable's model (the details of this will depend on your program's structure -- something we don't know at present) which will be a DefaultTableModel, and then you can easily do whatever needs to be done with this object including adding rows, removing rows, changing values held by cells...

If you're still stuck, consider creating and posting a minimal compilable and runnable example that demonstrates your problem, an sscce.

For example:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

public class TestTables {
   static Object[] columnNames = new Object[] { "Column 1", "Column 2" };
   static Integer[][] rowData = { {1, 2}, {3, 4} };

   private JPanel mainPanel = new JPanel();
   private DefaultTableModel tableModel = new DefaultTableModel(rowData, columnNames);  
   private JTable table = new JTable(tableModel);   

   public TestTables() {
      JButton timesTwoBtn = new JButton("Multiply By 2");
      timesTwoBtn.addActionListener(new ActionListener() {

         public void actionPerformed(ActionEvent e) {
            for (int row = 0; row < tableModel.getRowCount(); row++) {
               for (int col = 0; col < tableModel.getColumnCount(); col++) {
                  Integer value = (Integer) tableModel.getValueAt(row, col);
                  value *= 2;
                  tableModel.setValueAt(value, row, col);
               }
            }
         }
      });
      JPanel btnPanel = new JPanel();
      btnPanel.add(timesTwoBtn);      


      mainPanel.setLayout(new BorderLayout());
      mainPanel.add(new JScrollPane(table), BorderLayout.CENTER);
      mainPanel.add(btnPanel, BorderLayout.SOUTH);
   }

   public JPanel getMainPanel() {
      return  mainPanel;
   }

   private static void createAndShowGui() {
      TestTables testTables = new TestTables();

      JFrame frame = new JFrame("TableFoo");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(testTables.getMainPanel());
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}