Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法使用CustomTableModel将JButton添加到JTable_Java_Swing_Jtable_Jbutton_Abstracttablemodel - Fatal编程技术网

Java 无法使用CustomTableModel将JButton添加到JTable

Java 无法使用CustomTableModel将JButton添加到JTable,java,swing,jtable,jbutton,abstracttablemodel,Java,Swing,Jtable,Jbutton,Abstracttablemodel,我正在使用扩展AbstractTableModel的CustomTableModel创建一个表。我无法使用此自定义模型将JButton添加到列中。如果我对模型执行新的JButton(“一”)。。我看到的是文本“javax.swing.JButton[,0….,defaultCapable=true]”而不是按钮。谢谢你的帮助 public class CustomModelForTable extends AbstractTableModel { /** * */ private sta

我正在使用扩展AbstractTableModel的CustomTableModel创建一个表。我无法使用此自定义模型将JButton添加到列中。如果我对模型执行新的JButton(“一”)。。我看到的是文本“javax.swing.JButton[,0….,defaultCapable=true]”而不是按钮。谢谢你的帮助

public class CustomModelForTable extends AbstractTableModel {

/**
 * 
 */
private static final long serialVersionUID = 1L;

private String[] columnNames = {"First Name",
        "Last Name",
        "Sport",
        "# of Years",
        "Vegetarian",
        "Button"};

private Object[][] data = {
                            {"Kathy", "Smith", "Snowboarding", new Integer(5), new Boolean(false), new JButton("One")},
                            {"John", "Doe", "Rowing", new Integer(3), new Boolean(true), new JButton("Two")},
                            {"Sue", "Black", "Knitting", new Integer(2), new Boolean(false), new JButton("three")},
                            {"Jane", "White", "Speed reading", new Integer(20), new Boolean(true), new JButton("Four")},
                            {"Joe", "Brown", "Pool", new Integer(10), new Boolean(false), new JButton("Five")}
                          };

// # of Rows;
public int getRowCount() {
    return data.length;
}

// # of Columns;
public int getColumnCount() {
    return columnNames.length;
}

public Object getValueAt(int rowIndex, int columnIndex) {
    return data[rowIndex][columnIndex];
}

public Class getColumnClass(int column) {
    return getValueAt(0, column).getClass();
}

public boolean isCellEditable(int rowIndex, int columnIndex) {
    return true;
}

public void setValueAt(Object value, int rowIndex, int columnIndex) {
    if(isCellEditable(rowIndex, columnIndex)) {
       data[rowIndex][columnIndex] = value;
    }
}
}
编辑:
我能够通过实现TableCellRenderer来添加JButton。谢谢大家。

通过实现TableCellRenderer,我可以向表中添加按钮

class CustomModelForTable extends AbstractTableModel {
// Code
// After creating table using model.

 TableCellRenderer defaultRenderer = tableA.getDefaultRenderer(JButton.class);
    tableA.setDefaultRenderer(JButton.class, new      JButtonRendererClass(defaultRenderer) );
// Code
}


class JButtonRendererClass implements TableCellRenderer {

private TableCellRenderer __defaultRenderer;

public JButtonRendererClass(TableCellRenderer myRenderer) {
    __defaultRenderer = myRenderer;
}

public Component getTableCellRendererComponent(JTable table, Object value,
        boolean isSelected, boolean hasFocus, int row, int column) {
    if(value instanceof Component) {
        return (Component) value;
    }
    return __defaultRenderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
}
}
我觉得这对某人有用