简体   繁体   English

无法使用CustomTableModel将JButton添加到JTable

[英]Not able to add JButton to a JTable using CustomTableModel

I am creating a Table using a CustomTableModel which extends AbstractTableModel. 我正在使用扩展AbstractTableModel的CustomTableModel创建一个表。 I am not able to add JButton to the column using this my custom model. 我无法使用此自定义模型将JButton添加到列中。 If I do new JButton("One") to the model .. I am seeing text "javax.swing.JButton[,0 .... ,defaultCapable=true]" instead of button. 如果我对模型执行新的JButton(“ One”),则看到的是文本“ javax.swing.JButton [,0 ....,defaultCapable = true]”而不是按钮。 Any help Appreciated. 任何帮助表示赞赏。

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;
    }
}
}

EDIT: I was able to add the JButton by implementing TableCellRenderer. 编辑:我能够通过实现TableCellRenderer添加JButton。 Thank u all. 谢谢大家

By implementing TableCellRenderer I am able to add buttons to the table. 通过实现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);
}
}

Thought it could be useful to someone. 认为这对某人可能有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM