Ecco il codice:
codice:
private static JTextField txt1 = new JTextField(10);
private static JTextField txt2 = new JTextField(10);
private static JTextField txt3 = new JTextField(10);
private static JTextField txt4 = new JTextField(10);
//In questo esempio la tabella ha 4 colonne
private static DefaultTableModel mdl = new DefaultTableModel();
private static JTable tblIns = new JTable(mdl); //La tua tabella
private static JScrollPane sclTbl = new JScrollPane(tblIns);
private static JButton bti;
private static JDialog dialog = new JDialog();
int row = 0;
private static Action apriDialog = new AbstractAction("Ok")
{
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent evt)
{
row = tblIns.getSelectedRow();
txt1.setText((String) tblIns.getValueAt(row, 0 /*numero colonna*/));
txt2.setText((String) tblIns.getValueAt(row, 1));
txt3.setText((String) tblIns.getValueAt(row, 2));
txt4.setText((String) tblIns.getValueAt(row, 3));
txt4.setText((String) tblIns.getValueAt(row, 3));
}
};
private static WindowListener wnlGUIExit = new WindowAdapter()
{
private static final long serialVersionUID = 1L;
public void windowClosing(WindowEvent arg0) {
tblIns.setValueAt(txt1.getText(), row, 0);
tblIns.setValueAt(txt2.getText(), row, 1);
tblIns.setValueAt(txt3.getText(), row, 2);
tblIns.setValueAt(txt4.getText(), row, 3);
}
}
};
public static void main(String args[]){
JFrame frm = new JFrame();
bti = new JButton(apriDialog);
dialog.addWindowListener(wnlGUIexit);
//Popola la tabella come serve a te
dialog.add(txt1); //aggiungi le JTextField come vuoi tu al JDialog
dialog.add(txt2);
dialog.add(txt3);
dialog.add(txt4);
frm.add(tblIns, BorderLayout.NORTH)
frm.add(bti, BorderLayout.SOUTH)
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.pack();
frm.setVisible();
}
Questo è quello che farei io implementandolo leggermente