4

My code:

class PanelGlowny extends JPanel implements ActionListener{}

public class Formatka extends JFrame implements ActionListener{

private JMenuItem klienciMenuItem = new JMenuItem("Klienci");
private JPopupMenu menuPopup = new JPopupMenu();
private PanelGlowny panelGlowny = new PanelGlowny();


public Formatka() {

    add(panelGlowny, BorderLayout.CENTER);
    menuPopup.add(klienciMenuItem);
    panelGlowny.setComponentPopupMenu(menuPopup);


   }
}

And i do not see popupmenu when i click right button on mouse. Why?

user1304098
  • 71
  • 1
  • 3
  • 10

3 Answers3

8

Works for me when added to a JPanel.

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;

public class Formatka extends JPanel {

    private JMenuItem klienciMenuItem = new JMenuItem("Klienci");
    private JPopupMenu menuPopup = new JPopupMenu();

    public Formatka() {
        this.add(new JLabel("Right-click for popup menu."));
        menuPopup.add(klienciMenuItem);
        this.setComponentPopupMenu(menuPopup);
    }

    private void display() {
        JFrame f = new JFrame("Formatka");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Formatka().display();
            }
        });
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Thanks, you fix my problem. It wasn't wark because i add Button to my JPanel, when i remove it like in your code all works. Thanks – user1304098 May 28 '12 at 16:52
2

You did not set a Layout, so the component is probably not added to the JFrame. Set a layout to the JFrame with setLayout(new BorderLayout());.

Hidde
  • 11,493
  • 8
  • 43
  • 68
  • That line is important, and we cannot guess if you have put it down. – Hidde May 28 '12 at 14:52
  • The default layout of `PanelGlowny`, derived from `JPanel`, is `FlowLayout`; the default layout of `Formatka`, derived from `JFrame` is `BorderLayout`. I'm not sure how setting the latter again would help. – trashgod May 28 '12 at 15:45
1

This works with JFrame :D

    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import javax.swing.JMenuItem;
    import javax.swing.JPopupMenu;
    import javax.swing.JFrame;

    public class Popup extends JFrame{
        JMenuItem item1,item2;
        static JPopupMenu pop;

        Popup(){
          item1= new JMenuItem("This is Menu Item");
          item2= new JMenuItem("This is another Menu Item");

          pop= new JPopupMenu();

          MouseListener popListener = new PopupListener();

          pop.add(item1);
          pop.add(item2);

          addMouseListener(popListener);        

          setLocationRelativeTo(null);
          pack();
          setVisible(true);
    }

      public static void main(String a []){
          new Popup();
      }

    }

    class PopupListener extends MouseAdapter{

     public void mousePressed(MouseEvent e) {
          maybeShowPopup(e);
          }

       public void mouseReleased(MouseEvent e) {
        maybeShowPopup(e);
       }

       private void maybeShowPopup(MouseEvent e) {
        if (e.isPopupTrigger()) {
            Popup.pop.show(e.getComponent(),
                    e.getX(), e.getY());
        }
      }

    }
Harsha
  • 377
  • 1
  • 8
  • 21
  • 1
    but you _shouldn't_ do it that old way, at least not without a very good reason ;-) The general rule is to always use the highest abstraction available, which here is setComponentPopup. – kleopatra Feb 02 '14 at 17:20