任何人都可以帮助我理解为什么此代码不显示复选框图标而不是文本? 我试图在互联网上找到关于此的事情,但没有找到任何东西:(
JTabbedPane tabProcessamentoSalarial = new JTabbedPane(JTabbedPane.TOP);
tabbedPane.addTab("Remunera\u00E7\u00F5es", null, tabProcessamentoSalarial, null);
JPanel pnlAcumulados = new JPanel();
tabProcessamentoSalarial.addTab("Acumulados", null, pnlAcumulados, null);
pnlAcumulados.setLayout(null);
String[] columnAcumulados = {"First Name", "Last Name", "Sport", "# of Years", "Vegetarian"};
Object[][] dataAcumulados = {
{"Kathy", "Smith", "Snowboarding", new Integer(5), new Boolean(false)},
{"John", "Doe", "Rowing", new Integer(3), new Boolean(true)},
{"Sue", "Black", "Knitting", new Integer(2), new Boolean(false)},
{"Jane", "White", "Speed reading", new Integer(20), new Boolean(true)},
{"Joe", "Brown", "Pool", new Integer(10), new Boolean(true)}
};
JTable tblAcumulados = new JTable(dataAcumulados, columnAcumulados);
JScrollPane scrollPaneAcumulados = new JScrollPane(tblAcumulados);
tblAcumulados.setFillsViewportHeight(true);
tblAcumulados.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
scrollPaneAcumulados.setBounds(46, 36, 508, 160);
pnlAcumulados.add(scrollPaneAcumulados);
答案 0 :(得分:1)
我发现您正在尝试关注Oracle How to Use Tables。
不幸的是,这些例子不是很清楚。
如果使用String和Object []源,则不会出现复选框。你会得到文字,就像你发现的一样。
您必须使用Swing的TableModel来识别布尔字段并显示一个复选框。 DefaultTableModel在大多数情况下有效。
这是Oracle的example。我认为这不是很好,但我没有一个方便的例子。
class MyTableModel extends AbstractTableModel {
private String[] columnNames = {"First Name",
"Last Name",
"Sport",
"# of Years",
"Vegetarian"};
private Object[][] data = {
{"Kathy", "Smith",
"Snowboarding", new Integer(5), new Boolean(false)},
{"John", "Doe",
"Rowing", new Integer(3), new Boolean(true)},
{"Sue", "Black",
"Knitting", new Integer(2), new Boolean(false)},
{"Jane", "White",
"Speed reading", new Integer(20), new Boolean(true)},
{"Joe", "Brown",
"Pool", new Integer(10), new Boolean(false)}
};
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
/*
* JTable uses this method to determine the default renderer/
* editor for each cell. If we didn't implement this method,
* then the last column would contain text ("true"/"false"),
* rather than a check box.
*/
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
/*
* Don't need to implement this method unless your table's
* editable.
*/
public boolean isCellEditable(int row, int col) {
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.
if (col < 2) {
return false;
} else {
return true;
}
}
/*
* Don't need to implement this method unless your table's
* data can change.
*/
public void setValueAt(Object value, int row, int col) {
if (DEBUG) {
System.out.println("Setting value at " + row + "," + col
+ " to " + value
+ " (an instance of "
+ value.getClass() + ")");
}
data[row][col] = value;
fireTableCellUpdated(row, col);
if (DEBUG) {
System.out.println("New value of data:");
printDebugData();
}
}
private void printDebugData() {
int numRows = getRowCount();
int numCols = getColumnCount();
for (int i=0; i < numRows; i++) {
System.out.print(" row " + i + ":");
for (int j=0; j < numCols; j++) {
System.out.print(" " + data[i][j]);
}
System.out.println();
}
System.out.println("--------------------------");
}
}
答案 1 :(得分:0)
正如akf指向here,只需创建一个新类:
公共类CheckBoxRenderer扩展JCheckBox实现了TableCellRenderer {
CheckBoxRenderer() {
setHorizontalAlignment(JLabel.CENTER);
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if (isSelected) {
setForeground(table.getSelectionForeground());
//super.setBackground(table.getSelectionBackground());
setBackground(table.getSelectionBackground());
} else {
setForeground(table.getForeground());
setBackground(table.getBackground());
}
setSelected((value != null && ((Boolean) value).booleanValue()));
return this;
}
}
然后,在将模型插入表格后输入:
CheckBoxRenderer checkBoxRenderer = new CheckBoxRenderer();
mytable.getColumnModel().getColumn(number_of_the_one_I_want).setCellRenderer(checkBoxRenderer);