Using Java Swing I have 20 JLabels. Each JLabel has a MouseListener and a KeyListener. I'm been trying to come up with a way (with no luck) to be able to know which label the mouse has entered/hovering over and when the delete key is pressed.
For instance, when the delete key is pressed and the mouse is in JLabel 5. I want an action on JLabel 5 to be performed.
I know how to use MouseListener and KeyListener independently, but I don't know how to use them together like that.
Here is what I'm attempting to do.
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_DELETE){
//Get the JLabel that the mouse has entered/hovering over
//Perform action on that JLabel
}
}
In case it matters, I'm using a List for all of the JLabel's.
Suggestions that might surprise you:
Edit For example:
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.PointerInfo;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;
@SuppressWarnings("serial")
public class BindingExample extends JPanel {
private static final int ROWS = 10;
private static final int COLS = 8;
JLabel[][] labels = new JLabel[ROWS][COLS];
public BindingExample() {
setLayout(new GridLayout(ROWS, COLS));
for (int r = 0; r < labels.length; r++) {
for (int c = 0; c < labels[r].length; c++) {
String labelText = String.format("[%d, %d]", r, c);
labels[r][c] = new JLabel(labelText, SwingConstants.CENTER);
int eb = 4;
labels[r][c].setBorder(BorderFactory.createEmptyBorder(eb, eb, eb, eb));
add(labels[r][c]);
}
}
int condition = JComponent.WHEN_IN_FOCUSED_WINDOW;
InputMap inputMap = getInputMap(condition);
ActionMap actionMap = getActionMap();
KeyStroke delKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0);
String delete = "delete";
inputMap.put(delKeyStroke, delete);
actionMap.put(delete, new DeleteAction());
}
private class DeleteAction extends AbstractAction {
@Override
public void actionPerformed(ActionEvent evt) {
PointerInfo pInfo = MouseInfo.getPointerInfo();
Point ptOnScrn = pInfo.getLocation();
int xPanel = getLocationOnScreen().x;
int yPanel = getLocationOnScreen().y;
int x = ptOnScrn.x - xPanel;
int y = ptOnScrn.y - yPanel;
Component component = getComponentAt(x, y);
if (component != null) {
JLabel selectedLabel = (JLabel) component;
System.out.println("Selected Label: " + selectedLabel.getText());
}
}
}
private static void createAndShowGui() {
BindingExample mainPanel = new BindingExample();
JFrame frame = new JFrame("Key Bindings Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
Edit 2
Shoot, just saw the comments to your question, and yeah, a JList would probably be much better for your purposes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With