Mouse events on JPanel - java

i have mouselisteners on multiple jpanels. I need to detect if the mouse is pressed when it enters on a different jpanel than the one the event has started from. how can i do that?

Either listen on a JPanel that includes the others or see if using Drag and Drop might suit your needs better.

Related

Two JPanels, requesting focus and Keyboard input

Learning GUIs for an assignment, and would appreciate some advice...no code, for the aforementioned reason.
I have a programme with a JFrame, and a single JPanel, that contains methods that take keyboard input using the KeyEvent class.
My class that extends JPanel, has the following in it:
setFocusable(true);
requestFocus();
I've since modified the programme, to include a second JPanel (to add a control panel to the right containing 4 JButtons).
The problem is that the keyboard input no longer works on the first JPanel when I run the programme (it did before).
The keyboard input on the right JPanel only works before the second JPanel has been added to the programme. If I remove the second JPanel, keyboard input works, when it is there it doesn't....
I realised that I had setFocusable(true); and requestFocus(); in both panels, so I deleted it from the second panel (with the JButtons), but it still doesn't work. I sense it's a focus issue.....any advice?
Do I need to look at KeyBindings (don't know what this is yet, but a few similar threads suggest it)....
We can't tell you what is wrong based on two lines of code. We have no idea what panel1 and panel2 do or what components they contain.
My class that extends JPanel, has the following in it:
A couple of problems with that:
You should NOT be using the requestFocus() method. Read the API for that method and it will tell you the appropriate method to use.
Even if you do use the appropriate method, you can't use that method in the constructor of a class. Requesting focus on a component can only be done to visible components on a GUI.
Do I need to look at KeyBindings
Yes, Swing has newer and better API's than AWT. In AWT you didn't have a choice. In Swing you should be using Key Bindings. All Swing components do use Key Bindings and Actions. One of the main reasons is you don't have the focus issue.
Start by reading the Swing tutorial for Swing basics. There are sections on:
How to Use Key Bindings
How to Use Actions to get you started.
A Key Binding is simply the process of mapping a KeyStroke to an Action.
Also, in the future, when you ask a question post a proper SSCCE that demonstrates the problem.
If you mean with "pass back and force" that the focus should move on tab keypress between those two, use a FocusTraversalPolicy where you define who should get focus in which order.
When you click on a button, that button receives the keyboard focus. If you want the focus to revert back to the first JPanel, then you should add an ActionListener to each button, and in the actionPerformed() method of the listener just add the line:
firstPanel.requestFocusINnWindow();

Have KeyListener listen to different JFrame

Okay so long story short, here is what is happening and what I am trying to do.
I have a class called GameGUI, this is a JFrame.
This JFrame is populated with tiles(JLabels with ImageIcons)
I use my arrow keys to move my guy around the JFrame (just updates the JLabel Images)
I made a settings option, this is a new JFrame that I setVisbile(true), make changes, then setVisible(false)
After I setVisible(false) my arrow keys no longer make my character move on GameGUI.
I have tried the following, oh which none work: (all guesses based off Googling my problem)
GameGUI gg = new GameGUI();
gg.setFocusable(true);
gg.addKeyListener(null);
gg.requestFocusInWindow();
I cannot seem to find a way to get my KeyListener to move back to the GameGUI after I open (make visible) this settings menu then close it (make invisible). I do have radio buttons within the settings menu which is why I believe it gains focus, due to a physical mouse click.
Any help on getting focus back onto GameGUI would be much appreciated!
Thanks!
5.After I setVisible(false) my arrow keys no longer make my character move on GameGUI.
Don't use a KeyListener. Swing was designed to be used with Key Bindings.
See Motion Using the Keyboard, which will explain your probable problem and give the solution using Key Bindings.

java key listener doesn't work with multiple panels

I have a JFrame with 1 panel for drawing and set KyeListener for JFrame. It works fine unless I add JTextArea or JTextPane. Looks like it stops responding at all. I need some option to display the text and use keylistener for animation. I don't need anything to type, I just want my keylistener working all the time. I need something better than JLabel, I want some formatting.
KeyListener isn't designated for listening KeyEvents came from keyboard to the Swing GUI,
because KeyListener required focusable JComponent, you have to set for setFocusable() or programatically moving with Focus
Focus / Focus SubSystem is asynchonous
use KeyBindings rather that bothering with Focus / Focus SubSystem
Is there a reason you don't just add the KeyListener to the panel itself?
panel.addKeyListener(this);

Add actionlistener to jpanel

I would like to know if there is any way to add an ActionListener to a JPanel?
I have no problem adding those to JButtons, but JPanel seems not to have such a method.
Basically I have a bunch of JPanels inside a JFrame with a grid layout and I would like to know if there is any way to know when the user has clicked on one of them.
Any help much appreciated!
An action listener serves to listen to action events. A button triggers an action event when it's pressed and released using the mouse (and not when it's clicked), or when it's pressed using the keyboard (space bar, mnemonic, keyboard shortcut, etc.). It's a high-level event. A mouse click is a more low-level event, which is handled by a MouseListener.
Add a MouseListener to your JPanel if you want to handle mouse clicks.
Aparently you can use AddMouseLitener..
Silly me..

Manage multiple Jlabel's events

I have a left Panel with multiples Jlabels which i use them as buttons to change a Main Panel's content which is layouted with a CardLayout.
I cant work perfectly with these events:
mouseEntered : to make highlight effect to the jlabel
mouseExited : to take off the highlight effect.
mouseClicked : to change the content of the main Panel and start some threads
The problem here that can't found an event or a method tell me that another Jlabel has been clicked so i can stop my threads started in the mouseClicked event,
OR
an event or method tell me that a JPanel in the CardLayout has been displayed or hidden.
Your problem is not finding an appropriate event. I think you are doing this using a visual GUI builder and expect to solve everything out-of-the-box. It's not going to work that way, you will need to write some real code. For example, write a method that you will call from the mouse click listener of each of the three JLabels. Thus you will have arranged for this method to be called for each JLabel click. Then in the method do the appropriate handling. This is just a rough outline, you haven't provided much detail to give any further advice.
It sounds like you need FocusEvents and FocusListeners. These are supported by all JComponents like JPanel, JLabel, and JButton, such as by calling addFocusListener();
Basically a FocusListener can tell you when a JComponent gains focus (such as by clicking on the JComponent) and when it looses focus (such as by clicking on a different JComponent).
Refer to http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/event/FocusListener.html for further information

Categories

Resources