Doug Hauf
Doug Hauf

Reputation: 3213

How to get the data back of a JTable?

Question:

I would like to get the information in the JTable back into an Array of String[][] 2-dimensional array when the save button is pressed. Actually I would like to update a new String[][] array every time a key is pressed but that will have to wait until the Save button is working.

When I made my ActionListener I had to create another instance of TestTable so I could pass in the table since I like to make all of my ActionListeners outside of the main class.

Action Listener:

            class buttonListener implements ActionListener {
        private String[][] tempTable = null; 
        **TestTable tt = new TestTable();**

        public Object[][] getTableData (JTable table) {
          int nRow = table.getRowCount();
          int nCol = table.getColumnCount();
          String[][] tData = new String[nRow][nCol];

          for (int i = 0 ; i < nRow ; i++) {
            for (int j = 0 ; j < nCol ; j++) {
              tData[i][j] = (String) table.getValueAt(i,j);
            }
          }
            return tData;
        }

        **@Override
        public void actionPerformed(ActionEvent arg0) {
           tempTable = (String[][]) getTableData(tt.t);
        }**
    }

This is the code for the simple JTable.

Question: I want to be able to save the data when the button is clicked?

Code:

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class TestTable extends JPanel {
    private static final long serialVersionUID = 1L;
      String[] columnNames = {"Col1", "Col2", "Col3", "Col4", "Col5", "Col6", "Col7"};
      Object[][] data = {
            {"A", "B", "C", new Integer(1), new Boolean(true), "AA", "BB"},
            {"D", "E", "F", new Integer(2), new Boolean(false), "CC", "DD"},
            {"G", "H", "I", new Integer(3), new Boolean(true), "EE", "FF"},
            {"J", "K", "L", new Integer(4), new Boolean(false), "GG", "HH"},
            {"M", "N", "O", new Integer(5), new Boolean(true), "II", "JJ"}
      };
      JTable t;

    public TestTable() {
        t = new JTable(data, columnNames);
        t.addMouseListener( new mouseListener());
        t.setPreferredScrollableViewportSize(new Dimension(500, 200));
        JScrollPane scrollPane = new JScrollPane(t);
        add(scrollPane);
        //add(t);
    }

    private void OpenGUI() {
        JButton b1 = new JButton("Save JTable");
        b1.addActionListener(new buttonListener());
        b1.setBounds(0,0,150,150);

        TestTable n = new TestTable();
        JFrame f = new JFrame("...Test JTable...");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setContentPane(n);
        f.add(b1);
        //f.add(n);
        f.pack();
        f.setVisible(true);
    }

    public Object[][] getTableData (JTable table) {
        int nRow = table.getRowCount();
        int nCol = table.getColumnCount();

        String[][] tData = new String[nRow][nCol];

        for (int i = 0 ; i < nRow ; i++) {
            for (int j = 0 ; j < nCol ; j++) {
                tData[i][j] = (String) table.getValueAt(i,j);
            }
        }

        return tData;
    }

    private void printData(Object[][] d, String[] s) {
        for (int c=0;c<d.length;c++) {
            System.out.println("Column Name: " + s[c]);
            for (int r=0;r<d.length;r++) {
                System.out.print(d[c][r] + " ");
            }
            System.out.println();
        }
    }

    public static void main(String[] MeatBalls) {
        TestTable myClass = new TestTable();
        buttonListener l  = new buttonListener();
        myClass.OpenGUI();
        myClass.printData(myClass.data,myClass.columnNames);
        myClass.data = myClass.getTableData(myClass.t);
        myClass.printData(myClass.data,myClass.columnNames);
    }
}

class buttonListener implements ActionListener {
    private String[][] tempTable = null; 
    TestTable tt = new TestTable();

    public Object[][] getTableData (JTable table) {
      int nRow = table.getRowCount();
      int nCol = table.getColumnCount();
      String[][] tData = new String[nRow][nCol];

      for (int i = 0 ; i < nRow ; i++) {
        for (int j = 0 ; j < nCol ; j++) {
          tData[i][j] = (String) table.getValueAt(i,j);
        }
      }
        return tData;
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
       tempTable = (String[][]) getTableData(tt.t);
    }
}

Upvotes: 0

Views: 225

Answers (1)

saiarcot895
saiarcot895

Reputation: 562

There are a few things you can do to simplify this code (and follow Java conventions):

  1. Make the JTable that holds the data and is displayed an instance variable. Do not create a new JTable. Inner classes can access any of the outer class's variables, so you can (and should) have the instance variable be private.

  2. Get rid of the getTableData method in the outer class. You won't be needing that.

  3. Have the getTableData method return String[][] instead of Object[][]. You're using String[][] inside the method, and you aren't doing anyone favors by returning an Object[][].

  4. When the actionPerformed method is triggered, use the getTableData method in the inner class to get the data in the JTable through the instance variable in the outer class.

  5. Use proper casing for class names and method names. Method names begin with a lower case, while class names begin with an upper case. You can see how StackOverflow thinks OpenGUI is a class. Make your instance variables private if nothing outside of this class is using them (and if it's not a constant). Also, watch the indentation.

Upvotes: 1

Related Questions