我试图在解析xml后显示带有复选框的表。但是,当我尝试编辑复选框时,它引发了一个异常,表示无法将String强制转换为boolean。
public class CheckGlobalOptionsPanel{
private JPanel rootPane;
private ConvertXmlToObjects convertXmlToObjects;
@Override
public JComponent getComponent() {
if (rootPane == null) {
rootPane = new JPanel(new BorderLayout());
rootPane.add(createXMLRulePanel(), BorderLayout.CENTER);
}
return rootPane;
}
private JPanel createXMLRulePanel() {
JTable jTable = new JTable();
JScrollPane sp = null;
try {
convertXmlToObjects = new ConvertXmlToObjects();
RulesetType rulesetType = convertXmlToObjects.convertToObjects(null);
DefaultTableModel model = new DefaultTableModel();
jTable.setModel(model);
model.addColumn("Sl. No.");
model.addColumn("Rule Name");
model.addColumn("Message");
model.addColumn("Priority");
model.addColumn("Active");
for (int i = 0; i < rulesetType.getRule().size(); i++) {
model.addRow(new Object[]{i, rulesetType.getRule().get(i).getRef(), rulesetType.getRule().get(i).getMessage(),
rulesetType.getRule().get(i).getPriority(), true});
}
sp = new JScrollPane(jTable);
jTable.setVisible(true);
jTable.setDefaultRenderer(Object.class, new XMLCellRenderer());
} catch (JAXBException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
JPanel xmlPanel = new JPanel(new VerticalFlowLayout());
xmlPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 4, 0));
xmlPanel.add(sp);
return xmlPanel;
}
private class XMLCellRenderer implements TableCellRenderer {
DefaultTableCellRenderer DEFAULT_RENDERER = new DefaultTableCellRenderer();
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
Component objComponent = DEFAULT_RENDERER.getTableCellRendererComponent(table,
value, isSelected, hasFocus, row, column);
JCheckBox jCheckBox = new JCheckBox();
if (column == 4) {
if (value != null) {
jCheckBox.setSelected((Boolean) value);
}
return jCheckBox;
}
return objComponent;
}
}
}
由于最后一列被定义为布尔值,因此我在对象数据的最后传递了一个布尔值。
一旦我开始编辑复选框,它将显示为“ true”的文本。任何建议将不胜感激。