1

i added a ContextMenu to Header of all TitledPanes in an Accordion. it works fine, but how to prevent right Mouse Clicks from Expading or collapsing the TitledPane?

hier is my Code where i show the 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"));

                        }
                    });

UPDATE:

my environment: Linux Suse 12.3, JDK 1.7.0_45, JavaFX 2.2, Compiler Compliance level 1.7.

many thanks

Rami.Q
  • 2,486
  • 2
  • 19
  • 30

3 Answers3

0

try this...

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");
            }
        }
    }
    );
Anshul Parashar
  • 3,096
  • 4
  • 30
  • 56
  • and how to prevent the right click event from expading/collapsing the Contents of the Pane? – Rami.Q Nov 18 '13 at 10:54
  • @Rami.Q i am edited above answer..i hope this work for you :) – Anshul Parashar Nov 18 '13 at 11:45
  • i tried this before, its not working as expected. first time the right mouse clicked, will expande the Pane. and if i activate the expanding again on left clicks , pane expande again. i don't think , thats this is the right way to do it. – Rami.Q Nov 18 '13 at 11:55
  • @Rami.Q i am edited answer one more time. and this was perfectly worked try this – Anshul Parashar Nov 18 '13 at 12:14
  • Anshul Parashar, thank you for you tips, but there are many "errors" in your code, first one the variable p should be final since it is out side the eventhandling, and final variable cannot be assigned. Second "error": we handle mouse events, that means, node is sure allways hovered, so your if(abc.isHover() ) statement is allways true. this means abc is allways expanded. my work around is to hide the title and make my custom TitleBar. many thanks anyway – Rami.Q Nov 18 '13 at 12:51
  • @Rami.Q yep having some error in this. i tried but have issue nevertheless, if i found something better or correct informed you – Anshul Parashar Nov 18 '13 at 12:59
0

Try below code it will work fine as per your requirement:

@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);            
    }
}
Jitendra Pareek
  • 627
  • 7
  • 28
0

This works in Accordion.

@FXML
private Accordion accordion1;

@FXML
private TitledPane pane1,pane2;

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