我想檢查值是否存在或在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 –
您正在以字符串格式打印對象,而不是給定對象的任何關聯變量。 –