Actually i'm writing code. I want to display message dialogue (without pressing any button) when I left my JTextField
but don't know how to do this. Please Help. Im using NetBeans.
You can use Focus Listener API to achieve that.
On focusLost
event, you can show your dialog box.
Example from the documentation:
public void focusLost( FocusEvent e )
{
displayMessage( "Focus lost", e );
}
You can use FocusListener class focusLost() method.
Simple Example:
import java.awt.FlowLayout;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class ExampleClass {
JFrame MainFrame;
JTextField textField1;
JTextField textField2;
public ExampleClass(){
MainFrame = new JFrame("Example");
MainFrame.setLayout(new FlowLayout());
textField1 = new JTextField(10);
textFieldFocus();
textField2 = new JTextField("Dummy text");
MainFrame.add(textField1);
MainFrame.add(textField2);
MainFrame.pack();
MainFrame.setVisible(true);
}
private void textFieldFocus() {
textField1.addFocusListener(new FocusListener() {
@Override
public void focusLost(FocusEvent e) {
JOptionPane.showMessageDialog(null, "Done!");
}
@Override
public void focusGained(FocusEvent e) {}
});
}
public static void main(String[] args) {
new ExampleClass();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.