

นำ component มาวางบน Frame จากนั้นแก้ไขดังภาพ
คลิดขวาที่ปุ่ม Add --> Event --> Action --> ActionPerformed แล้วเขียนโค้ดดังนี้
| |
if (checkTable == false) {
dataValues = new String[5][4];
checkTable = true;
}
DefaultTableModel df = new DefaultTableModel();
/////////////////////////////////////////////////////
String[] columnNames = {"Name", "Tel", "Email", "Address"};
dataValues[count][0] = "" + jTextField1.getText();
dataValues[count][1] = "" + jTextField2.getText();
dataValues[count][2] = "" + jTextField3.getText();
dataValues[count][3] = "" + jTextField4.getText();
df.setDataVector(dataValues, columnNames);
jTable1.setModel(df);
count += 1; |
|
คลิดขวาที่ปุ่ม Jtable --> Mouse --> MousePressed แล้วเขียนโค้ดดังนี้
| |
String ClickTable1 = jTable1.getValueAt(jTable1.getSelectedRow(), 0).toString();
String ClickTable2 = jTable1.getValueAt(jTable1.getSelectedRow(), 1).toString();
String ClickTable3 = jTable1.getValueAt(jTable1.getSelectedRow(), 2).toString();
String ClickTable4 = jTable1.getValueAt(jTable1.getSelectedRow(), 3).toString();
jTextArea1.setText("Show Click\n Name:" + ClickTable1 + "\nTel:" + ClickTable2 + "\nEmail:" + ClickTable3 + "\nAddress:" + ClickTable4);
|
| จากนั้นทำการประกาศตัวแปรใน Class ดังนี้ |
|
| |
private String dataValues[][];
private int count = 0;
private boolean checkTable = false; |
|
Shift+F6 เพื่อทดสอบโปรแกรม

Download This Project Netbeans.