JavaFX:阻止鼠标右键单击以展开/折叠TitledPane

时间:2013-11-17 19:50:58

标签: popup javafx right-mouse-button

我将一个ContextMenu添加到手风琴中所有TitledPanes的标题中。它工作正常,但如何防止鼠标点击从Expading或折叠TitledPane?

hier是我的代码,我展示了ContextMenu:

header.setOnContextMenuRequested(new EventHandler<ContextMenuEvent>() {
                    @Override
                    public void handle(ContextMenuEvent event) {
                        contextMenu.show(header, Side.RIGHT, 0, 0);
                        event.consume();
                            setSelectedAccount((Element)header.getUserData());
                            selectedItem.setText(getSelectedAccount().getAttribute("MAIL"));

                        }
                    });

更新

我的环境:Linux Suse 12.3,JDK 1.7.0_45,JavaFX 2.2,编译器合规性级别1.7。

非常感谢

3 个答案:

答案 0 :(得分:0)

试试这个......

header.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>
    () {

        @Override
        public void handle(MouseEvent t) {
            if(t.getButton().toString().equals("PRIMARY"))
            {
                System.out.println("click on left mouse click");
            }
        }
    }
    );

答案 1 :(得分:0)

尝试下面的代码,它可以根据您的要求正常工作:

@FXML
private TitledPane abc;

@FXML
void mouseClicked(MouseEvent event) {
    if(event.getButton().toString().equals("SECONDARY"))
    {
        if(abc.isExpanded())
            abc.setExpanded(false);
        else
            abc.setExpanded(true);            
    }
}

答案 2 :(得分:0)

这适用于手风琴。

@FXML
private Accordion accordion1;

@FXML
private TitledPane pane1,pane2;

@FXML
void mouseClicked(MouseEvent event) {
        if(pane1.isExpanded())
            pane2.setExpanded(false);
        else
            pane2.setExpanded(true);            
    }