I am developing a Java Swing application that will have several panels in it, one of which is NASA's WorldWind. In this WorldWind panel I need to have a right click menu for various actions. I was successful in adding the right click menu and all is good, until the user resizes the window.
I am not sure what about that action would be causing the right click menu to stop working. The mouse listener is working fine still, but the menu no longer shows up. None of the objects get garbage collected, nothing seems to change.
Here is a quick sample of how my code is set up:
class ClickListener extends MouseAdapter {
public void mouseClicked(MouseEvent e) {
if (SwingUtilities.isRightMouseButton(e) == true) {
showRightClickMenu(e);
} else {
addPoint(e);
}
}
}
void showRightClickMenu(MouseEvent e) {
System.out.println("In Right Click");
if (menu == null) {
setupRightClickMenu();
}
menu.show(e.getComponent(), e.getX(), e.getY());
}
void setupRightClickMenu() {
menu = new JPopupMenu("RightClick");
JMenuItem button1 = new JMenuItem("Show Waypoint Table");
button1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
// ptTable.showTable();
}
});
menu.add(button1);
JMenuItem button2 = new JMenuItem("Clear Waypoints");
button2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
ptLayer.removeAllRenderables();
}
});
menu.add(button2);
}
I can set the size to whatever I want at application launch and the right click works, but once I resize, it fails. Any ideas?
UPDATE
Actually after playing with it a bit more and talking with someone else I am working with, it looks like it may be drawing the menu behind the WorldWind canvas. I am not sure why this is, but I need to make sure it shows in front of it.
I tried changing the JPopupMenu property talked about here, but that does not seem to help.
Related
I am creating a simple text editor and have run into a small problem. I can open and have open several new windows for creating text documents by clicking on the new button in the menu. My problem is if I click on "File" then "Exit" from the menu it closes all the windows not just the one I wanted closed. How do I go about making it so that it will close just the chosen window and not the entire application.
Here's some code:
public void actionPerformed(ActionEvent event) {
if(event.getSource() == newFile) {
new EditorGUI();
} else if(event.getSource() == openFile) {
JFileChooser open = new JFileChooser();
open.showOpenDialog(null);
File file = open.getSelectedFile();
openingFiles(file);
} else if(event.getSource() == exit) {
System.exit(0);
}
}
It works however when I click on the X at the top right hand corner of the window:
private JFrame createEditorWindow() {
editorWindow = new JFrame("JavaEdit");
editorWindow.setVisible(true);
editorWindow.setExtendedState(Frame.MAXIMIZED_BOTH);
editorWindow.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
// Create Menu Bar
editorWindow.setJMenuBar(createMenuBar());
editorWindow.add(scroll, BorderLayout.CENTER);
editorWindow.pack();
// Centers application on screen
editorWindow.setLocationRelativeTo(null);
return editorWindow;
}
Screenshot:
Don't use multiple frames. An application should only have a single main JFrame.
Then the child window should be a JDialog. A JDialog will only support DISPOSE_ON_CLOSE so you don't have to worry about exiting the application.
This also seems to do the trick:
if(event.getSource() == exit) {
editorWindow.dispose();
}
You could code something like this:
private JFrame createEditorWindow() {
JFrame editorWindow = new JFrame("JavaEdit");
editorWindow.setExtendedState(JFrame.MAXIMIZED_BOTH);
editorWindow.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
editorWindow.addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent event) {
exitProcedure();
}
});
// Create Menu Bar
editorWindow.setJMenuBar(createMenuBar());
editorWindow.add(scroll, BorderLayout.CENTER);
editorWindow.pack();
// Centers application on screen
editorWindow.setLocationRelativeTo(null);
editorWindow.setVisible(true);
return editorWindow;
}
public void exitProcedure() {
editorWindow.dispose();
}
You perform the exitProcedure in your JMenuUtem action listener for Exit.
I'm trying to create system tray icon with two popup menus, one should be called with left mouse button and another with right. AWT offers simple way to create PopupMenu but it's not posible to call menu with left mouse button without making invisible JFrame or something. So, I find out that swing JPopupMenu can be called by any of mouse buttons. But JPopupMenu have bug (I don't know if this is really a bug or I'm not so good in Java) that it's not hiding when I press mouse outside JPopupMenu. I've tried to use mouse listener's function mouseExited, but it works only on JPopupMenu border. If mouse leaves JPopupMenu border it hides and I can't press any of JPopupMenu buttons. Maybe, anyone had the same problem and could help me find out how to make it work right.
public static JPopupMenu jpm;
public static TrayIcon ti;
public static void main(String args[]) throws IOException, AWTException,
ClassNotFoundException, InstantiationException,
IllegalAccessException, UnsupportedLookAndFeelException {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SystemTray st = SystemTray.getSystemTray();
Image icon = ImageIO.read(SipLogin.class
.getResource("/resources/phone-yellow-small.png"));
ti = new TrayIcon(icon, "Sip login", null);
jpm = new JPopupMenu();
JMenuItem jmi1 = new JMenuItem("JMenuItem1");
JMenuItem jmi2 = new JMenuItem("JMenuItem2");
JMenuItem jmi3 = new JMenuItem("JMenuItem3");
JMenuItem jmi4 = new JMenuItem("JMenuItem4");
ti.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger()) {
jpm.setLocation(e.getX(), e.getY());
jpm.setInvoker(jpm);
jpm.setVisible(true);
}
}
});
jpm.add(jmi1);
jpm.add(jmi2);
jpm.add(jmi3);
jpm.add(jmi4);
jpm.addMouseListener(new MouseAdapter() {
public void mouseExited(MouseEvent e) {
jpm.setVisible(false);
}
});
st.add(ti);
}
Take that one mouse listener and separate the left and right click:
Change from
ti.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger()) {
jpm.setLocation(e.getX(), e.getY());
jpm.setInvoker(jpm);
jpm.setVisible(true);
}
}
});
to
ti.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
if(e.getclickCount < 2){
if(e.getButton().equals(MouseEvent.Button1){
showPopup1();
}
if(e.getButton().equals(MouseEvent.Button3){
showPopup2();
}
}
}
});
thanks for very useful conversation, but I need to add some words:
Sometimes when you add menuItems the JPopupMenu behaves in unpredictable manner (it is not closed, when you mouseover your mouse from it).
In this case you need to delete the mouse motion listeners from your menu items
JMenuItem jmi1 = new JMenuItem("JMenuItem1");
jmi1.removeMouseMotionListener(jmi1.getMouseMotionListeners()[0]);
jmi1.removeMouseListener(jmi1.getMouseListeners()[0]);
I am trying to change the background of a JButton after it is clicked. Currently my buttons are located in a GridLayout (3x3) and look like this:
tiles.add(new JButton(new AbstractAction("") {
#Override
public void actionPerformed(ActionEvent e) {
this.setIcon("foo.png");
}
}));
This does not work. How do I manipulate the background of the image from within the actionperformed?
A JToggleButton might be ideal for this, as shown on Swing JToolbarButton pressing
Note that you would need to add some code to ensure a button was only clickable once.
Alternately you might use a standard JButton and call AbstractButton.setDisabledIcon(Icon). Disable the button when clicked, and it will flip to the alternate icon.
You can create your own listener that implements the MouseListener. This way, you can control when the background of the button changes (when the mouse is released, pressed, etc). Here is an example
//Add the listener to the button
myButton.addMouseListener(new customActionListener());
//Create the listener
class customActionListener implements MouseListener {
public void mouseExited(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
Icon icon = new ImageIcon("icon.gif");
myButton.setIcon(icon);
}
public void mouseClicked(MouseEvent e) {
}
}
At whatever point you want to set the background back to its default, use:
myButton.setIcon(new ImageIcon());
I am trying to write a Minesweeper clone in Java for fun. I have a grid of JButtons whose labels I will change to represent the danger count, flags, etc.
My problem is, I don't know how to get a right click on a JButton to depress the button. I've done the following:
button.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
boolean mine = field.isMine(x, y);
if (e.isPopupTrigger()) {
button.setText("F");
}
else {
if (mine) {
button.setText("X");
}
}
}
});
This doesn't seem to be working at all; the "F" is never shown, only the "X" part. But more importantly, this does nothing for depressing the button.
EDIT: Macs have popup trigger happen on mousePress, not mouseClick.
EDIT: Here's the solution I worked out based off of accepted answer:
button.addMouseListener(new MouseAdapter(){
boolean pressed;
#Override
public void mousePressed(MouseEvent e) {
button.getModel().setArmed(true);
button.getModel().setPressed(true);
pressed = true;
}
#Override
public void mouseReleased(MouseEvent e) {
//if(isRightButtonPressed) {underlyingButton.getModel().setPressed(true));
button.getModel().setArmed(false);
button.getModel().setPressed(false);
if (pressed) {
if (SwingUtilities.isRightMouseButton(e)) {
button.setText("F");
}
else {
button.setText("X");
}
}
pressed = false;
}
#Override
public void mouseExited(MouseEvent e) {
pressed = false;
}
#Override
public void mouseEntered(MouseEvent e) {
pressed = true;
}
});
add(button);
Minesweeper clone http://grab.by/1y9z
Button can't be pressed by right click. Add such a lines to you mouse listener
mousePressed:
if(isRightButtonPressed) {underlyingButton.getModel().setPressed(true));
mouseReleased:
if(needReset) {underlyingButton.getModel().setPressed(false));
or do there whatever want.
I wouldn't use isPopupTrigger but directly check for the right button:
button.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
boolean mine = field.isMine(x, y);
if (e.getButton() == MouseEvent.BUTTON2) {
button.setText("F");
}
...
Just a small addition: the simplest way to check for the right button is SwingUtilities.isRightMouseButton
As you have mentioned that checking for "mousePressed" solved your issue. And the Javadoc of isPopupTrigger would explain the need for this:
public boolean isPopupTrigger()
...
Note: Popup menus are triggered differently on different systems. Therefore, isPopupTrigger should be checked in both mousePressed and mouseReleased for proper cross-platform functionality.
Also see the section on The Mouse Listener API in the Java Swing tutorial.
MouseEvent has some properties
static int BUTTON1
static int BUTTON2
static int BUTTON3
among others. Check those when your event fires.
EDIT
public int getButton()
Returns which, if any, of the mouse buttons has changed state.
The button being visibly depressed on right click isn't part of the "normal" behavior of buttons. You may be able to fake it using JToggleButtons, or simply changing the button's background color and maybe border while the right mouse button is being held down.
If you are certain that the event is properly being triggered (debug FTW!) and that the button.setText("F") is happening, then perhaps you simply need to repaint.
Repaint the button.
http://java.sun.com/javase/6/docs/api/javax/swing/JComponent.html#repaint(java.awt.Rectangle)
This works for me fine on Mac:
import java.awt.event.*;
import javax.swing.*;
public class ButtonTest extends JFrame {
JButton button;
public ButtonTest() {
button = new JButton("W");
button.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getButton() == 3) { // if right click
button.setText("F");
button.getModel().setPressed(false);
// button.setEnabled(true);
} else {
button.setText("X");
button.getModel().setPressed(true);
// button.setEnabled(false);
}
}
});
this.add(button);
this.setVisible(true);
}
public static void main(String[] args) {
new ButtonTest();
}
}
You might as well check for e.getButton() == 2 but I don't know when this one is triggered on Macs.
I needed a JButton with an attached dropdown style menu. So I took a JPopupMenu and attached it to the JButton in the way you can see in the code below. What it needs to do is this:
show the popup when clicked
hide it if clicked a second time
hide it if an item is selected in the popup
hide it if the user clicks somewhere else in the screen
These 4 things work, but because of the boolean flag I'm using, if the user clicks somewhere else or selects an item, I have to click twice on the button before it shows up again. That's why I tried to add a FocusListener (which is absolutely not responding) to fix that and set the flag false in these cases.
EDIT: Last attempt in an answer post...
Here are the listeners: (It's in a class extending JButton, so the second listener is on the JButton.)
// Show popup on left click.
menu.addFocusListener(new FocusListener() {
#Override
public void focusLost(FocusEvent e) {
System.out.println("LOST FOCUS");
isShowingPopup = false;
}
#Override
public void focusGained(FocusEvent e) {
System.out.println("GAINED FOCUS");
}
});
addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("isShowingPopup: " + isShowingPopup);
if (isShowingPopup) {
isShowingPopup = false;
} else {
Component c = (Component) e.getSource();
menu.show(c, -1, c.getHeight());
isShowingPopup = true;
}
}
});
I've been fighting with this for way too long now. If someone can give me a clue about what's wrong with this, it would be great!
Thanks!
Code:
public class Button extends JButton {
// Icon.
private static final ImageIcon ARROW_SOUTH = new ImageIcon("ArrowSouth.png");
// Unit popup menu.
private final JPopupMenu menu;
// Is the popup showing or not?
private boolean isShowingPopup = false;
public Button(int height) {
super(ARROW_SOUTH);
menu = new JPopupMenu(); // menu is populated somewhere else
// FocusListener on the JPopupMenu
menu.addFocusListener(new FocusListener() {
#Override
public void focusLost(FocusEvent e) {
System.out.println("LOST FOCUS");
isShowingPopup = false;
}
#Override
public void focusGained(FocusEvent e) {
System.out.println("GAINED FOCUS");
}
});
// ComponentListener on the JPopupMenu
menu.addComponentListener(new ComponentListener() {
#Override
public void componentShown(ComponentEvent e) {
System.out.println("SHOWN");
}
#Override
public void componentResized(ComponentEvent e) {
System.out.println("RESIZED");
}
#Override
public void componentMoved(ComponentEvent e) {
System.out.println("MOVED");
}
#Override
public void componentHidden(ComponentEvent e) {
System.out.println("HIDDEN");
}
});
// ActionListener on the JButton
addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("isShowingPopup: " + isShowingPopup);
if (isShowingPopup) {
menu.requestFocus();
isShowingPopup = false;
} else {
Component c = (Component) e.getSource();
menu.show(c, -1, c.getHeight());
isShowingPopup = true;
}
}
});
// Skip when navigating with TAB.
setFocusable(true); // Was false first and should be false in the end.
menu.setFocusable(true);
}
}
Here's a variant of Amber Shah's "big hack" suggestion I just made. Without the isShowingPopup flag...
It's not bulletproof, but it works quite well until someone comes in with an incredibly slow click to close the popup (or a very fast second click to reopen it...).
public class Button extends JButton {
// Icon.
private static final ImageIcon ARROW_SOUTH = new ImageIcon("ArrowSouth.png");
// Popup menu.
private final JPopupMenu menu;
// Last time the popup closed.
private long timeLastShown = 0;
public Button(int height) {
super(ARROW_SOUTH);
menu = new JPopupMenu(); // Populated somewhere else.
// Show and hide popup on left click.
menu.addPopupMenuListener(new PopupMenuListener() {
#Override
public void popupMenuWillBecomeInvisible(PopupMenuEvent arg0) {
timeLastShown = System.currentTimeMillis();
}
#Override public void popupMenuWillBecomeVisible(PopupMenuEvent arg0) {}
#Override public void popupMenuCanceled(PopupMenuEvent arg0) {}
});
addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if ((System.currentTimeMillis() - timeLastShown) > 300) {
Component c = (Component) e.getSource();
menu.show(c, -1, c.getHeight());
}
}
});
// Skip when navigating with TAB.
setFocusable(false);
}
}
As I said in comments, that's not the most elegant solution, but it's horribly simple and it works in 98% of the cases.
Open to suggestions!
Here is another approach which is not too bad of a hack, if not elegant, and which, as far as I could tell, works. First, at the very top, I added a second boolean called showPopup.
The FocusListener has to be as follows:
menu.addFocusListener(new FocusListener() {
#Override
public void focusLost(FocusEvent e) {
System.out.println("LOST FOCUS");
isShowingPopup = false;
}
#Override
public void focusGained(FocusEvent e) {
System.out.println("GAINED FOCUS");
isShowingPopup = true;
}
});
The isShowingPopup boolean does not get changed anywhere else--if it gains focus, it assumes it's shown and if it loses focus, it assumes it isn't.
Next, the ActionListener on the button is different:
addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("isShowingPopup: " + isShowingPopup);
if (showPopup) {
Component c = (Component) e.getSource();
menu.show(c, -1, c.getHeight());
menu.requestFocus();
} else {
showPopup = true;
}
}
});
Now comes the really new bit. It's a MouseListener on the button:
addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
System.out.println("ispopup?: " + isShowingPopup);
if (isShowingPopup) {
showPopup = false;
}
}
#Override
public void mouseReleased(MouseEvent e) {
showPopup = true;
}
});
Basically, mousePressed gets called before the menu loses focus, so isShowingPopup reflects whether the popup was shown before the button is pressed. Then, if the menu was there, we just set showPopup to false, so that the actionPerformed method does not show the menu once it gets called (after the mouse is let go).
This behaved as expected in every case but one: if the menu was showing and the user pressed the mouse on the button but released it outside of it, actionPerformed was never called. This meant that showPopup remained false and the menu was not shown the next time the button was pressed. To fix this, the mouseReleased method resets showPopup. The mouseReleased method gets called after actionPerformed, as far as I can tell.
I played around with the resulting button for a bit, doing all the things I could think of to the button, and it worked as expected. However, I am not 100% sure that the events will always happen in the same order.
Ultimately, I think this is, at least, worth trying.
You could use the JPopupMenu.isVisible() instead of your Boolean variable to check the current state of the popup menu.
Have you tried adding a ComponentListener to the JPopupMenu, so that you know when it's been shown and hidden (and update your isShowingPopup flag accordingly)? I'm not sure listening for focus changes is necessarily the right approach.
What you need is a PopupMenuListener:
menu.addPopupMenuListener(new PopupMenuListener() {
#Override
public void popupMenuWillBecomeVisible(PopupMenuEvent arg0) {
}
#Override
public void popupMenuWillBecomeInvisible(PopupMenuEvent arg0) {
System.out.println("MENU INVIS");
isShowingPopup = false;
}
#Override
public void popupMenuCanceled(PopupMenuEvent arg0) {
System.out.println("MENU CANCELLED");
isShowingPopup = false;
}
});
I inserted this into your code and verified that it works.
Well, I can't be sure without seeing all of your code, but is it possible that the popup never actually gets focus at all? I've had problems with things' not getting focus properly in Swing before, so it could be the culprit. Try calling setFocusable(true) on the menu and then calling requestFocus() when you make the menu appear.
I tried the Answer of Tikhon Jelvis (introducing a smart combination of focusListener and mouseListener). It does not work for me on Linux (Java7/gtk). :-(
Reading http://docs.oracle.com/javase/7/docs/api/javax/swing/JComponent.html#requestFocus%28%29 there is written "Note that the use of this method is discouraged because its behavior is platform dependent."
It may be that the order of listener calls changed with Java7 or it changes with GTK vs Windows. I would not recommend this solution if you want to be platform independent.
BTW: I created a new account on stackoverflow to give this hint. It seems I am not allowed to comment to his answer (because of reputation). But it seems I have a button to edit it. This stackoverflow is a very funny thing. :-)