How can i display a data from a 'specific column' of JTable when a row is selected (table.getvalue(table.getValueAt(table.getSelectedRow(),'specific column'))into a Textarea. is there any Listener to make这项工作?我的意思是在每次选择中都必须更新 textarea 并获取选择行的特定列的值。
问问题
3015 次
1 回答
0
MouseListener
在表中添加一个。当用户选择单元格时,只需获取数据并将文本设置为TextField
. 您可以通过运行以下代码来查看演示。
private void createUI() {
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
Object[][] data = {{"a", "b"}, {"c", "d"}, {"e", "f"}};
Object[] columnNames = {"Col-1", "Col-2"};
final JTable table = new JTable(data, columnNames);
JPanel btnPnl = new JPanel(new FlowLayout(FlowLayout.LEFT));
btnPnl.add(new JLabel("Selected Value: "));
final JTextField text = new JTextField(10);
btnPnl.add(text);
table.getTableHeader().setReorderingAllowed(false);
table.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if(table.getSelectedRow() != -1 && table.getSelectedColumn() != -1) {
String selData = table.getValueAt(table.getSelectedRow(), table.getSelectedColumn()).toString();
text.setText(selData);
}
}
});
frame.add(table.getTableHeader(), BorderLayout.NORTH);
frame.add(table, BorderLayout.CENTER);
frame.add(btnPnl, BorderLayout.SOUTH);
frame.setTitle("Table to TextField Example.");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
于 2013-07-05T12:06:58.000 回答