Java system trayicon mouselistener - java

Here is the code. I want to ask why is that mouseClicked for a trayicon works perfectly but mouseEntered won't work at all?
mouseListener = new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
showStage(stage);
EventQueue.invokeLater(new Runnable() {
public void run() {
}
});
}
}
#Override
public void mouseEntered(MouseEvent e) {
// why this cant be triggerd
trayIcon.displayMessage(
"警告", "这是一个警告提示!", TrayIcon.MessageType.WARNING);
}
};
trayIcon.addMouseListener(mouseListener);

After
trayIcon.addMouseListener(mouseListener);
add this line:
trayIcon.addMouseMotionListener(mouseListener);

Related

How do I simplify MouseListener so that I don't have all these unused methods?

Below I have the following code, so that when someone clicks on the "Close", the window will close. Below that is another exit button on the same menu bar, simply for redundancy (it'll be changed later to be something else, but the point stands as follows). My question is, is there any way to make this more simplistic? I mean there are four unused methods for every menu, and I'm going to need to do a few more. Any ideas on how to fix this?
closeFile.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent arg0) {
System.exit(0);
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
});
exit.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent arg0) {
System.exit(0);
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
});
Also, ActionListener wouldn't work for me, so I can't use that (don't believe I'm supposed to either).
Use a MouseAdapter and override the methods that you want.
closeFile.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent arg0) {
System.exit(0);
}
});
closeFile.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e) {
//your code
}
});
Note: You dont have to write 'implements MouseListener' during class definition.
For more information, search for adapter classes, more specifically for MouseAdapter class.

Java Event listeners (mouse and keyboard)

So everything in my program works, except for the mouse and the keyboard listeners.
I've got a couple actionListeners working on Jbuttons that do exactly what I'm trying to do here, but the assignment says it has to work with all three.
So I would like to know why it compiles, but doesn't work. Am I doing something wrong?
panel.addKeyListener(new KeyListener() {
#Override
public void keyTyped(KeyEvent e) {
}
#Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_UP){
shape.addSides();
}
if(e.getKeyCode() == KeyEvent.VK_DOWN){
shape.subSides();
}
}
#Override
public void keyReleased(KeyEvent e) {
}
}
);
panel.addMouseListener(new MouseListener(){
#Override
public void mouseEntered(MouseEvent e){
if(e.getButton() == MouseEvent.BUTTON1){
shape.addSides();
}
if(e.getButton() == MouseEvent.BUTTON3){
shape.subSides();
}
}
#Override
public void mouseClicked(MouseEvent e) {
}
#Override
public void mousePressed(MouseEvent e) {
}
#Override
public void mouseReleased(MouseEvent e) {
}
#Override
public void mouseExited(MouseEvent e) {
}
}
);
Regarding KeyListener: it won't work unless the listened-to component is focusable and has focus. So you will want to call setFocusable(true) and requestFocusInWindow() on your JPanel. As for the MouseListener -- something else may be taking the mouse event and preventing it from reaching your JPanel. To debug this you need to post a minimal, compilable, runnable example program.
Also regarding your MouseListener, you're checking getButton() in a mouseEntered event which makes no sense. The buttons are not involved in this type of event. Are you instead meaning to check mouseDragged(...) of a MouseMotionListener?

What is the difference between registerKeyboardAction(...) and getInputMap().put(...) + getActionMap().put(...)?

What is the difference between these two methods:
getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(
"pressed F10"), "someAction");
getActionMap().put("someAction", new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
//Do something
}
});
and:
registerKeyboardAction(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
//Do something
}
}, KeyStroke.getKeyStroke("pressed F10"), JComponent.WHEN_FOCUSED);
There is no difference, but registerKeyboardAction is obsolete as stated in the javadoc.

Java Swing mouse listener not working

