As the title suggests I am trying to read the data that is dynamically entered by the user. This is my first time using JTables.
This is how I am reading the data:
public void readData() { for(int i=0; i < rows.length; i++) { for (int j=0; j < 7; j++) { System.out.println(rows[i][j]); } } }
I think what my problem is here is that the rows array is not being updated as a user inputs information. I've tried implementing TableModelListener but when I do it returns an error when I add more rows to the table.
Here is the code
package sycorp.iface; import java.awt.Container; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Vector; import javax.swing.*; import javax.swing.table.DefaultTableModel; public class Gui extends JPanel implements ActionListener { // Declare JFrame private JFrame f = new JFrame("Table Test"); // Declare Menus private JMenuBar mb = new JMenuBar(); private JMenu muFile = new JMenu("File"); private JMenuItem muItemClose = new JMenuItem("Close"); // Declare Buttons private JButton addNew = new JButton("Add New"); private JButton read = new JButton("Read Data"); // Table private String columns[] = {"Make", "Model", "Year", "Miles", "Condition", "Price", "Notes"}; private String rows[][] = {{}}; private JTable jt = new JTable(rows, columns); // Declare Panels private JScrollPane dataPane = new JScrollPane(jt); public Gui() { f.setJMenuBar(mb); // Add the menu bar to the JFrame jt.setPreferredScrollableViewportSize(new Dimension(865, 600)); // Set the Deminsions of the table jt.setFillsViewportHeight(true); // Set it to the correct height jt.setModel(new DefaultTableModel(rows, columns)); // File Menu mb.add(muFile); // Add the file menu to the menu bar muFile.add(muItemClose); // Add the close options to the file menu // Button mb.add(addNew); mb.add(read); // Add Action Listener muItemClose.addActionListener(this); // Add action listener to muItemClose addNew.addActionListener(this); // Add action listener to addNew button read.addActionListener(this); // Add action listener to remove button // Set Action Command muItemClose.setActionCommand("close"); // Set command for muItemClose addNew.setActionCommand("addNew"); // Set command for addNew button read.setActionCommand("read"); // set command for remove button } public void launchFrame() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); // Set the theme Container pane = f.getContentPane(); // Create the frame pane.add(dataPane); // Add the pane f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Set close operation f.setVisible(true); // Make it visible f.setResizable(true); // Make it resizable f.setSize(865, 600); // Set the default size } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (UnsupportedLookAndFeelException e) { e.printStackTrace(); } } @Override public void actionPerformed(ActionEvent e) { String cmd = e.getActionCommand(); switch(cmd) { case "close": System.exit(0); break; case "addNew": newRow(); break; case "read": readData(); } } public void newRow() { ((DefaultTableModel) jt.getModel()).addRow(new Vector(6)); } public void readData() { for(int i=0; i < rows.length; i++) { for (int j=0; j < 7; j++) { System.out.println(rows[i][j]); } } } public void messageBox(String msg) { JOptionPane.showMessageDialog(f, msg); } public String inputBox(String msg) { return null; } }