0

I am trying to add JPopupMenu (on right click) to an array of check boxes and am doing it in below way:

JPanel Pane = new JPanel();
Pane.setLayout(new BoxLayout(Pane, BoxLayout.PAGE_AXIS));
m_popMenu = new JPopupMenu();
JMenuItem item = new JMenuItem("Setup");
item.addActionListener(this);       
m_popMenu.add(item);
for (int k = 0; k < 5; k++) {
            checkBoxes[k] = new JCheckBox(List[k]); //in List i have names for CheckBoxes
            checkBoxes[k].addActionListener(this);
                        //For popupmenu item
            checkBoxes[k].addMouseListener(this);
            Pane.add(checkBoxes[k]);
            checkBoxes[k].add(m_popMenu);
}

This code does what I need exactly but GUI has some problems. If only two check boxes are there, check boxes gets aligned with more space and if I right click to open popup menu the alignment changes properly.

Whenever I open popup by right click (for the first time) the checkboxes reduce space between them.

Why does this happen?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user3164187
  • 1,382
  • 3
  • 19
  • 50
  • For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve). – Andrew Thompson Jan 20 '14 at 09:12
  • 1
    COuld you try without the adding checkBoxes[k].add(m_popMenu); ? – StanislavL Jan 20 '14 at 09:32
  • @StanislavL in that case there is no problem checkboxes are properly aligned. – user3164187 Jan 20 '14 at 10:10
  • @StanislavL But i need to add popup menu to each checkBox so i added that line of code – user3164187 Jan 20 '14 at 10:11
  • there is Bug in AbstractButton (to **** most of events), is required to wrap (any Top Level Containers and its derivates) into invokeLater – mKorbel Jan 20 '14 at 11:22
  • @StanislavL removing checkBoxes[k].add(m_popMenu); worked but though i have added mouse listener only to checkBoxes[k].addMouseListener(this); the popup comes where i ever i do right click how can i avoid it? – user3164187 Jan 21 '14 at 09:57
  • 1
    You can use public void show(Component invoker, int x, int y)method of JPopupMenu. Pass proper component (appropriate JCheckBox instance or if you need one popup then paretn of the instance) and you can shift it passing x,y – StanislavL Jan 21 '14 at 11:08

0 Answers0