如何在netbeans中的Jtable单元格中添加按钮?将按钮添加到Jtable中的netbeans中的单元格
1
A
回答
0
您需要添加自定义单元格渲染器,当按钮状态更改时,重定位表格中的一些事件并重新绘制单元格。这是邪恶的,它是讨厌的,但它可以做到。
2
0
如Button Table Example所示,我们将创建一个类JButton
并实现TableCellRenderer
。
class ButtonRenderer extends JButton implements TableCellRenderer {
public ButtonRenderer() {
setOpaque(true);
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if (isSelected) {
setForeground(table.getSelectionForeground());
setBackground(table.getSelectionBackground());
} else {
setForeground(table.getForeground());
setBackground(UIManager.getColor("Button.background"));
}
setText((value == null) ? "" : value.toString());
return this;
}
}
然后您需要为该列创建一个单元格编辑器。
class ButtonEditor extends DefaultCellEditor {
protected JButton button;
private String label;
private boolean isPushed;
public ButtonEditor(JCheckBox checkBox) {
super(checkBox);
button = new JButton();
button.setOpaque(true);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fireEditingStopped();
}
});
}
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column) {
if (isSelected) {
button.setForeground(table.getSelectionForeground());
button.setBackground(table.getSelectionBackground());
} else {
button.setForeground(table.getForeground());
button.setBackground(table.getBackground());
}
label = (value == null) ? "" : value.toString();
button.setText(label);
isPushed = true;
return button;
}
public Object getCellEditorValue() {
if (isPushed) {
//
//
JOptionPane.showMessageDialog(button, label + ": Ouch!");
// System.out.println(label + ": Ouch!");
}
isPushed = false;
return new String(label);
}
public boolean stopCellEditing() {
isPushed = false;
return super.stopCellEditing();
}
protected void fireEditingStopped() {
super.fireEditingStopped();
}
}
然后,我们将设置ButtonRender
的实例作为细胞呈现该列和单元格编辑器ButtonEditor的一个实例。
\\"Button" is the column name
table.getColumn("Button").setCellRenderer(new ButtonRenderer());
table.getColumn("Button").setCellEditor(
new ButtonEditor(new JCheckBox()));
提供的链接中的example具有完整的可运行示例。
相关问题
- 1. 在JTable中添加按钮 - Netbeans
- 2. 将按钮添加到jtable
- 3. 将按钮添加到jtable
- 4. 将JButton添加到JTable单元格+ DB
- 5. 使用[tableView dequeueReusableCellWithIdentifier:forIndexPath:]将单元格中的按钮添加到按钮中。
- 6. 如何将JComboBox添加到JTable中的特定单元格
- 7. 如何将工具提示添加到jtable中的单元格?
- 8. 在JTable的单元格内添加按钮以及数据?
- 9. 将目标添加到tableview单元格内的collectionview单元格的按钮
- 10. 如何增加iPhone中的特定单元格的行高,并将按钮添加到增加的单元格
- 11. JTable:单元格中的自定义面板中的按钮
- 12. 如何将按钮添加到表格视图中的单元格
- 13. 的UITableView单元格添加按钮
- 14. 如何将按钮添加到uitableview的每个单元格?
- 15. 将动画添加到collectionview单元格内的按钮
- 16. 将不同的JLists添加到JTable中的单元格渲染器中
- 17. 在单元格中的单击按钮上添加新的单元格行
- 18. 将JComboBox添加到JTable单元格。选择的项目不留
- 19. 使用Netbeans将JCheckBox添加到JTable
- 20. 如何将格式添加到GWT中的单选按钮?
- 21. 如何将单选按钮添加到GWT中的网格?
- 22. 单击按钮后,如何将JTable添加到JPanel?
- 23. 使用按钮将新数据行添加到jTable单击
- 24. jtable单元格中的按钮和文本框对齐
- 25. 一个按钮添加到TableView中单元格:
- 26. 如何将按钮添加到datagridview单元格不是整列
- 27. 将展开/折叠按钮添加到DataGrid单元格
- 28. 如何将按钮添加到jQuery数据表单元格?
- 29. 如何将多个按钮添加到dojo网格单元
- 30. 如何将一个按钮添加到单元格末尾Swift
伟大的人,我发现它....非常感谢您的帮助! – Jasra 2010-11-30 09:48:10