gpt4 book ai didi

java - 复选框——新 boolean 值(真/假)

转载 作者:行者123 更新时间:2023-12-01 13:48:11 24 4
gpt4 key购买 nike

任何人都可以帮助我理解为什么此代码不显示复选框图标而不是文本?我试图在互联网上查找有关此的信息,但没有找到任何内容:(

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

enter image description here

最佳答案

我发现您正在尝试遵循 Oracle How to Use Tables .

不幸的是,这些例子不是很清楚。

如果您使用 String 和 Object[] 源,您将不会看到复选框。您将收到文本,就像您发现的那样。

您必须使用 Swing 的 TableModel 来识别 boolean 字段并显示复选框。 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("--------------------------");
}
}

关于java - 复选框——新 boolean 值(真/假),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20182855/

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