今天很高兴是我的第一篇博客,我把最近做的课程设计内容的一点小问题拿出来解决,就是JTable中某一行的值如何传到新的窗口中的对应文本框中。`table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) // 单击鼠标左键
if (e.getClickCount() == 2) {
xiugai病人 c=new xiugai病人("d");
c.openDialog(table);
}
}
});`
给JTable一个事件,弹出一个新的窗口,然后创建一个方法,把你设置的table当作参数即可。
“`
public void openDialog(JTable table) {
// TODO Auto-generated method stub
int rown;int coln;
rown = table.getSelectedRow();
coln= table.getSelectedColumn();
String value0=(String) table.getValueAt(rown,0);
String value1=(String) table.getValueAt(rown,1);
String value2=(String) table.getValueAt(rown,2);
String value3=(String) table.getValueAt(rown,3);
t3.setText(value0);
t4.setText(value1);
t5.setText(value2);
t6.setText(value3);
c.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
}
});}
“`在窗口中实现上面的方法,然后即可获取相应行的值。
在实现上面之前要先把JTable的单元格设置为不可编辑才行。
到此结束了。
本人菜鸟一枚,望高手提点提点,甚是感谢。