我在最后一列的 JTable 中有一个 JButton,当客户端单击它时,它将显示一个 JFrame。但是我不知道如何获取该行,以便可以获取该行中的对象并将其发送到 JFrame 构造函数?
这是我的桌子:
table = new JTable(model);
JTableHeader tableHeader = table.getTableHeader();
tableHeaderRenderer = table.getTableHeader().getDefaultRenderer();
tableHeader.setDefaultRenderer(new TableCellRenderer() {
private JLabel label;
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if (selectedColumn == value) {
label = (JLabel) tableHeaderRenderer.getTableCellRendererComponent(table,
value, true, true, row, column);
label.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
label.setBorder(BorderFactory.createCompoundBorder(label.getBorder(),
BorderFactory.createEmptyBorder(0, 5, 0, 0)));
label.setHorizontalAlignment(SwingConstants.LEFT);
} else {
label = (JLabel) tableHeaderRenderer.getTableCellRendererComponent(table,
value, false, false, row, column);
label.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
label.setBorder(BorderFactory.createCompoundBorder(label.getBorder(),
BorderFactory.createEmptyBorder(0, 5, 0, 0)));
label.setHorizontalAlignment(SwingConstants.CENTER);
}
return label;
}
});
table.getColumnModel().getColumn(3).setCellRenderer(new TableCellRenderer() {
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
JPanel buttonPanel = new JPanel(new BorderLayout());
buttonPanel.setBackground(Color.white);
JButton button = Shorter.button("Details", true);
buttonPanel.add(button);
return buttonPanel;
}
});
table.getColumnModel().getColumn(3).setCellEditor(new TableCellEditor() {
public boolean stopCellEditing() {return false;}
public boolean shouldSelectCell(EventObject arg0) {return false;}
public void removeCellEditorListener(CellEditorListener arg0) {}
public boolean isCellEditable(EventObject arg0) {return true;}
public Object getCellEditorValue() {return null;}
public void cancelCellEditing() {}
public void addCellEditorListener(CellEditorListener arg0) {}
public Component getTableCellEditorComponent(JTable arg0, Object arg1,
boolean arg2, int arg3, int arg4) {
JFrame frame = new JFrame();
frame.setVisible(true);
return null;}
});
加载框架时,我想知道选择哪一行添加到框架构造函数中...