JTable单元格中的按钮:在单元格中存储数据时保持标题不变

时间:2012-08-10 11:25:07

标签: java swing jtable tablemodel abstracttablemodel

我正在开发一个JTable,它有一列包含List<List<String>>。此列应向用户显示一个按钮。当他点击按钮时,将对单元格的数据进行一些操作。

使用以下代码可以正常工作:

    JFrame testFrame = new JFrame("TEST");
    testFrame.setLayout(new BorderLayout() );
    testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    List<List<String>> list = new ArrayList<List<String>>();
    list.add(new ArrayList<String>());
    list.get(0).add("Hello World");

    OptConstraintTableModel dm = new OptConstraintTableModel(
        new String[] {"Data with a button"}, new Class<?>[] {List.class}, new Object[][] { {list} } 
            );
    JTable table = new JTable(dm);
    Action action_show = new AbstractAction()
    {
        private static final long serialVersionUID = 1L;
        public void actionPerformed(ActionEvent e)
        {
            JTable table = (JTable)e.getSource();
            int modelRow = Integer.valueOf( e.getActionCommand() );
            System.out.println(((List<List<String>>) table.getValueAt(modelRow,0)).get(0).get(0));
        }
    };
    ButtonColumn button_show = new ButtonColumn(table, action_show, 0);
    testFrame.add(table);

    testFrame.pack();
    testFrame.setVisible(true);

ButtonColumn取自here,其中TableModel的实现方式如下:

public class OptConstraintTableModel extends AbstractTableModel {

/**
 * Default Serial Version UID
 */
private static final long serialVersionUID = 1L;

private String[] columnNames;
private List<Object[]> data;
private Class<?>[] values;

/**
 * @param objects2 
 * @param objects 
 * 
 */
public OptConstraintTableModel(String[] columnNames, Class<?>[] values, Object[][] data) {
    this.columnNames = columnNames;
    this.values = values;
    this.data = new ArrayList<Object[]>();
    for(Object[] row : data) {
        this.data.add(row);
    }
}

/**
 * Adds the given Object array as last row into the TableModel
 * @param objects   the row to be added
 */
public void addRow(Object[] objects) {
    this.data.add(objects);
    this.fireTableRowsInserted(data.size()-1, data.size()-1);
}

/* (non-Javadoc)
 * @see javax.swing.table.TableModel#getColumnCount()
 */
@Override
public int getColumnCount() {
    return columnNames.length;
}

/* (non-Javadoc)
 * @see javax.swing.table.TableModel#getRowCount()
 */
@Override
public int getRowCount() {
    return data.size();
}

/* (non-Javadoc)
 * @see javax.swing.table.TableModel#getValueAt(int, int)
 */
@Override
public Object getValueAt(int row, int col) {
    return data.get(row)[col];
}

/*
 * (non-Javadoc)
 * @see javax.swing.table.AbstractTableModel#setValueAt(java.lang.Object, int, int)
 */
public void setValueAt(Object aValue, int row, int column) {
    data.get(row)[column] = aValue;
}

/*
 * (non-Javadoc)
 * @see javax.swing.table.AbstractTableModel#getColumnClass(int)
 */
public Class<? extends Object> getColumnClass(int c) {
    return values[c];
}

/*
 * (non-Javadoc)
 * @see javax.swing.table.AbstractTableModel#getColumnName(int)
 */
public String getColumnName(int col) {
    return columnNames[col];
}

/*
 * (non-Javadoc)
 * @see javax.swing.table.AbstractTableModel#isCellEditable(int, int)
 */
public boolean isCellEditable(int row, int col) {
    if (col == 1) return (Boolean) this.getValueAt(row, 0);
    if (col == 3) return (Boolean) this.getValueAt(row, 2);
    return true;
}

/**
 * Removes the indicated row from the table model
 * @param i     the index of the row to be delete
 */
public void removeRow(int i) {
    this.data.remove(i);
    this.fireTableRowsDeleted(i, i);
}
}

问题是:如何更改按钮的标题?现在,单元格的当前数据也显示为Button的标题,这对用户不是很友好。我希望所有按钮都带有标题“编辑”,我怎么能意识到这一点?

2 个答案:

答案 0 :(得分:1)

ButtonColumn班级重写方法getTableCellRendererComponent()中设置你想要按钮标题的内容。

而不是

renderButton.setText( value.toString() )

renderButton.setText("Edit");

答案 1 :(得分:1)

这是在ButtonColumn

中完成的
renderButton.setText( value.toString() );
renderButton.setIcon( null );

如果您只是将“编辑”设置为文字而不是value.toString(),则所有按钮都会显示“修改”

这必须在方法getTableCellRendererComponent中完成。必须对方法editButton中的getTableCellEditorComponent执行相同的操作,否则该值仍会显示在按钮上,而用户会对其执行单击操作。