我想检查值是否存在或在Tree
当试图从Tree
添加节点。该值不匹配的情况下,我得到Object
而不是String
。无法获取Object字符串值;需要转换为字符串来比较
这里是动作代码调用existsInTable()
try {
DefaultMutableTreeNode selectedElement = (DefaultMutableTreeNode) TestTree.getSelectionPath().getLastPathComponent();
Object[] row = {selectedElement};
DefaultTableModel model = (DefaultTableModel) myTests_table.getModel();
if (selectedElement.isLeaf() == true && existsInTable(myTests_table, row) == false) {
model.addRow(row);
} else {
JOptionPane.showMessageDialog(null, "Please Choose Test name!", "Error", JOptionPane.WARNING_MESSAGE);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Error");
}
以下是检查方法
public boolean existsInTable(JTable table, Object[] testname) {
int row = table.getRowCount();
for (int i = 0; i < row; i++) {
String str = "";
str = table.getValueAt(i, 0).toString();
if (testname.equals(str)) {
System.out.println(str);
JOptionPane.showMessageDialog(null, "data alreadyexist.", "message", JOptionPane.PLAIN_MESSAGE);
return true;
}
}
return false;
}
the result is this : [Ljava.lang.Object;@11da1f8
but it should be : Test
什么是错误? –
谢谢 结果是这样的:[Ljava.lang.Object; @ 11da1f8 但它应该是:Test –
您正在以字符串格式打印对象,而不是给定对象的任何关联变量。 –