I want to customize a JLable to make him clickable, i'm not talking about adding an Action Listener because i already have, i'm talking about changing the mouse cursor's reaction when the JLabel gain Focus just like the reaction of a mouse hoover on a link in the browser.
I have impression that is not possible with swing.
How about:
label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
Swing APIs have a way of doing it:
label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
and when you change your mind:
label.setCursor(Cursor.getDefaultCursor());
I have impression that is not possible with swing.
Or maybe you just haven't yet learned how to do this perhaps? A MouseListener and MouseMotionListener have worked great for me.
Edit: and setting the JLabel's curser as noted by Max -- 1+ up-vote for him!
Related
This seems like something really simple and yet I can't find any good solutions online. I am making a simple game and in the main frame I have JButtons. What I would like is for when I click a button it stays looking visually depressed and then when I click it again it "pops" back up. I know I need an ActionListener on the button but I can't figure out the code inside of it. I've seen some discussions referencing "setPressed(true);" but as far as I can tell that is not a method on a JButton object, so I don't know how it's useful. Thanks.
You can use a JToggleButton.
Read the section from the Swing tutorial on How to Use Buttons for more information.
I was rotating my jME3 scene object from Swing program. Unfortunately, rotations does not occur until I click jME3 canvas by mouse.
Is it possible to implement modifications immediately?
Is a question a long time ago and you have probably already found a solution.
I had the same problem and searched very hard for an solution.
For those who have the same problem, here is a link that might help.
http://hub.jmonkeyengine.org/t/swing-canvas-and-events/21352/2
Conclusion:
It is a problem of the lost focus in JME canvas.
You can solve it with 2 line code!
First: choose on appsetting: setPauseOnLostFocus(false);
this make your update methode run also when lost the focus, by clicking on swing components
Second: Also set the focusable property for the other controls in the windows to false with: object.setFocusable(false);
I hope I could help.
Best regards.
EsKay.
I have music Applet which uses keyboard, mouse and GUI buttons. When applet is first loaded keyboard events work fine, as do mouse events. However, after I have pressed one of my GUI buttons the mouse events still work but the keyboard events don't, and don't start working again until I refresh the applet.
After hunting around on the net I found some posible solutions, I've tried adding button.setFocusable(true); and button.addKeyListener(this); to all my buttons, and and my panels. No effect at all. I've seen recommendations for converting to a JApplet and using key binding, but surely there must be a simpler way?
Sorry for the lack of code, I've been working on this project since I was a newbie and it's a bit of a mess, and very long!
Any help much appreciated!
button.setFocusable(true); and button.addKeyListener(this); to all my buttons
For JButton use Swing Action or default implementations for ActionListener, rather than KeyBindings (for Swing based Container and JComponents), nor using KeyListener
EDIT
if isn't there really important reasons, don't use prehistoric AWT Applet, use JApplet, may be sufficient would be plain JFrame
Try to cut the problem area out of your project and put it here. Its highly probable, than while localizing the problem area you'll find some errors already.
If your project is a mess already, then the first and the most important thing you should do, is to order it. If it is a mess for you, that means you don't understand it. So, it simply can't work. That is your first and main error.
I'd like to create a set of buttons in a Java Swing application like you get in a typical tool palette in a paint program. That is, a set of small square buttons, each containing an icon, only one of which is pressed down, and when you press another button, the first is deselected. I've thought of a number of solutions and none of them seem very easy/elegant.
This sounds like a job for JRadioButton, but if you add an Icon to that, you still get the small circle, which is fairly space inefficient. I guess an option would be finding an alternative Look and Feel or painting code for JRadioButton.
Another alternative might be to add JButton to a ButtonGroup, maybe setting a JToggleButton.ToggleButtonModel as the model, but that doesn't have the desired effect, as the painting code for a standard JButton does not keep it depressed when selected. Possibly the JButton code could be modified to do this. Like making it painting "selected" the same way as "pressed".
A third alternative would be to use normal JButton's, and add a common mouse listener that keeps them pressed or not, and communicates the changes between the buttons.
Can anyone advise on the best way to achieve the aim please? A simple method I've missed would be best, but advice on which of these three alternatives would be best and pointers on how to get started would be useful too.
What about a plain JToggleButton in a ButtonGroup? It is not abstract, you can instantiate one with an Icon, and it stays depressed while selected.
See the SwingSet2 demo:
http://java.sun.com/products/plugin/1.4/demos/jfc/SwingSet2/SwingSet2.html
Click the second icon on the toolbar (the one twith the check box and radio button) then tab "Radio buttons". Then click on "Paint Border" on the right panel, under "Display Options".
Source code of the demo is under your JDK install dir, so for example on my PC it's under \jdk1.6.0_01\demo\jfc\SwingSet2\src
Is there a way to globally make right click also select the element that you right click on?
From what I understand this has been a bug in Swing for a long time likely to never be fixed because at this point applications depend on it.
Any advice on doing this on a global scale? Perhaps on the L&F?
Using the Glass Pane will do the trick.
Here's a tutorial on how to use the glass pane to get the right click button and redispatch it to the right component.
As the glass pane is not a solution in this case, I suggest you take a look at the Toolkit class. Specificaly the addAWTEventListener method. You can add a global event listener with it. To add a mouse event listener:
Toolkit.getDefaultToolkit().
addAWTEventListener(listener, AWTEvent.MOUSE_EVENT_MASK);
Cheers