如何在Swing java中的JTable的一行中添加按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1475543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add button in a row of JTable in Swing java
提问by om.
I have made one swing GUI which have JTable with some rows and Columns.How should I add a button to row in a JTable ?
我制作了一个带有一些行和列的 JTable 的摆动 GUI。我应该如何向 JTable 中的行添加一个按钮?
回答by Bostone
You don't add it to a row - you add it to the cell. This tutorialdescribes what you need.
您不会将其添加到行中 - 您将其添加到单元格中。本教程描述了您需要什么。
回答by camickr
Table Button Columngive one approach.
Table Button Column给出了一种方法。
回答by xrath
You can add Component as a table cell.
您可以将组件添加为表格单元格。
First of all, you should implement a class that have JButton as parent class and two interfaces that TableCellRenderer and TableCellEditor.
首先,您应该实现一个以 JButton 作为父类和两个接口 TableCellRenderer 和 TableCellEditor 的类。
The reason that should implement TableCellEditor is for receiving button's ActionEvent.
应该实现 TableCellEditor 的原因是为了接收按钮的 ActionEvent。
public class TableButton extends JButton implements TableCellRenderer, TableCellEditor {
private int selectedRow;
private int selectedColumn;
Vector<TableButtonListener> listener;
public TableButton(String text) {
super(text);
listener = new Vector<TableButtonListener>();
addActionListener(new ActionListener() {
public void actionPerformed( ActionEvent e ) {
for(TableButtonListener l : listener) {
l.tableButtonClicked(selectedRow, selectedColumn);
}
}
});
}
public void addTableButtonListener( TableButtonListener l ) {
listener.add(l);
}
public void removeTableButtonListener( TableButtonListener l ) {
listener.remove(l);
}
@Override
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row, int col) {
return this;
}
@Override
public Component getTableCellEditorComponent(JTable table,
Object value, boolean isSelected, int row, int col) {
selectedRow = row;
selectedColumn = col;
return this;
}
@Override
public void addCellEditorListener(CellEditorListener arg0) {
}
@Override
public void cancelCellEditing() {
}
@Override
public Object getCellEditorValue() {
return "";
}
@Override
public boolean isCellEditable(EventObject arg0) {
return true;
}
@Override
public void removeCellEditorListener(CellEditorListener arg0) {
}
@Override
public boolean shouldSelectCell(EventObject arg0) {
return true;
}
@Override
public boolean stopCellEditing() {
return true;
}
}
Then, I added a EventListener named TableButtonListener for handling button event as following.
然后,我添加了一个名为 TableButtonListener 的 EventListener 来处理按钮事件,如下所示。
public interface TableButtonListener extends EventListener {
public void tableButtonClicked( int row, int col );
}
And use above Renderer/Editor.
并使用上面的渲染器/编辑器。
TableButton buttonEditor = new TableButton("Button");
buttonEditor.addButtonListener(new TableButtonListener() {
@Override
public void tableButtonClicked(int row, int col) {
// do something
}
});
TableColumn col = new TableColumn(1, 80);
col.setCellRenderer(buttonEditor);
col.setCellEditor(buttonEditor);
cols.addColumn(colPattern);
If you want to display different button's label for each row, you should insert code block into the getTableCellRendererComponent and getTableCellEditorComponent which is to modify button's label.
如果你想为每一行显示不同的按钮标签,你应该在 getTableCellRendererComponent 和 getTableCellEditorComponent 中插入代码块来修改按钮的标签。
edit: Code compiles now. Bracketing error in TableButton constructor fixed.
编辑:代码现在编译。修复了 TableButton 构造函数中的括号错误。