我正在尝试将JButton
放在JTable
中。没有显示。我做了很多尝试,但都没有任何效果。
这是我的代码:
public class ButtonRenderer extends JButton implements TableCellRenderer {
JButton button;
boolean enabled = true;
public ButtonRenderer()
{
button = new JButton();
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if (table.getValueAt(row, column) == "clicked") {
button.setEnabled(false);
} else {
button.setEnabled(true);
}
return button;
}
public void setEnabled(boolean enabled)
{
this.enabled = enabled;
}
}
class ButtonEditor extends AbstractCellEditor implements
TableCellEditor,ActionListener
{
JButton button;
private JTable cwwObjTblMainTable = null;
boolean enabled = true;
int clickCountToStart = 1;
public ButtonEditor()
{
button = new JButton();
button.addActionListener(this);
}
public Component getTableCellEditorComponent(JTable table,
Object value,
boolean isSelected,
int row, int column) {
cwwObjTblMainTable=table;
button.setText("Voir");
button.setEnabled(enabled);
return button;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public Object getCellEditorValue() {
return button.getText();
}
public boolean isCellEditable(EventObject anEvent) {
if (anEvent instanceof MouseEvent) {
return ((MouseEvent)anEvent).getClickCount() >= clickCountToStart;
}
return true;
}
public void actionPerformed(ActionEvent e)
{
enabled=false;
button.setEnabled(false);
//Business logic execution
System.out.println("Clicked");
}
}
视图上的调用:
tableau.setDefaultRenderer(JButton.class, new ButtonRenderer());
tableau.getColumn("Voir l'évenement").setCellEditor(new ButtonEditor());
答案 0 :(得分:0)
您如何尝试在下面的行中使用setDefaultRenderer()
是错误的。此方法的第一个参数必须是打算使用此渲染器的列的“数据类型”;不是渲染器的类型。
tableau.setDefaultRenderer(JButton.class, new ButtonRenderer());
以下程序演示了如何将JButton
添加到JTable
。
import javax.swing.*;
import javax.swing.event.CellEditorListener;
import javax.swing.table.*;
import java.awt.Component;
import java.awt.event.*;
import java.util.EventObject;
public class ButtonInTable {
public static void main(String[] args) {
DefaultTableModel tableModel = new DefaultTableModel(
new Object[][] {
{"aa", "aaa", ""},
{"bb", "bbb", ""},
{"cc", "ccc", ""},
{"dd", "ddd", ""},
{"ee", "eee", ""}},
new Object[] {"Column 1", "Column 2", "Column 3"});
JTable table = new JTable(tableModel);
table.getColumn("Column 3").setCellRenderer(new RendererAndEditor());
table.getColumn("Column 3").setCellEditor(new RendererAndEditor());
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new JScrollPane(table));
f.setBounds(300, 200, 400, 300);
f.setVisible(true);
}
}
class RendererAndEditor implements TableCellRenderer, TableCellEditor {
private JButton button;
RendererAndEditor() {
button = new JButton("Button");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Button clicked");
}
});
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
return button;
}
@Override
public java.awt.Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row,
int column) {
return button;
}
@Override
public Object getCellEditorValue() {
return null;
}
@Override
public boolean isCellEditable(EventObject anEvent) {
return true;
}
@Override
public boolean shouldSelectCell(EventObject anEvent) {
return true;
}
@Override
public boolean stopCellEditing() {
return true;
}
@Override
public void cancelCellEditing() { }
@Override
public void addCellEditorListener(CellEditorListener l) { }
@Override
public void removeCellEditorListener(CellEditorListener l) { }
}