I would need to have a MouseListener active on a SwingX JXDatePicker component, enabling me to perform specific actions when the user clicks on the component.
Unfortunately the event is never triggered.
Hereby a small piece of code that reproduces the problem:
public class TestDummy4 extends JFrame implements MouseListener{
private static final long serialVersionUID = -2424758762078571430L;
public TestDummy4(){
super();
this.getContentPane().setLayout(new BorderLayout());
//Adds date picker
JXDatePicker dp = new JXDatePicker();
dp.getEditor().setEditable(false);
dp.getEditor().setHighlighter(null);
dp.addMouseListener(this);
this.getContentPane().add(dp);
this.pack();
this.setVisible(true);
}
public static void main(String[] args) throws IOException {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
//Builds GUI
new TestDummy4();
}
});
}
#Override
public void mouseClicked(MouseEvent e) {
System.out.println("Mouse clicked");
}
#Override
public void mousePressed(MouseEvent e) {
System.out.println("Mouse pressed");
}
#Override
public void mouseReleased(MouseEvent e) {}
#Override
public void mouseEntered(MouseEvent e) {
System.out.println("Mouse Entered");
}
#Override
public void mouseExited(MouseEvent e) {}
}
With this code, I have not a single line of output on the console when clicking on the JXDatePicker.
Any help / hint would be greatly appreciated!
Thomas
To add a MouseListener to the JXDatePicker's editor component use:
dp.getEditor().addMouseListener(this);
Update:
To add a ActionListener to the component's open JButton you can use:
JButton openButton = (JButton) dp.getComponent(1);
openButton.addActionListener(myActionListener);

java action listener on menu, and not on menu item

I need enlightenment.
how to add action actionListener event bind to the menu, and not bind to the menu ITEM
here is the demo code, that works(on menuITEM)..
menuFileItem.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.out.println("It works");
}
}
);
but when i try the same , but just on the MENU itself it doesn't work!
menuFile.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.out.println("Plz work... :( ");
}
}
);
is it possible to add listener to menu? i taught listener could be added to everything.
You can add an ActionListener to a JMenu as this method is inherited from AbstractButton. (JMenu Documentation)
JMenu menu = new JMenu("MyMenu");
menu.addActionListener(new ActionListener(){...});
But, it is not intended to be used this way: JMenu ignores ActionEvent. You should use MenuEvent and MenuListener instead.
JMenu menu = new JMenu("MyMenu");
menu.addMenuListener(new MenuListener() {
#Override
public void menuSelected(MenuEvent e) {
System.out.println("menuSelected");
}
#Override
public void menuDeselected(MenuEvent e) {
System.out.println("menuDeselected");
}
#Override
public void menuCanceled(MenuEvent e) {
System.out.println("menuCanceled");
}
});
I use this addMouseListener(), because when you click over a JMenu, it marks it blue and executes the event just after.
I think it's that effect that you want.
Here it works using this code:
I declare:
JMenu jmCadastrar = new JMenu("Cadastrar");
jmCadastrar.addMouseListener(new MouseListener() {
#Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
});
}
If you instantiated JMenu class to bulid a menu object, try the method addMenuListener().
based on the previous answers, I added an additional key listener to the solution.
Here is a convenience function:
public static void bind(JMenu menu, ActionListener listener) {
menu.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
listener.actionPerformed(new ActionEvent(e.getSource(), ActionEvent.ACTION_PERFORMED, "Delegate"));
}
});
menu.addMenuKeyListener(new MenuKeyListener() {
#Override
public void menuKeyPressed(MenuKeyEvent e) {
if(e.getKeyCode() != KeyEvent.VK_ENTER){
return;
}
// Find out if the menu is the currently selected menu element
MenuElement[] selectionPath = e.getMenuSelectionManager().getSelectedPath();
// Because the JMenu will open a popup menu, the menu is the penultimate element in the path
if (selectionPath.length < 2) {
return;
}
MenuElement selectedMenu = selectionPath[selectionPath.length - 2];
if (selectedMenu == menu) {
listener.actionPerformed(new ActionEvent(e.getSource(), ActionEvent.ACTION_PERFORMED, "Delegate"));
}
}
#Override
public void menuKeyTyped(MenuKeyEvent e) {}
#Override
public void menuKeyReleased(MenuKeyEvent e) {}
});
}

Categories