I have a problem, I have been making a Swing application.
My question is about how to handle Jbutton like a JOptionPane, if it's possible?
I want handle all of the buttons similarly to JOptionpane button, but our message written in main function System.out.println("this line executes...how to prevent..");
This function is to display the message, until Jframe is visible.
Can anyone let me know how to prevent & how to handle button functionality? Especially when it executes further when I click the button.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.Border;
#SuppressWarnings("serial")
public class InputVerifierExample extends JPanel {
public static final Color WARNING_COLOR = Color.red;
private JTextField firstNameField = new JTextField(10);
private JTextField middleNameField = new JTextField(10);
private JTextField lastNameField = new JTextField(10);
JLabel name=new JLabel("Name:");
private JTextField[] nameFields = {
firstNameField,
middleNameField,
lastNameField };
private JLabel warningLabel = new JLabel(" ");
public InputVerifierExample() {
warningLabel.setOpaque(false);
JPanel namePanel = new JPanel();
namePanel.add(name);
MyInputVerifier verifier = new MyInputVerifier();
for (JTextField field : nameFields) {
field.setInputVerifier(verifier);
namePanel.add(field);
}
namePanel.add(new JButton(new SubmitBtnAction()));
setLayout(new BorderLayout());
add(namePanel, BorderLayout.CENTER);
warningLabel.setForeground(Color.red);
add(warningLabel, BorderLayout.NORTH);
}
private class SubmitBtnAction extends AbstractAction {
public SubmitBtnAction() {
super("Submit");
}
#Override
public void actionPerformed(ActionEvent e) {
// first check all fields aren't empty
for (JTextField field : nameFields) {
if (field.getText().trim().isEmpty()) {
return ; // return if empty
}
}
String name = "";
for (JTextField field : nameFields) {
name += field.getText() + " ";
field.setText("");
}
name = name.trim();
JOptionPane.showMessageDialog(InputVerifierExample.this, name, "Name Entered",
JOptionPane.INFORMATION_MESSAGE);
}
}
private class MyInputVerifier extends InputVerifier {
#Override
public boolean verify(JComponent input) {
JTextField field = (JTextField) input;
if (field.getText().trim().isEmpty()) {
warningLabel.setText("Please do not leave this field empty :"+name.getText());
warningLabel.setBackground(WARNING_COLOR);
//firstNameField.setText("sorry");
return false;
}
warningLabel.setText("");
warningLabel.setBackground(null);
return true;
}
}
private static void createAndShowGui() {
JFrame frame = new JFrame("InputVerifier Example");
frame.setSize(200, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new InputVerifierExample());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
createAndShowGui();
System.out.println("this line executes...how to prevent..");
}
}
Basically, you have something like this:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class TestButton {
protected void createAndShowGUI() {
JFrame frame = new JFrame("Test button");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("Click me");
frame.add(button);
frame.setSize(200, 200);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new TestButton().createAndShowGUI();
}
});
System.err.println("Executed once the button has been clicked");
}
}
And you want the line System.err.println("Executed once the button has been clicked"); to be executed when the button is pressed (which is not the case here above).
The solution is actually very simple: you move the code to execute after the button click in another method (see below the proceed() method) and you invoke that line from an ActionListener:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class TestButton {
protected void createAndShowGUI() {
JFrame frame = new JFrame("Test button");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("Click me");
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
proceed();
}
});
frame.add(button);
frame.setSize(200, 200);
frame.setVisible(true);
}
protected void proceed() {
System.err.println("Executed once the button has been clicked");
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new TestButton().createAndShowGUI();
}
});
}
}
Well, the question is not very much clear,but from your comment,you dont want to do any thing till a JButton is clicked? Or you want to preform a task after clicking of a button?
If that is so, dont put your further code inside your main block, call a function from actionPerformed block.Something like this:
public void actionPerformed(ActionEvent e) {
// first check all fields aren't empty
for (JTextField field : nameFields) {
if (field.getText().trim().isEmpty()) {
return ; // return if empty
}
}
String name = "";
for (JTextField field : nameFields) {
name += field.getText() + " ";
field.setText("");
}
name = name.trim();
JOptionPane.showMessageDialog(InputVerifierExample.this, name, "Name Entered",
JOptionPane.INFORMATION_MESSAGE);
display();///////////this is the function containing further code
}
}
//this is display
public void display()
{
System.out.println("this line executes...how to prevent..");
}
Related
I'm making a small java program where I have two JTextFields labeled field1, field2. I have a calculate button as well which initially set to disabled. I want the button only to be enabled when the 2 text boxes have values in them. Currently what i have for the key listener is:
field1.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
if (field1.getDocument().getLength() > 0) {
bt1.setEnabled(true);
}
else {
bt1.setEnabled(false);
}
}
});
Is there a way to include field 2 into the above block? I've tried just copying and pasting the same code block twice but changing the field1 to field2 but that still doesn't work.
Thanks for the help
You really never want to use a KeyListener with a JTextField as this can mess up the JTextField's function. Much better is to use a DocumentListener and give it to both JTextField's Documents.
For example please check out this similar question
Or if you need to be notified of text changes before they are validated, use a DocumentFilter. For more on that, please see this question.
e.g.,
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class DocListenerEg extends JPanel {
private JTextField field1 = new JTextField(10);
private JTextField field2 = new JTextField(10);
private JButton button = new JButton("Button");
public DocListenerEg() {
add(field1);
add(field2);
add(button);
button.setEnabled(false);
DocumentListener docListener = new DocumentListener() {
#Override
public void removeUpdate(DocumentEvent e) {
checkForText();
}
#Override
public void insertUpdate(DocumentEvent e) {
checkForText();
}
#Override
public void changedUpdate(DocumentEvent e) {
checkForText();
}
private void checkForText() {
boolean textOK = !field1.getText().trim().isEmpty() && !field2.getText().trim().isEmpty();
button.setEnabled(textOK);
}
};
field1.getDocument().addDocumentListener(docListener);
field2.getDocument().addDocumentListener(docListener);
}
private static void createAndShowGui() {
DocListenerEg mainPanel = new DocListenerEg();
JFrame frame = new JFrame("DocListenerEg");
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();
}
});
}
}
You current requirement may be for only two text fields, but you should always design to be more flexible and allow any number of text fields. This also allows the code to be reusable.
Something like:
import java.awt.*;
import java.awt.event.*;
import java.util.List;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.event.*;
public class DataEntered implements DocumentListener
{
private JButton button;
private List<JTextField> textFields = new ArrayList<JTextField>();
public DataEntered(JButton button)
{
this.button = button;
}
public void addTextField(JTextField textField)
{
textFields.add( textField );
textField.getDocument().addDocumentListener( this );
}
public boolean isDataEntered()
{
for (JTextField textField : textFields)
{
if (textField.getText().trim().length() == 0)
return false;
}
return true;
}
#Override
public void insertUpdate(DocumentEvent e)
{
checkData();
}
#Override
public void removeUpdate(DocumentEvent e)
{
checkData();
}
#Override
public void changedUpdate(DocumentEvent e) {}
private void checkData()
{
button.setEnabled( isDataEntered() );
}
private static void createAndShowUI()
{
JButton submit = new JButton( "Submit" );
submit.setEnabled( false );
JTextField textField1 = new JTextField(10);
JTextField textField2 = new JTextField(10);
DataEntered de = new DataEntered( submit );
de.addTextField( textField1 );
de.addTextField( textField2 );
JFrame frame = new JFrame("SSCCE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(textField1, BorderLayout.WEST);
frame.add(textField2, BorderLayout.EAST);
frame.add(submit, BorderLayout.SOUTH);
frame.pack();
frame.setLocationByPlatform( true );
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}
I am trying to have the number the user inputs into the frame either multiply by 2 or divide by 3 depending on which button they decide to click. I am having an hard time with working out the logic to do this. I know this needs to take place in the actionperformed method.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Quiz4 extends JFrame ActionListener
{
// Global Variable Declarations
// Our list input fields
private JLabel valueLabel = new JLabel("Enter a value between 1 and 20: ");
private JTextField valueField = new JTextField(25);
// create action buttons
private JButton multiButton = new JButton("x2");
private JButton divideButton = new JButton("/3");
private JScrollPane displayScrollPane;
private JTextArea display = new JTextArea(10,5);
// input number
private BufferedReader infirst;
// output number
private NumberWriter outNum;
public Quiz4()
{
//super("List Difference Tool");
getContentPane().setLayout( new BorderLayout() );
// create our input panel
JPanel inputPanel = new JPanel(new GridLayout(1,1));
inputPanel.add(valueLabel);
inputPanel.add(valueField);
getContentPane().add(inputPanel,"Center");
// create and populate our diffPanel
JPanel diffPanel = new JPanel(new GridLayout(1,2,1,1));
diffPanel.add(multiButton);
diffPanel.add(divideButton);
getContentPane().add(diffPanel, "South");
//diffButton.addActionListener(this);
} // Quiz4()
public void actionPerformed(ActionEvent ae)
{
} // actionPerformed()
public static void main(String args[])
{
Quiz4 f = new Quiz4();
f.setSize(1200, 200);
f.setVisible(true);
f.addWindowListener(new WindowAdapter()
{ // Quit the application
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
} // main()
} // end of class
Here's something simpler, but it essentially does what you want out of your program. I added an ActionListener to each of the buttons to handle what I want, which was to respond to what was typed into the textbox. I just attach the ActionListener to the button, and then in the actionPerformed method, I define what I want to happen.
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class Quizx extends JFrame {
private JPanel panel;
private JTextField textfield;
private JLabel ansLabel;
public Quizx() {
panel = new JPanel(new FlowLayout());
this.getContentPane().add(panel);
addLabel();
addTextField();
addButtons();
addAnswerLabel();
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setTitle("Quiz 4");
this.setSize(220, 150);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setVisible(true);
}
private void addTextField() {
textfield = new JTextField();
textfield.setColumns(9);
panel.add(textfield);
}
private void addButtons() {
JButton multButton = new JButton("x2");
JButton divButton = new JButton("/3");
panel.add(multButton);
panel.add(divButton);
addMultListener(multButton);
addDivListener(divButton);
}
private void addLabel() {
JLabel valueLabel = new JLabel("Enter a value between 1 and 20: ");
panel.add(valueLabel);
}
private void addAnswerLabel() {
ansLabel = new JLabel();
panel.add(ansLabel);
}
private void addMultListener(JButton button) {
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
ansLabel.setText(String.valueOf(Integer.parseInt(textfield.getText().trim()) * 2));
}
});
}
private void addDivListener(JButton button) {
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
ansLabel.setText(String.valueOf(Double.parseDouble(textfield.getText().trim()) /3));
}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Quizx();
}
});
}
}
Hope that helps.
I am supposed to implement an application to the user that has 2 buttons(Increment/decrement) and a label. When increment is pressed the number increases and decreases by one when decrement is pressed. The number starts at 50. I have it to where it shows the buttons and they work, but they work on 2 different variables, so their is 2 number printed to the screen instead of 1. My question is how can i make the button act on only one number. I have seen people use push etc. but is there another way to do this by passing in a value to both or something? Thanks
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ButtonModifier
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
FlowLayout flow = new FlowLayout();
frame.getContentPane().setLayout(flow);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,300);
frame.setTitle("Button Modifier");
IncrementPanel panel = new IncrementPanel();
DecrementPanel panel1 = new DecrementPanel();
frame.add(panel);
frame.add(panel1);
frame.setVisible(true);
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DecrementPanel extends JPanel
{
private JButton button1;
private JLabel label;
private int number = 50;
public DecrementPanel()
{
button1 = new JButton("Decrement");
button1.addActionListener(new /*DecrementPanel.*/ButtonListener());
label = new JLabel("" + number);
this.add(button1);
this.add(label);
}
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
//int increment = 50;
number--;
label.setText("" + number);
}
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class IncrementPanel extends JPanel
{
private JButton button;
private JLabel label;
int number = 50;
public IncrementPanel()
{
button = new JButton("Increment");
button.addActionListener(new ButtonListener());
label = new JLabel("" + number);
this.add(button);
this.add(label);
}
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
//int increment = 50;
number++;
label.setText("" + number);
}
}
}
I am supposed to implement an application to the user that has 2 buttons(Increment/decrement) and a label."
Then why do you have two?
IncrementPanel panel = new IncrementPanel();
DecrementPanel panel1 = new DecrementPanel();
Just use one and change the text on that one
Should be more like this
public class ButtonModifier extends JFrame {
private JLabel numberLabel = new JLable("50");
private JButton decrease = new JButton("-1");
private JButton increase = new JButton("+1");
private static int num = 50;
public ButtonModifier(){
setLayout(new GridLayout(1, 3));
add(increase);
add(numberLabel);
add(decrease);
increase.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
num++;
numLabel.setText("" + num);
}
});
decrease.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
num--;
numLabel.setText("" + num);
}
});
}
public static void main(String[] args){
JFrame frame = ButtonModifier();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,300);
frame.setTitle("Button Modifier");
frame.setVisible(true);
}
}
You should have one JLabel which will display the only number in your program.
Then your two buttons will do operations on that number and update the label.
Your mistake is that each Panel has its own number and its own Label to display the number.
public class ButtonModifier {
private static int number = 50;
private static JLabel label;
public static void main(String[] args) {
JFrame frame = new JFrame();
label = new JLabel("" + number);
// <SNIP>
JButton increment = new JButton("Increment");
increment.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
number++;
label.setText("" + number);
}
}
JButton decrement = new JButton("Increment");
increment.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
number--;
label.setText("" + number);
}
}
frame.add(label);
frame.add(increment);
frame.add(decrement);
frame.setVisible(true);
}
}
An important note: Swing is not thread-safe, and all the operations with GUI components must be performed on Event Dispatch Thread. So your main must actually look this way:
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// Here you create the frame and all the components
}
});
}
Take a look at this program :
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class IncDecApp extends JFrame {
private JButton incBtn = new JButton("Increment");
private JButton decBtn = new JButton("Decrement");
private JPanel lowPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
private JLabel showLbl = new JLabel("00", JLabel.CENTER);
private Font myFont = new Font("Tahoma", Font.BOLD, 60);
private int valueInt;
public IncDecApp() {
setTitle("IncDec Application =)");
setDefaultCloseOperation(EXIT_ON_CLOSE);
lowPanel.add(incBtn);
lowPanel.add(decBtn);
incBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
valueInt = Integer.parseInt(showLbl.getText());
valueInt++;
if (valueInt >= 10) {
showLbl.setText(String.valueOf(valueInt));
} else {
showLbl.setText("0" + String.valueOf(valueInt));
}
}
});
decBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
valueInt = Integer.parseInt(showLbl.getText());
if (valueInt > 0) {
valueInt--;
}
if (valueInt >= 10) {
showLbl.setText(String.valueOf(valueInt));
} else {
showLbl.setText("0" + String.valueOf(valueInt));
}
}
});
showLbl.setFont(myFont);
add(showLbl, BorderLayout.CENTER);
add(lowPanel, BorderLayout.SOUTH);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
new IncDecApp();
}
}
Create the JLabel in the main function. Have the incrementPanel and DecrementPanel classes constructors take a JLabel as an argument that they store as a private variable. The ButtonListeners csn also be passed the JLabel as an argument. Now the button listeners csn update a common JLabel.
Now, you can improve things by combining the code of IncrementPanel and DecrementPanel classes by passing an int in the constructor indicating the increment of +1 or -1.
A quick and dirty way to implement the functionality is through the use of anonymous classes implementing button listeners within a single monolithic class.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class ButtonApplet extends Applet implements ActionListener{
Button buttonInc, buttonDec;
int x=0;
public void init(){
buttonInc=new Button("Increment");
buttonDec=new Button("Decrement");
buttonInc.addActionListener(this);
buttonDec.addActionListener(this);
add(buttonInc);
add(buttonDec);
}
public void paint(Graphics g){
g.drawString("Count is : "+x,50,100);
}
public void actionPerformed(ActionEvent ev){
if(ev.getSource() == buttonInc)
{
x++;
repaint();
}
else if(ev.getSource() == buttonDec){
x--;
repaint();
}
}
}
Make Java GUI Application using AWT
You are required to make one label (Count), one textfield, one button (Increment), one button (Decrement) and one button (Close)
When the increment button is clicked, you need to increment the value in textfield, value should be incremented again and again when the button is clicked
When the decrement button is clicked, you need to decrement the value in textfield, value should be decremented again and again when the button is clicked
When the close button is clicked, you need to close the AWT Frame
Okay I can get text fields and normal text and even images to show but I can not get a button to show. I am not sure what I am doing wrong because I have done the same steps for the rest. Any help would be great thanks!
package EventHandling2;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import EventHandling.GUITest;
public class EventMain extends JFrame{
private JLabel label;
private JButton button;
public static void main(String[] args) {
EventMain gui = new EventMain ();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // when click x close program
//gui.setSize(600, 300);
gui.setVisible(true);
gui.setTitle("Button Test");
}
public void EventMain(){
setLayout(new FlowLayout());
button = new JButton ("click for text");
add(button);
label = new JLabel ("");
add(label);
Events e = new Events();
button.addActionListener(e);
}
public class Events implements ActionListener {
public void actionPerformed(ActionEvent e) {
label.setText("Now you can see words");
}
}
}
The problem is with the method: void EventMain()
Constructor has NO return type. Just remove "void". The code will work just fine.
Your actionListener(e) contains a minor control structure error:
public void actionPerformed(ActionEvent e) {
label.setText("Now you can see words");
}
Change to:
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button) {
label.setText("Now you can see words");
}
}
First off, you have to remove void keyword in EventMain's constructor. Then, creating JPanel and add components into it, then add the JPanel to the JFrame.contentPane.
The following code should work:
public class EventMain extends JFrame {
private final JLabel label;
private final JButton button;
public static void main(String[] args) {
EventMain gui = new EventMain();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // when click x
// close program
gui.setSize(600, 300);
gui.setTitle("Button Test");
gui.setVisible(true);
}
public EventMain() {
// setLayout(new FlowLayout());
JPanel panel = new JPanel(new FlowLayout());
button = new JButton("click for text");
panel.add(button);
label = new JLabel("");
panel.add(label);
Events e = new Events();
button.addActionListener(e);
this.getContentPane().add(panel);
}
public class Events implements ActionListener {
public void actionPerformed(ActionEvent e) {
label.setText("Now you can see words");
}
}
}
I have a couple text fields that I'm tabbing between. On focusLost() I am opening a JOptionPane. I would like the code in focusGained() to be executed AFTER the JOptionPane has been closed. Even though the dialog is modal, focusGained() is being called before the JOptionPane is closed. Is there any way around this?
Found this similar question, but it doesn't seem to have been solved either.
Postpone Event Queue after Focus Lost
Here's a code sample. You'll notice "Focus Gained" is printed before the JOptionPane is closed.
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class ShortTest implements FocusListener
{
private void go()
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JTextField text1 = new JTextField();
text1.setName("text1");
text1.addFocusListener(this);
JTextField text2 = new JTextField();
text2.setName("text2");
text2.addFocusListener(this);
panel.add(new JLabel("tex1"));
panel.add(text1);
panel.add(new JLabel("text2"));
panel.add(text2);
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
}
public static void main(String [] args)
{
ShortTest test = new ShortTest();
test.go();
}
#Override
public void focusGained(FocusEvent e)
{
if (!e.isTemporary() && (e.getSource() instanceof JTextField))
{
System.out.println("Focus Gained: " + ((JTextField)e.getSource()).getName());
}
}
#Override
public void focusLost(FocusEvent e)
{
if (!e.isTemporary() && (e.getSource() instanceof JTextField))
{
JOptionPane.showOptionDialog(null, ((JTextField)e.getSource()).getName() + " lost focus", "Title", JOptionPane.DEFAULT_OPTION, 0, null, null, null);
}
}
}
Perhaps what you want is not a focus listener (a very low-level construct) but rather an input verifier (a higher level construct). This should respond before the focus has shifted. For example, in the code below the verifier reacts if the user tries to enter non-numeric data into the text field. Yes this can also be done using a DocumentFilter.
import javax.swing.*;
public class VerifierEg extends JPanel {
private static final int FIELD_COUNT = 3;
public VerifierEg() {
InputVerifier inputVerifier = new InputVerifier() {
#Override
public boolean verify(JComponent input) {
final JTextField textField = (JTextField) input;
String text = textField.getText();
for (char c : text.toCharArray()) {
if (!Character.isDigit(c)) {
textField.setText("");
JOptionPane.showMessageDialog(VerifierEg.this, "Text: \""
+ text + "\" must hold only digits", "Text Field Error",
JOptionPane.ERROR_MESSAGE);
return false;
}
}
return true;
}
};
for (int i = 0; i < FIELD_COUNT; i++) {
JTextField field = new JTextField(6);
field.setInputVerifier(inputVerifier);
add(field);
}
}
private static void createAndShowGui() {
JFrame frame = new JFrame("Enter Numbers");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new VerifierEg());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
Edit:
The InputVerifier could work for your purpose, even if you're not verifying the input in any specific way. For example, to modify your code:
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.BoxLayout;
import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class ShortTest2 {
private void go() {
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
InputVerifier inputVerifier = new InputVerifier() {
#Override
public boolean verify(JComponent input) {
JOptionPane.showMessageDialog(frame,
"Focus Lost on " + input.getName());
return true;
}
};
FocusListener focusListener = new FocusListener() {
#Override
public void focusLost(FocusEvent e) {
String name = ((JComponent)e.getSource()).getName();
System.out.println("Focus Lost: " + name );
}
#Override
public void focusGained(FocusEvent e) {
String name = ((JComponent)e.getSource()).getName();
System.out.println("Focus Gained: " + name );
}
};
JTextField[] textFields = new JTextField[2];
for (int i = 0; i < textFields.length; i++) {
JTextField textField = new JTextField(10);
String name = "text " + (i + 1);
textField.setName(name);
textField.setInputVerifier(inputVerifier);
textField.addFocusListener(focusListener);
panel.add(new JLabel(name));
panel.add(textField);
}
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
ShortTest2 test = new ShortTest2();
test.go();
}
}
1+ for your SSCCE by the way!