问题描述
JCheckBox
但是当我使用system.out.print时它仍然将其值显示为false。如果焦点丢失但是使用 JCheckBox
仍然检查然后返回true。
,当我选中2复选框时,结果显示是第一个复选框。未显示第二个复选框的状态。完成编程如下所示:请运行编程并纠正我的错误。欢迎任何帮助。
JCheckBox
is checked but it still shows its value as false when i use system.out.print .If the focus is lost but with JCheckBox
still checked then it returns true.i.e when i check 2 checkbox the result showed is for first checkbox. the status of second checkbox is not shown. complete prog is shown below: pls run the prog and do rectify my mistake. any help is welcome.
public class check extends JFrame {
public check() {
setBounds(00, 40, 400, 400);
Color c = new Color(160, 200, 100);
getContentPane().setBackground(c);
Color c3 = new Color(0, 50, 50, 2);
setTitle("MARKING OF TARGET HABITATION");
setUndecorated(true);
getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String[] columnNames = {"Country", "Capital", "Population in Millions",
"Democracy"};
Object[][] data = {
{"USA", "Washington DC", 280, new Boolean(false)},
{"Canada", "Ottawa", 32, new Boolean(false)},
{"United Kingdom", "London", 60, new Boolean(false)},
{"Germany", "Berlin", 83, new Boolean(false)},
{"France", "Paris", 60, new Boolean(false)},
{"Norway", "Oslo", 4.5, new Boolean(false)},
{"India", "New Deli", 1046, new Boolean(false)}
};
final JTable table = new JTable(data, columnNames) {
public boolean isCellEditable(int rowIndex, int colIndex) {
return (colIndex == 3);//Disallow the editing of any cell
}
};
table.getColumnModel().getColumn(3).setCellEditor(new CheckBoxCellEditor());
table.getColumnModel().getColumn(3).setCellRenderer(new CWCheckBoxRenderer());
JScrollPane scrollPane = new JScrollPane(table);
JButton button = new JButton("check");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
boolean[] rsel = new boolean[table.getRowCount()];
for (int i = 0; i < table.getRowCount(); i++) {
rsel[i] = (boolean) table.getValueAt(i, 3);
if (rsel[i]) {
System.out.println("checkbox selected at row"
+ " " + i + "is" + rsel[i]);
for (int col = 0; col <= 3; col++) {
table.getValueAt(i, col);
System.out.println("data at row" + " "
+ i + "is" + table.getValueAt(i, col));
}
}
}
}
});
JPanel buttonpanel = new JPanel();
buttonpanel.add(button);
add(scrollPane, BorderLayout.CENTER);
add(buttonpanel, BorderLayout.SOUTH);
Color c1 = new Color(160, 200, 100);
table.setBackground(c1);
buttonpanel.setBackground(c1);
setBackground(c1);
setVisible(true);
}
public static void main(String args[]) {
new check();
}
class CheckBoxCellEditor extends AbstractCellEditor implements TableCellEditor {
private static final long serialVersionUID = 1L;
protected JCheckBox checkBox;
public CheckBoxCellEditor() {
checkBox = new JCheckBox();
checkBox.setHorizontalAlignment(SwingConstants.CENTER);
// checkBox.setBackground( Color.white);
}
public Component getTableCellEditorComponent(
JTable table,
Object value,
boolean isSelected,
int row,
int column) {
checkBox.setSelected(((Boolean) value).booleanValue());
return checkBox;
}
public Object getCellEditorValue() {
return Boolean.valueOf(checkBox.isSelected());
}
}
class CWCheckBoxRenderer extends JCheckBox implements TableCellRenderer {
private static final long serialVersionUID = 1L;
Border border = new EmptyBorder(1, 2, 1, 2);
public CWCheckBoxRenderer() {
super();
setOpaque(true);
setHorizontalAlignment(SwingConstants.CENTER);
}
public Component getTableCellRendererComponent(
JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column) {
if (value instanceof Boolean) {
setSelected(((Boolean) value).booleanValue());
//setEnabled(table.isCellEditable(row, column));
setForeground(table.getForeground());
setBackground(table.getBackground());
}
return this;
}
}
}
推荐答案
可能最好覆盖模型中的getColumnClass以返回Boolean。默认渲染器/编辑器处理大小写,您可以看到/使用复选框。
May be it's better to override getColumnClass in the model to return Boolean. Default Renderer/Editor handle the case and you can see/use checkbox.
这篇关于JTable中的JCheckBox值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!