gpt4 book ai didi

java - JTable 单元格中的按钮 : Keep Caption constant while storing data in the cell

转载 作者:行者123 更新时间:2023-12-02 07:38:20 24 4
gpt4 key购买 nike

我正在开发一个 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);
}
}

问题是:如何更改按钮的标题?现在,单元格的当前数据也显示为按钮的标题,这不太人性化。我希望所有按钮都带有“编辑”标题,我怎样才能实现这一点?

最佳答案

这是在 ButtonColumn 类中完成的

renderButton.setText( value.toString() );
renderButton.setIcon( null );

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

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

关于java - JTable 单元格中的按钮 : Keep Caption constant while storing data in the cell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11900673/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com