我想在按下保存按鈕時將JTable中的信息返回到String [] []二維數組的數組中。實際上,我希望每次按下某個鍵時都要更新一個新的String [] []數組,但必須等到Save按鈕處於工作狀態。
當我創建ActionListener時,我不得不創建TestTable的另一個實例,所以我可以傳入表中,因爲我喜歡將所有的ActionListener都放在主類之外。
的動作監聽:
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);
}**
}
這是簡單的JTable中的代碼。
問題:我希望能夠在單擊按鈕時保存數據?
代碼:
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);
}
}