【问题标题】:mouseClicked event not working properlymouseClicked 事件无法正常工作
【发布时间】:2013-12-30 21:59:15
【问题描述】:
 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();
  }

【问题讨论】:

  • private JTable getTRent() { 中的所有内容(来自此处发布的代码)都是无用的,这应该是向 TableModel 添加数据的一部分,真的不知道你的目标是什么

标签: java swing jtable mouselistener


【解决方案1】:

不要将字符串与== 比较,使用equals()equalsIgnoreCase()

if ("N".equalsIgnoreCase(childrenAllowed))

How do I compare Strings in Java

【讨论】:

  • ListSelectionListener(测试是否选择了行)或 rowAtPoint
  • @mKorbel 抱歉,我没明白你的意思。您是否建议 OP 使用ListSelectionListener?您应该将其发布为答案。我不明白 OP 的要求足以尝试提供什么。我刚刚发布了我注意到的错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-30
  • 2021-12-10
相关资源
最近更新 更多