Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8246077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:23:29+00:00 2026-06-07T22:23:29+00:00

I have a button in my dialog. After pressing enter key in that dilog,

  • 0

I have a button in my dialog. After pressing enter key in that dilog, actions assigned to that button is getting called one by one.

public void buttonAction1() { }
....
public void buttonAction2() { }

In my dialog I also have one text field on which focus lost event is listened. Whenever initially focus is inside that textfield, I click tab or using mouse I click on button, focus lost event is called and opens a popup. This pop-up helps to set the value inside text field. This value will be used by action called on button press.

public void focusLostAction() { }

Now the issue is that, when focus is inside text field and I press enter key, focus lost event is not getting called. So Pop-up is not getting opened and and proper values are not set inside that text field. but due to enter key event, action on that perticular button is getting called and this action is unable to find proper value inside text field.

Inside the action method, which are called on enter key event, I have tried to set focus on button manually using:

public void buttonAction1() {
    button.requestFocusInWindow();
}

public void buttonAction2() {
    // do the remaining task
} 

I also tried using, button.requestFocus;

I was expecting that setting focus manually on that button will call loose focus from text field and focus lost event may get called (as this is implemented in separate swing worker thread). But it is not working. Please let me know, If you have faced this issue earlier and the solution for the same.
Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T22:23:30+00:00Added an answer on June 7, 2026 at 10:23 pm

    Here second JTextField is acting the way you want it to. Try your hands on this code example :

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.text.*;
    
    public class TextFieldExample
    {
        private JTextField tfield1;
        private JTextField tfield2;
        private JLabel label;
        private JButton button;
        private FocusListener tfieldListener = new FocusListener()
        {
            @Override
            public void focusGained(FocusEvent fe)
            {
            }
    
            @Override
            public void focusLost(FocusEvent fe)
            {
                System.out.println("I am LOST");
                String num1 = tfield1.getText().trim();
                String num2 = tfield2.getText().trim();
                if (num1 == null || num1.equals(""))
                    num1 = "0";
                if (num2 == null || num2.equals(""))
                    num2 = "0";         
                label.setText(Integer.toString(Integer.parseInt(num1) + Integer.parseInt(num2)));
            }
        };
    
        private void displayGUI()
        {
            JFrame frame = new JFrame("Text Field Focus Example");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            JPanel contentPane = new JPanel();
            contentPane.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
    
            tfield1 = new JTextField(10);
            tfield2 = new JTextField(10);
            tfield2.addActionListener(new ActionListener()
            {
                @Override
                public void actionPerformed(ActionEvent ae)
                {
                    button.requestFocusInWindow();
                }
            });
    
            tfield1.addFocusListener(tfieldListener);
            tfield2.addFocusListener(tfieldListener);
    
            ((AbstractDocument)tfield1.getDocument()).setDocumentFilter(new MyDocumentFilter());
            ((AbstractDocument)tfield2.getDocument()).setDocumentFilter(new MyDocumentFilter());
    
            label = new JLabel("SUM IS");
    
            button = new JButton("USELESS");
    
            contentPane.add(tfield1);
            contentPane.add(tfield2);
            contentPane.add(label);
            contentPane.add(button);
    
            frame.setContentPane(contentPane);
            frame.pack();
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        }
    
        class MyDocumentFilter extends DocumentFilter
        {
            @Override
            public void insertString(FilterBypass fb, int offset
                                                    , String text
                                                    , AttributeSet aset)
            {
                try
                {
                    super.insertString(fb, offset, text.replaceAll("\\D++", ""), aset);
                }
                catch(BadLocationException ble)
                {
                    ble.printStackTrace();
                }
            }
    
            @Override
            public void replace(FilterBypass fb, int offset, int len
                                               , String text
                                               , AttributeSet aset)
            {
                try
                {
                    super.replace(fb, offset, len, text.replaceAll("\\D++", ""), aset);
                }
                catch(BadLocationException ble)
                {
                    ble.printStackTrace();
                }
            }       
        }
    
        public static void main(String... args)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    new TextFieldExample().displayGUI();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form inside a dialog that appears after clicking in another button.
I have a button that opens a dialog when clicked. The dialog displays a
I have a dialog preference with a button on it that I want to
I have a datagrid column with a button that opens a modal dialog box
I have jqgrid and in that I have one custom navigation button to export
I have a link that its href is set.I 've assigned jQuery ui dialog
On my web page I have a button that opens a modal jQuery dialog.
I have a button, on click of which i display a multi-select dialog. I
I have been trying to add an askquestion dialog box to a delete button
I have a program where you click a button, and a dialog pops up

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.