I want to check if the value does exist or not in Tree
when trying to add node from Tree
. The value does not match in the case that I got Object
instead of String
.
Here is the action code for calling 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");
}
Here are the check method
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