private JTable getTRent() {
if (tRent == null) { ...
tRent.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
tDescription.setText(house.getDescripcionMansion(tRent.getSelectedRow()));
image.setIcon(new ImageIcon(BookWindow.class.getResource(
"/images/" + house.getCodeMansion(tRent.getSelectedRow()) + ".png")));
String childrenAllowed = house.getChildrenAllowed(tRent.getSelectedRow());
if (childrenAllowed == "N"){
txKids.setEditable(false);
cbBookHouse.setEnabled(true);
}
}
});
}
return tRent;
}
我在前面的代码中遇到的问题是,除了图像之外,任何条件都已实现,似乎被忽略了。
我只是检查了该值是否被正确读取,并且在控制台中使用
println
时,根据行,我得到了N或Y,但是如果我检查该值,则希望其状态为的组件没有任何反应改变了。还尝试将这些条件放入组件内部,并且同样发生。
例如,方法
getChildrenAllowed()
的代码是这样的 public String getChildrenAllowed(int index) {
return houseRelation.get(index).getChildren();
}
最佳答案
不要将字符串与==
进行比较,请使用equals()
或equalsIgnoreCase()
if ("N".equalsIgnoreCase(childrenAllowed))
见How do I compare Strings in Java