我在最后一列的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;}
});
当帧加载时我想知道选择哪一行添加到帧构造函数...
答案 0 :(得分:2)
但是我不知道怎么能得到这一行所以我可以得到该对象 行并将其发送到JFrame构造函数?
有三种选择
JButton inside a JTable in the last column
的事件必须从JTables view
重新调整正确的坐标,在JTables view
排序
过滤
列已重新排序
列被隐藏(从JTables视图中删除)
(和/或)将ListSelectionListener
添加到JTable
Mouse Events
也可以返回,请阅读官方Oracle tutorial How to use Tables - Specifying Tool Tips for Cells
了解工作代码示例
答案 1 :(得分:1)
我前段时间写了一个简单的ButtonColumn
,我认为它可以帮助你一点点,但正如@mKorbel所提到的,你必须解决排序,过滤等问题。
public class ButtonColumn extends AbstractCellEditor
implements TableCellRenderer, TableCellEditor{
private JButton button;
private String value;
public ButtonColumn(){
button = new JButton();
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
System.out.println("pressed"+value);
}
});
}
@Override
public Component getTableCellRendererComponent(JTable arg0, Object arg1,
boolean arg2, boolean arg3, int arg4, int arg5) {
button.setText(arg4+"");
value = " "+arg4;
return button;
}
@Override
public Object getCellEditorValue() {
return null;
}
@Override
public Component getTableCellEditorComponent(JTable arg0, Object arg1,
boolean arg2, int arg3, int arg4) {
button.setText(arg3+"");
value = " "+arg3;
return button;
}
}
以下一种方式将其设置为您的列:
table.getColumnModel().getColumn(YOUR_COLUMN_NUMBER).setCellEditor(new ButtonColumn());
table.getColumnModel().getColumn(YOUR_COLUMN_NUMBER).setCellRenderer(new ButtonColumn());
答案 2 :(得分:0)
请参阅Table Button Column以获取为您提供该行的解决方案。您需要做的就是为其提供Action
。