When my application loads, which is made using netbeans, the first JTextField is automatically focused, and in this JTextField, I have written "Enter your Username", it will go away when the user clicks on this field, but when the application is loaded, this field is focused, means I can't see "Enter your Username", how to unfocus it on startup ?
A log-in would be best done in a modal dialog, but that introduces problems in that the method requestFocusInWindow() must be called after the component is visible, but that is blocked by the fact the dialog is modal!
This example uses Rob Camick's RequestFocusListener (as presented in Dialog Focus) to manage focus after the dialog is visible.
Note: That is how it appears before the user does anything. The password field is focused by default.
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
public class LoginRequired {
LoginRequired() {
JFrame f = new JFrame("Login Required");
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setResizable(false);
f.setSize(400, 300); // not recommended, but used here for convenience
f.setLocationByPlatform(true);
f.setVisible(true);
showLogin(f);
}
private void showLogin(JFrame frame) {
JPanel p = new JPanel(new BorderLayout(5,5));
JPanel labels = new JPanel(new GridLayout(0,1,2,2));
labels.add(new JLabel("User Name", SwingConstants.TRAILING));
labels.add(new JLabel("Password", SwingConstants.TRAILING));
p.add(labels, BorderLayout.LINE_START);
JPanel controls = new JPanel(new GridLayout(0,1,2,2));
JTextField username = new JTextField("Joe Blogs");
controls.add(username);
JPasswordField password = new JPasswordField();
password.addAncestorListener(new RequestFocusListener(false));
controls.add(password);
p.add(controls, BorderLayout.CENTER);
JOptionPane.showMessageDialog(
frame, p, "Log In", JOptionPane.QUESTION_MESSAGE);
System.out.println("User Name: " + username.getText());
System.out.println("Password: " + new String(password.getPassword()));
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new LoginRequired();
});
}
}
/**
* Convenience class to request focus on a component.
*
* When the component is added to a realized Window then component will
* request focus immediately, since the ancestorAdded event is fired
* immediately.
*
* When the component is added to a non realized Window, then the focus
* request will be made once the window is realized, since the
* ancestorAdded event will not be fired until then.
*
* Using the default constructor will cause the listener to be removed
* from the component once the AncestorEvent is generated. A second constructor
* allows you to specify a boolean value of false to prevent the
* AncestorListener from being removed when the event is generated. This will
* allow you to reuse the listener each time the event is generated.
*/
class RequestFocusListener implements AncestorListener
{
private boolean removeListener;
/*
* Convenience constructor. The listener is only used once and then it is
* removed from the component.
*/
public RequestFocusListener()
{
this(true);
}
/*
* Constructor that controls whether this listen can be used once or
* multiple times.
*
* #param removeListener when true this listener is only invoked once
* otherwise it can be invoked multiple times.
*/
public RequestFocusListener(boolean removeListener)
{
this.removeListener = removeListener;
}
#Override
public void ancestorAdded(AncestorEvent e)
{
JComponent component = e.getComponent();
component.requestFocusInWindow();
if (removeListener)
component.removeAncestorListener( this );
}
#Override
public void ancestorMoved(AncestorEvent e) {}
#Override
public void ancestorRemoved(AncestorEvent e) {}
}
textField.setFocusable(false);
textField.setFocusable(true);
If, and only if, textField had focus, the next component in TAB-order order will get focus automatically. The effect is the same as a TAB press.
(not tested in a GUI with just one focusable component :) )
Use requestFocusInWindow() to set focus on some other component rather then your JTextfield first.
But i'd suggest not to alter the native focus system, rather setText(String s) on the JTextField after initComponents() call in the constructor (assumed to be in netbeans).
Further optional reading: How to Use the Focus Subsystem
I think giving keyboard focus to the username field is the correct behavior, assuming that's what the user needs to do first. Instead of clearing on focus, why not clear only after the user types something?:
import java.awt.*;
import javax.swing.*;
import javax.swing.text.Document;
public class PlaceholderTextField extends JTextField {
public static void main(final String[] args) {
final PlaceholderTextField tf = new PlaceholderTextField("");
tf.setColumns(20);
tf.setPlaceholder("All your base are belong to us!");
final Font f = tf.getFont();
tf.setFont(new Font(f.getName(), f.getStyle(), 30));
JOptionPane.showMessageDialog(null, tf);
}
private String placeholder;
public PlaceholderTextField() {
}
public PlaceholderTextField(
final Document pDoc,
final String pText,
final int pColumns)
{
super(pDoc, pText, pColumns);
}
public PlaceholderTextField(final int pColumns) {
super(pColumns);
}
public PlaceholderTextField(final String pText) {
super(pText);
}
public PlaceholderTextField(final String pText, final int pColumns) {
super(pText, pColumns);
}
public String getPlaceholder() {
return placeholder;
}
#Override
protected void paintComponent(final Graphics pG) {
super.paintComponent(pG);
if (placeholder.length() == 0 || getText().length() > 0) {
return;
}
final Graphics2D g = (Graphics2D) pG;
g.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(getDisabledTextColor());
g.drawString(placeholder, getInsets().left, pG.getFontMetrics()
.getMaxAscent() + getInsets().top);
}
public void setPlaceholder(final String s) {
placeholder = s;
}
}
If you really just want to remove focus, some options:
component.setFocusable(false);
KeyboardFocusManager.getCurrentKeyboardFocusManager().focusNextComponent();
KeyboardFocusManager.getCurrentKeyboardFocusManager().clearGlobalFocusOwner();
Related
I have a main window called MainFrame which is a jForm to which I update the data depending on a timer, but the problem is that I cannot update the data in the same MainFrame after using the jdialog, since I end up creating another duplicate window, but with the data changed, one with the original timer and the other with the new timer, I know that I can close the first window with dispose() and then keep the second, but I would like to avoid changing windows so much
the code with which I create another window when pressing the jDialog button is the following
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
String textoFieldTimer = jTextField1.getText();
int timeUserConfig = Integer.parseInt(textoFieldTimer);
Timer timeDefault = new Timer(timeUserConfig, null);
TokenAccess token = new TokenAccess();
token.access_code = code;
MainFrame mainFrame = new MainFrame(token);
mainFrame.setVisible(true);
mainFrame.timeDefault.stop();
mainFrame.timeDefault = timeDefault;
mainFrame.setUpdateTime(timeUserConfig);
this.dispose();
}//GEN-LAST:event_jButton1ActionPerformed
Is there any alternative to update the window? something like mainFrame.update(); or maybe send the value of the jTextField from the jDialog to mainFrame? since the previous code creates another MainFrame for me.
Method main setLabel and Timer.start/stop
public void setUpdateTime(int timeUserConfig) {
this.timeUserConfig = timeUserConfig;
if (timeUserConfig == 0) {
timeDefault.start();
timeDefault.addActionListener(new java.awt.event.ActionListener() {
#Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
setLabelText();
String timeUserConfigStr = Integer.toString(timeDefaultInt);
tiempoActualizado.setText("Tiempo de Actualizado: " + timeUserConfigStr+"ms");
}
});
} else {
timeDefault.stop();
timeDefault = new Timer(timeUserConfig, null);
timeDefault.start();
timeDefault.addActionListener(new java.awt.event.ActionListener() {
#Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
setLabelText();
String timeUserConfigStr = Integer.toString(timeUserConfig);
tiempoActualizado.setText("Tiempo de Actualizado: " + timeUserConfigStr+"ms");
}
});
}
}
setLabelText is a method set of label
public void setLabelText() {
String humedadStr = String.valueOf(humedad);
String temperaturaStr = String.valueOf(temperatura);
String presionStr = String.valueOf(co2);
temporalHum.setText(humedadStr);
temporalTemperatura.setText(temperaturaStr);
temporalPresion.setText(presionStr);
}
Any help would be appreciated.
Thanks for the update, and I found another solution without using an OptionPane from this question: programmatically close a JPanel which is displayed in JDialog.
I cannot replicate your codings
Start with the MainFrame, assuming you opened the JDialog by clicking on a button and wants to setText() to label lbSomething:
private void btInputActionPerformed(java.awt.event.ActionEvent evt) {
// Open new JDialog when button is clicked
NewJDialog dialog = new NewJDialog(new javax.swing.JFrame, true);
dialog.setVisible(true);
// Get user input from JDialog
String temp = dialog.getInput();
if (temp != null) {
/*
* Perform jButton1ActionPerformed() content here
* Including timeUserConfig, timeDefault and setUpdateTime() here
* so that you don't have to access mainFrame in the JDialog.
*/
lbSomething.setText(temp);
}
}
Then about the JDialog (with simple input detection):
public class NewJDialog extends javax.swing.JDialog {
// Set the variable as class variable
private String textTOFieldTimer;
public NewJDialog(java.awt.Frame parent, boolean modal) {
// default contents
}
#SupressWarinings("unchecked")
private void initComponents() {
// default contents
}
private void btSaveAction Performed(java.awt.event.ActionEvent evt) {
// Check if input correct and whether to disable JDialog
if (tfInput.getText.length() != 0) {
input = tfInput.getText();
// Connect to the whole JDialog by getWindowAncestor()
Window window = SwingUtilities.getWindowAncestor(NewJDialog.this);
// Just setVisible(false) instead of dispose()
window.setVisible(false);
} else {
JOptionPane.showMessageDialog(this, "Wrong Input");
}
}
public String getInput() {
return textToFieldTimer;
}
// default variables declarations
}
Hope this answer helps you well.
Would be better if you displayed the source code, but a simple solution to update values to an existing JFrame is by using setText() and getText().
For example:
String input = JOptionPane.showInputDialog(this, "Nuevo valor");
lbPresionActual.setText(input);
If you created a self-defined JDialog, it is about to transfer the input value when closing the JDialog, and that could be a different question.
I am using (or trying to) use the MVC Design Pattern.
So I have a UI class that just minds its own business and a controller that, well, controlls the view.
When I register a ActionListener in the controller class it does not work. As debugging seems to show, the constructor does not even finsish after calling the constructor of a JDialog.
As describing does not make much senese, here are my classes:
This is the View:
package view;
import javax.swing.*;
import java.awt.*;
/**
* Created by Timbo on 04.05.17.
* This window will be used to authenticate library members and allow them to access staff functions
*/
public class AuthenticationWindow
{
/**
* The window that holds the authentication content
*/
private JDialog _window;
/**
* This Panel holds the fields required for the input of username and password
*/
private JPanel _textFieldPanel;
/**
* This panel will hold the buttons
*/
private JPanel _buttonPanel;
/**
* The textfield in which the username can be entered
*/
private JTextField _userName;
/**
* The field, in which the password can be entered
*/
private JPasswordField _password;
/**
* Holds a string to hint for the username
*/
private JLabel _usernameHint;
/**
* Holds a string to hint for the password
*/
private JLabel _passwordHint;
/**
* A Button that will trigger the login process
*/
private JButton _confirm;
/**
* A Button that will cancel the login process
*/
private JButton _cancel;
/**
* Creates a new Authentication Window in which a user can authenticate to get staff clearance
* #param parent The parent frame of this Window
*/
public AuthenticationWindow(Frame parent)
{
//Initialize all the components
_window = new JDialog(parent, "Please Authenticate", true);
_textFieldPanel = new JPanel(new GridLayout(2, 2));
_buttonPanel = new JPanel(new FlowLayout());
_userName = new JTextField();
_password = new JPasswordField();
_passwordHint = new JLabel("Password");
_usernameHint = new JLabel("Username");
_confirm = new JButton("Confirm");
_cancel = new JButton("Cancel");
//Assemble the textfield panel
_textFieldPanel.add(_usernameHint);
_textFieldPanel.add(_userName);
_textFieldPanel.add(_passwordHint);
_textFieldPanel.add(_password);
//Assemble the button panel
_buttonPanel.add(_cancel);
_buttonPanel.add(_confirm);
//Assemble the window
_window.setLayout(new BoxLayout(_window.getContentPane(), BoxLayout.PAGE_AXIS));
_window.add(_textFieldPanel);
_window.add(_buttonPanel);
//Configure the window
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
_window.setLocation(dim.width/2-_window.getSize().width/2, dim.height/2-_window.getSize().height/2);
_window.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
_window.pack();
//Show window
_window.setVisible(true);
}
//All the getter methods for this class
public JDialog getWindow()
{
return _window;
}
public JTextField getUsernameInput()
{
return _userName;
}
public JPasswordField getPasswordInput()
{
return _password;
}
public JButton getConfirm()
{
return _confirm;
}
public JButton getCancel()
{
return _cancel;
}
}
This is the controller in charge of the view:
package controller.start.viewController;
import view.AuthenticationWindow;
import view.ErrorScreen;
import javax.swing.*;
import java.awt.*;
import java.util.Observable;
/**
* Created by Timbo on 05.05.17.
* This is the controller class for the Authentication window. It handles updates and functionality for the view
*/
public class AuthenticationWindowController extends Observable
{
private static final char[] PASSWORD = {'T', 'o', 'r', 'v', 'a', 'l', 'd', 's'};
/**
* The view that holds the components
*/
private AuthenticationWindow _ui;
/**
* The parent frame of this view
*/
private Frame _parent;
public AuthenticationWindowController(Frame parent)
{
_ui = new AuthenticationWindow(parent);
_parent = parent;
registerListener();
}
private void registerListener()
{
_ui.getConfirm().addActionListener(e -> {checkInput();});
_ui.getCancel().addActionListener(e -> {_ui.getWindow().dispose(); System.out.println("Window disposed");});
}
/**
* This method checks the input and gives feedback to the user in case the input was wrong
* If the input was correct the user is being authenticated as staff and can access the staff view in the start window
*/
private void checkInput()
{
String username = _ui.getUsernameInput().getText();
char[] password = _ui.getPasswordInput().getPassword();
if (username.isEmpty())
{
new ErrorScreenController(_parent, ErrorScreen.USERNAME_ERROR);
}
else if (password.length == 0)
{
new ErrorScreenController(_parent, ErrorScreen.PASSWORD_ERROR);
}
else if ((username.equalsIgnoreCase("linus")) || (password.equals(PASSWORD)))
{
this.setChanged();
this.notifyObservers();
}
else
{
new ErrorScreenController(_parent, ErrorScreen.WRONG_CREDENTIALS);
}
}
}
In the constructor the actionListeners just do not fire.
If I set a breakpoint to the _parent = parent line the program only stops when I close the JDialoge that is the AuthenticationWindow.
The funny thing is I have two other classes that work in this manner. If need be I will post them here. Please let me know.
Thank you already for your help!
Your problem is that the showing of a modal dialog "stops" the current thread, until the dialog will be closed. To avoid this problem you should redesign your application as followed:
1) Remove the line _window.setVisible(true); from constructor of AuthenticationWindow
2) Create a new method in AuthenticationWindow called show
public void show() {
_window.setVisible(true);
}
3) Change the constructor of AuthenticationWindowController
public AuthenticationWindowController(Frame parent)
{
_ui = new AuthenticationWindow(parent);
_parent = parent;
registerListener();
_ui.show();
}
Some further hints:
First of all prefer Action to ActionListener. Secondary: your controller should not work with GUI widgets. So it's better to provide some high level method to your UI like setValidator() to transfer your validation functionality from controller into the UI (in this case you can create and register action directly in the UI). Also make the method to access password and login (not the fileds to input them).
So I have a GUI which acts kind of like a console. I want the user to enter text into a JTextField and press enter. I've used key bindings to make a callback for when the user presses enter.
Now I want to create a method called waitForInput() which waits for the user to enter something and returns it. What I'm trying is below. But it results in a java.lang.IllegalMonitorStateException when notify() is called in the callback function.
public class MainWindow{
private JFrame mainWindow;
private JTextArea textEntry;
private String inputStringMonitor = ""; // lock/user input value
private Boolean stringReady = false; //flag for wait while loop
public MainWindow(){
mainWindow = new JFrame("console");
textEntry = new JTextArea();
// set up key bindings
InputAction = new UserInputAction();
textEntry.getInputMap().put( KeyStroke.getKeyStroke( "ENTER" ),"EnterAction" );
textEntry.getActionMap().put( "EnterAction", InputAction);
//configure window
mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainWindow.setMinimumSize(new Dimension(800,675));
mainWindow.getContentPane().add(textEntry);
mainWindow.pack();
mainWindow.setVisible(true);
}
// callback action when user presses enter
public class UserInputAction extends AbstractAction
{
public void actionPerformed( ActionEvent bp )
{
System.out.println( "enter pressed" );
textEntry.setText("> ");
textEntry.setCaretPosition(2);
synchronized(inputStringMonitor){
stringReady = true;
inputStringMonitor = textEntry.getText();
inputStringMonitor.notify(); //causes exception
}
}
}
public String waitForInput() throws InterruptedException {
String retval = "";
synchronized(inputStringMonitor){
stringReady = false;
System.out.println("waiting");
while(!stringReady){
inputStringMonitor.wait();
}
retval = inputStringMonitor;
}
return retval;
}
}
I think that I have an idea of what you're trying to do, and if so, I feel that I have a better solution. Correct me if I'm wrong, but I think that you want to create a GUI text entry window that other programs can use, and that notifies other programs when text as been entered. If so, then a better solution is to use a tool that is already present within Swing GUI components -- PropertyChangeSupport. If you want to listen for changes in a String's state, then make the String a "bound" property, one that notifies the GUI if its state ever changes by firing the Swing innate property change method. This way outside classes can register as listeners and be notified of this change.
For instance, the class below extends JPanel, partly because this will give the class a SwingPropertyChangeSupport object as well as add/remove PropertyChangeListener methods, but if you don't want to extend the Swing component, you can easily roll your own by adding your own SwingPropertyChangeSupport object as well as add/remove PropertyChangeListener methods to your class.
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;
#SuppressWarnings("serial")
public class CallBackGui extends JPanel {
// public constant for the propertyName
public static final String TEXT_ENTRY = "text entry";
private static final int ROWS = 20;
private static final int COLUMNS = 40;
private static final String CARET_MARKER = "> ";
private JTextArea textEntryArea = new JTextArea(ROWS, COLUMNS);
private String enteredText = ""; // "bound" property
public CallBackGui() {
textEntryArea.setText(CARET_MARKER);
textEntryArea.setWrapStyleWord(true);
textEntryArea.setLineWrap(true);
JScrollPane scrollPane = new JScrollPane(textEntryArea);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
add(scrollPane);
int condition = WHEN_FOCUSED;
InputMap inputMap = textEntryArea.getInputMap(condition);
ActionMap actionMap = textEntryArea.getActionMap();
KeyStroke enterKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
inputMap.put(enterKeyStroke, TEXT_ENTRY);
actionMap.put(TEXT_ENTRY, new TextEntryAction());
}
public String getEnteredText() {
return enteredText;
}
// or can make this private if you wish it to not be changed by outside forces
public void setEnteredText(String enteredText) {
String oldValue = this.enteredText;
String newValue = enteredText;
this.enteredText = enteredText; // change our bound property here
// notify listeners here
firePropertyChange(TEXT_ENTRY, oldValue, newValue);
}
// used by Key Bindings
private class TextEntryAction extends AbstractAction {
#Override
public void actionPerformed(ActionEvent e) {
// call method to set bound poperty
setEnteredText(textEntryArea.getText().substring(CARET_MARKER.length()));
textEntryArea.setText(CARET_MARKER);
}
}
}
Then any outside class that has a reference to the displayed CallBackGui can register a property change listener onto this object and get notification. A very (overly) simple example:
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class TestCallBackGui {
private static void createAndShowGui() {
CallBackGui callBackGui = new CallBackGui();
JFrame frame = new JFrame("CallBackGui");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(callBackGui);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
// add our PropertyChangeListener
callBackGui.addPropertyChangeListener(CallBackGui.TEXT_ENTRY, new PropertyChangeListener() {
#Override
public void propertyChange(PropertyChangeEvent evt) {
System.out.println("Text Entered:");
// result held by newValue
System.out.println(evt.getNewValue());
// or can call callBackGui.getEnteredText()
}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
The benefit -- avoidance of all low-level wait/notify/synchronized code, especially the use of this type of code on the Swing event thread, and instead use of safer higher level constructs. Also, since the Swing component actually uses a SwingPropertyChangeSupport object, all call backs will be made on the Swing event thread, an important point if the listening program is also a Swing GUI.
Okay. So here's a solution thanks to Titus
1) Using synchronized() in the callback blocks the EDT, so that's bad. Instead use the invokeLater() to notify asynchronously.
// thread to branch off in order to notify
Runnable doNotify = new Runnable() {
public void run() {
synchronized(inputStringMonitor){
userString = textEntry.getText();
inputStringMonitor.notify();
}
}
};
// callback function
public void actionPerformed( ActionEvent bp )
{
System.out.println( "enter pressed" );
textEntry.setText("> ");
textEntry.setCaretPosition(2);
SwingUtilities.invokeLater(doNotify);
}
2) Assigning to inputStringMonitor re-initializes the lock and messes things up. Instead use a dedicated lock, and a separate string for storing the actual data.
I have two frames (One main and one popup). On the main frame key & mouse listeners are added. Both work just fine. On the pop up there are 50 buttons. I have one action listener for all of them. It works fine. I also have key & mouse listeners. Mouse works. Key is flaky.
The same keyListener class used in main frame is added to the pop-up frame too.
As soon as the pop up shows, key listener works, once mouse click happens (Action listener kicks in) keylistener stops working. Please help. The code attached is a simplified version
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class key {
private static class toolsAction implements ActionListener {
public void actionPerformed (ActionEvent ae) {
log("Command " + Integer.parseInt(ae.getActionCommand()));
}
}
private static void log(String s) { System.out.println(s); }
private static class keyboardHandler implements KeyListener {
public void keyPressed( KeyEvent e) { log("KB Press called "); }
public void keyReleased(KeyEvent e) { log("KB Release called "); }
public void keyTyped (KeyEvent e) { log("KB Typed called "); }
}
public static void main(String [] args) {
JFrame pFrame = new JFrame("Frame");
pFrame.addKeyListener(new keyboardHandler());
Container pane = pFrame.getContentPane();
pane.setLayout(null);
pane.setVisible(true);
pFrame.setSize(650, 300);
pFrame.setVisible(true);
JButton[] buttons = new JButton[50];
toolsAction action = new toolsAction();
for (int i = 0; i < 50; i++) {
buttons[i] = new JButton("" + i);
buttons[i].setActionCommand("" + i);
buttons[i].addActionListener(action);
pane.add(buttons[i]);
buttons[i].setBounds(((i % 10) * 60), ((i / 10) * 40), 60, 40);
}
}
}
An alternative approach uses Action and key bindings. The example below binds 10 buttons to the number keys, also using the numbers as the MNEMONIC_KEY of each.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/** #see http://stackoverflow.com/a/14182227/230513 */
public class Key {
public static final int SIZE = 10;
private static class ToolAction extends AbstractAction {
public ToolAction(int i) {
super(String.valueOf(i));
putValue(MNEMONIC_KEY, KeyEvent.VK_0 + i);
}
#Override
public void actionPerformed(ActionEvent ae) {
System.out.println(ae.getActionCommand());
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame f = new JFrame("Frame");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new GridLayout(0, SIZE / 2));
for (int i = 0; i < SIZE; i++) {
final ToolAction toolAction = new ToolAction(i);
JButton b = new JButton(toolAction);
String name = b.getText();
b.getInputMap(JButton.WHEN_IN_FOCUSED_WINDOW).put(
KeyStroke.getKeyStroke(KeyEvent.VK_0 + i, 0), name);
b.getActionMap().put(name, toolAction);
f.add(b);
}
f.pack();
f.setVisible(true);
}
});
}
}
The KeyListener will only be executed if the component, to which the listener is added, has the (keyboard) focus. If you click on another component, that component will become the focus and the keyboard events will be sent to its KeyListeners.
Not sure if that is the problem without seeing the code of the popup, but it's the problem in the posted code...
EDIT
you can add an AWTEventListener to the Toolkit to intercept all events independently of the focused component:
private static class AWTListener implements AWTEventListener {
#Override
public void eventDispatched(AWTEvent event) {
log("AWT: " + event);
}
};
...
Toolkit toolkit = Toolkit.getDefaultToolkit();
toolkit.addAWTEventListener(new AWTListener(), AWTEvent.KEY_EVENT_MASK);
I solved this problem by adding MouseMotionListener and in the mouseMoved method calling pFrame.requestFocusInWindow() & also adding focus request in the button's actionPerformed method.
Even with just one button in a frame as long as the ActionListener is added to the button, KeyListener does not work.
This is kludgy at best, but works for me. I would still love to hear from the community why my original code does not work.
I have a swing component that has several sub components. What I want to do change some label if the mouse is over any of those components, and then change it to something else if the mouse moves off all of the components. I'm trying to find a more efficient way to do this.
Currently I have mouse listeners over all of the child components that look something like:
class AMouseListener extends MouseAdapter {
private boolean mouseOver;
mouseEntered(MouseEvent e) { mouseOver = true; updateLabel(); }
mouseExited(MouseEvent e) { mouseOver = false; updateLabel(); }
void updateLabel() {
String text = "not-over-any-components";
// listeners are each of the listeners added to the child components
for ( AMouseListener listener :listeners ) {
if ( listener.mouseOver ) {
text = "over-a-component";
break;
}
}
}
}
This works, but I feel like there should be a better way to handle this by only handling mouseEntered and mouseExited events on the parent container, but since the child components intercept these events, I'm not sure how to go about doing this (I don't necessarily have control over the child components so I Can't forward the mouse events to the parent event if I wanted to).
For example
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
public class TestMouseListener {
public static void main(String[] args) {
final JComboBox combo = new JComboBox();
combo.setEditable(true);
for (int i = 0; i < 10; i++) {
combo.addItem(i);
}
final JLabel tip = new JLabel();
tip.setPreferredSize(new Dimension(300, 20));
JPanel panel = new JPanel();
panel.add(combo);
panel.add(tip);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(panel);
frame.pack();
frame.setVisible(true);
panel.addMouseListener(new MouseAdapter() {
#Override
public void mouseEntered(MouseEvent e) {
tip.setText("Outside combobox");
}
#Override
public void mouseExited(MouseEvent e) {
Component c = SwingUtilities.getDeepestComponentAt(
e.getComponent(), e.getX(), e.getY());
// doesn't work if you move your mouse into the combobox popup
tip.setText(c != null && SwingUtilities.isDescendingFrom(
c, combo) ? "Inside combo box" : "Outside combobox");
}
});
}
private TestMouseListener() {
}
}
Check out the docs and examples for the "glass pane".
This should give you what you need: The Glass Pane
I know this is very old, but here's a simple solution with which you can create a mouse listener for a component and all components inside it's bounds (without adding the listener to all components individually):
/**
* Creates an {#link AWTEventListener} that will call the given listener if
* the {#link MouseEvent} occurred inside the given component, one of its
* children or the children's children etc. (recursive).
*
* #param component
* the component the {#link MouseEvent} has to occur inside
* #param listener
* the listener to be called if that is the case
*/
public static void addRecursiveMouseListener(final Component component, final MouseListener listener) {
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
#Override
public void eventDispatched(AWTEvent event) {
if(event instanceof MouseEvent) {
MouseEvent mouseEvent = (MouseEvent) event;
if(mouseEvent.getComponent().isShowing() && component.isShowing()){
if (containsScreenLocation(component, mouseEvent.getLocationOnScreen())) {
if(event.getID() == MouseEvent.MOUSE_PRESSED) {
listener.mousePressed(mouseEvent);
}
if(event.getID() == MouseEvent.MOUSE_RELEASED) {
listener.mouseReleased(mouseEvent);
}
if(event.getID() == MouseEvent.MOUSE_ENTERED) {
listener.mouseEntered(mouseEvent);
}
if(event.getID() == MouseEvent.MOUSE_EXITED) {
listener.mouseExited(mouseEvent);
}
if(event.getID() == MouseEvent.MOUSE_CLICKED){
listener.mouseClicked(mouseEvent);
}
}
}
}
}
}, AWTEvent.MOUSE_EVENT_MASK);
}
/**
* Checks if the given location (relative to the screen) is inside the given component
* #param component the component to check with
* #param screenLocation the location, relative to the screen
* #return true if it is inside the component, false otherwise
*/
public static boolean containsScreenLocation(Component component, Point screenLocation){
Point compLocation = component.getLocationOnScreen();
Dimension compSize = component.getSize();
int relativeX = screenLocation.x - compLocation.x;
int relativeY = screenLocation.y - compLocation.y;
return (relativeX >= 0 && relativeX < compSize.width && relativeY >= 0 && relativeY < compSize.height);
}
Note: Once the mouse exits the root component of this listener the mouseExited(mouseEvent) will probably not fire, however you can just add the mouse listener to the root component itself and it should fire.
mouseExited(mouseEvent) is unreliable in general though.
You could initiate a single instance of the listener and add that instance to each component.
Like this:
AMouseListener aMouseListener=new AMouseListener();
for each(Component c:components) {
caddMouseListener(aMouseListener);
}