I have a JPopupMenu that is displayed when a JButton is held down. This menu contains a series of JMenuItems, each associated with an action, that change sometimes. The problem I'm having is that intermitently the JMenuItems aren't being drawn, I simply get a grey JPopupMenu, but the items appear if I move the mouse cursor over them. I thought the problem might be with not properly repainting the components after changes, but testing show the problem keeps happening even when there are no changes to the items. Here is the relevant code:
if (!listChanged) {
myPopupMenu.show(myButton, x, y);
} else {
List<String> menuList = getMenuList();
MyData data = getData();
myPopupMenu.removeAll();
for (int i = 0; i < menuList.size(); i++) {
String name = menuList.get(i);
JMenuItem item = new JMenuItem(new MyMenuAction(this, name,
data, i));
item.addActionListener(this);
myPopupMenu.add(item);
myPopupMenu.validate();
}
myPopupMenu.repaint();
myPopupMenu.show(myButton, x, y);
}
...
private static class MyMenuAction extends AbstractAction {
private MyClass parent;
private int index;
private MyData data;
public MyMenuAction (MyClass parent, String name,
MyData data, int index) {
super(name);
this.parent = parent;
this.index = index;
this.data = data;
}
@Override
public void actionPerformed(ActionEvent e) {
Object[] actionParameters;
try {
actionParameters = data.getParameters(index);
} catch (ImmediateException e1) {
log(e1.getMessage(), "Error");
return;
}
parent.myButtonAction(actionParameters);
}
}
Just to clarify, the actions are working fine and 8 times out of 10 the JPopupMenu and all the JMenuItems are drawn right, but I can't figure out why they don't appear sometimes (regardless of whether the list has changed or not). Any help would be appreciated.
EDIT: Ok, following Andrew Thompson's recomendation, here is a short complete example. Many of the methods have been striped bare, but the basic is still there. Just click and hold the button "SHOW MENU" to display the JPopupMenu. Since the problem is intermitent, it may be necessary to do it several times until the problem occurs.
package main;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JToolBar;
import javax.swing.UIManager;
public class MyClass implements ActionListener, MouseListener {
boolean listChanged = true;
boolean mousePressed = false;
long clickStart;
JPopupMenu myPopupMenu;
JButton myButton;
JFrame myFrame;
ArrayList<String> list;
public static void main(String[] args) {
MyClass myClass = new MyClass();
myClass.start();
}
private void start() {
myFrame = new JFrame();
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
System.out.println(e.getClass().getName() + " " + e.getMessage());
}
startList();
myButton = new JButton("SHOW MENU");
myPopupMenu = new JPopupMenu();
JToolBar toolbar = new JToolBar();
toolbar.add(new JButton("Button1"));
toolbar.add(new JButton("Button2"));
toolbar.add(new JButton("Button3"));
toolbar.add(new JButton("Button4"));
toolbar.add(new JButton("Button5"));
toolbar.add(myButton);
myButton.addMouseListener(this);
toolbar.setBorder(BorderFactory.createEtchedBorder(1));
JPanel emptyPanel = new JPanel();
myFrame.add(toolbar, BorderLayout.PAGE_START);
myFrame.add(emptyPanel, BorderLayout.CENTER);
myFrame.pack();
myFrame.setExtendedState(myFrame.getExtendedState()
| JFrame.MAXIMIZED_BOTH);
myFrame.setVisible(true);
}
public void showMenu() {
if (!listChanged) {
myPopupMenu.show(myButton, 0, myButton.getHeight());
} else {
listChanged = false;
List<String> menuList = getMenuList();
MyData data = getData();
myPopupMenu.removeAll();
for (int i = 0; i < menuList.size(); i++) {
String name = menuList.get(i);
JMenuItem item = new JMenuItem(new MyMenuAction(this, name,
data, i));
item.addActionListener(this);
myPopupMenu.add(item);
myPopupMenu.validate();
}
myPopupMenu.repaint();
myPopupMenu.show(myButton, 0, myButton.getHeight());
}
}
private void startList() {
list = new ArrayList<String>();
list.add("Item 1");
list.add("Item 2");
list.add("Item 3");
list.add("Item 4");
list.add("Item 5");
}
private List<String> getMenuList() {
return list;
}
public void myButtonAction() {
Object[] defaultParameters = getDefaultParameters();
myButtonAction(defaultParameters);
}
private Object[] getDefaultParameters() {
// Placeholder
return null;
}
public void myButtonAction(Object[] actionParameters) {
// Placeholder
}
private MyData getData() {
// Placeholder
return new MyData();
}
private void changeList(List<String> newList) {
list.clear();
list.addAll(newList);
listChanged = true;
}
@Override
public void actionPerformed(ActionEvent e) {
// Placeholder
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
if (e.getSource() == myButton) {
mousePressed = true;
clickStart = System.currentTimeMillis();
new Thread(new Runnable() {
@Override
public void run() {
synchronized (this) {
while (mousePressed)
try {
this.wait(10);
if (System.currentTimeMillis() - clickStart > 300) {
MyClass.this.showMenu();
return;
}
} catch (InterruptedException e1) {
break;
}
MyClass.this.myButtonAction();
}
}
}).start();
}
}
@Override
public void mouseReleased(MouseEvent e) {
mousePressed = false;
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
private static class MyData {
public Object[] getParameters(int index) {
// Placeholder
return null;
}
}
private static class MyMenuAction extends AbstractAction {
private MyClass parent;
private int index;
private MyData data;
public MyMenuAction(MyClass parent, String name, MyData data, int index) {
super(name);
this.parent = parent;
this.index = index;
this.data = data;
}
@Override
public void actionPerformed(ActionEvent e) {
Object[] actionParameters;
try {
actionParameters = data.getParameters(index);
} catch (Exception e1) {
System.out.println(e1.getMessage());
return;
}
parent.myButtonAction(actionParameters);
}
}
}
I have a JPopupMenu that is displayed when a JButton is held down.
First of all I have a problem with a UI like that. The standard is to show a popup when you right click (in windows). Follow known standards. Read the section from the Swing tutorial on Bringing Up a Popup Menu for more information and working examples.
Secondly, I can't reproduce the problem (no matter how long I try). Random problems are usually caused by the GUI not being updated on the EDT. So don't use a Thread.
Instead use a Swing Timer.
Set the Timer
to fire after 200ms at which time you display the menu. Code executed from the Timer IS invoked on the EDT. So you would restart()
the Timer
in mousePressed
. and stop()
the Timer
in mouseReleased
.