将按钮添加到 jtable
我有一个表,我必须在其中添加 JButton。
我正在做
TableColumnModel colModel = table.getColumnModel();
colModel.getColumn(0).setCellEditor(new MYCellEditor(new JCheckbox()));
MyCellEditor extends DefaultCellEditor{
public MyCellEditor(JCheckbox checkbox){
super(checkbox);
Jbutton button = new JButton("Start");
//actionlistener for button.
}
}
MyRenderer extends DefaultTablecellRenderer{
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
//return a button for column ==0
}
我的理解是,单元格编辑器对于列中的所有单元格都有相同的按钮实例。因此,如果我单击一个按钮,文本将从“开始”更改为“停止”,但如果我单击另一行中的按钮,它不起作用。调试后,它显示文本已为“停止”。
我怎样才能在每一行中有不同的按钮实例?
I am having a table in which i have to adda JButton.
I am doing
TableColumnModel colModel = table.getColumnModel();
colModel.getColumn(0).setCellEditor(new MYCellEditor(new JCheckbox()));
MyCellEditor extends DefaultCellEditor{
public MyCellEditor(JCheckbox checkbox){
super(checkbox);
Jbutton button = new JButton("Start");
//actionlistener for button.
}
}
MyRenderer extends DefaultTablecellRenderer{
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
//return a button for column ==0
}
My understanding is that the Celleditor has same instance of button for all cells in a column. So if i click on one button the text changes from "Start" to "stop" but if i click on button in other row it doesnt work.. After debugging it shows that the text is alreadt Stop .
How can i have different instance of button in each row ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
文章表格按钮列@camickr 之前的答案中引用的提供了更灵活的解决方案,但您可能会找到教程如何使用表:使用其他编辑器也很有帮助。
ColorEditor< /code>
讨论了
TableDialogEditDemo
,可通过 Java Web Start。您需要更改相应的ColorRenderer
相应地。The article Table Button Column cited in @camickr's previous answer provides a more flexible solution, but you may find the tutorial How to Use Tables: Using Other Editors helpful, too. The
ColorEditor
discussed there is part of theTableDialogEditDemo
, available via Java Web Start. You'll need to change the correspondingColorRenderer
accordingly.