Sobiaholic
Sobiaholic

Reputation: 2957

show JPopupMenu when RightClick on a JTextArea in Java?

I'm creating a simple sticky note app. I want to make a JPopupMenu to show when ever I click on the JTextArea . Because it's a sticky note so obviously the whole app will be a textArea

Short Code:

    //I've tried my best to follow SSCE

private JTextArea textArea = new JTextArea();
private JPopupMenu popup = new JPopupMenu("Popup Menu");
private JMenuItem hideBar = new JMenuItem("Hide Bar");
private JMenuItem hideTitle = new JMenuItem("Hide Item");

public mySticky(){

add(textArea); //Text Area is using the whole Frame "Sticky Note"
popup.add(hideBar);  //adding MenuItem
popup.add(hideTitle); //adding MenuItem
//addMouseListener(new popupTriggerListener());
textArea.addMouseListener(new popupTriggerListener());

}


private class popupTriggerListener extends MouseAdapter{
    public void MousePressed(MouseEvent e){
        if(e.isPopupTrigger())
            popup.show(textArea,e.getX(),e.getY()); //I've added texArea I'm not sure what to add inside.
    }

    public void MouseReleased(MouseEvent e){
        if(e.isPopupTrigger())
            popup.show(textArea,e.getX(),e.getY()); //I've added texArea I'm not sure what to add inside.
    }

    public void MouseClicked(MouseEvent e){

    }
}

Upvotes: 1

Views: 8599

Answers (1)

camickr
camickr

Reputation: 324098

I've tried my best to follow SSCE

How can this possibly be a SSCCE given that the code doesn't even compile? Try reading the link again.

I suggest you start by read the section from the Swing tutorial on Bringing Up a Popup Menu for a working example.

hmmm I'm missing something here

You are missing the @Override statement which should preceed the method signature when you override a method. This will prevent you from making typing mistakes.

Upvotes: 3

Related Questions