Wednesday, November 30, 2016

Create a popup menu

In the case you want to display a set of options once you have clicked a component, you may use a popup menu to show the options.

Lets say you have a JButton, when it is clicked, a menu will popup.

JButton popMenuButton = new JButton("Your Choices');
popMenuButton.addActionListener(new PopupListener());

JPopupMenu popMenu = new JPopupMenu();
JMenuItem choice1 = new JMenuItem();
JMenuItem choice2 = new JMenuItem();

private class PopupListener implements ActionListener {
      public void actionPerformed(ActionEvent e) {
            buildPopupMenu();
      }
}

private void buildPopupMenu() {
      popMenu.setLabel("Options");
   
      choice1.setText("Choice 1");
      choice1.setActionCommand("Choice 1");
      choice1.setMnemonic(1);
      choice1.addActionListener(new ChoiceListener());
      popMenu.add(choice1);

      choice2.setText("Choice 2");
      choice2.setActionCommand("Choice 2");
      choice2.setMnemonic(2);
      choice2.addActionListener(new ChoiceListener());
      popMenu.add(choice2);

      popMenu.setRequestFocusEnabled(true);
      popMenu.setBorderPainted(true);
      popMenu.show(popMenuButton, 5, -60);
}

private class ChoiceListener implements ActionListener {
      public void actionPerformed(ActionEvent e) {
            popMenu.setVisible(false);
            Object obj = e.getSource();
            if (obj == choice1) {
                  //Display result of choice 1
           } else if (obj == choice2) {
                 //Display result of choice 2
           }
      }
}

---------------------------------------------------------------------------------------------------

                        



If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.
            

No comments:

Post a Comment