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 724569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:14:15+00:00 2026-05-14T06:14:15+00:00

(Problem occur in Ubuntu only. Works fine in Windows. I don’t know in other

  • 0

(Problem occur in Ubuntu only. Works fine in Windows. I don’t know in other Linux environments)

I have used the approach of the ComponentListener to call focus in JTextField within a dialog, but for this case is just not working, I don’t know why. It shows the focus in the text field and fast change to the button. Run and see:

import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class User {

    private String username = "";
    private String password = "";

    public User() {
    // default constructor
    }

    public User(String username, String password) {
    this.username = username;
    this.password = password;
    }

    /** Create a panel containing the componet and tha label. */
    public JPanel createLabeledComponent(JLabel label, Component comp) {
    GridLayout layout = new GridLayout(2, 1);
    JPanel panel = new JPanel(layout);
    panel.add(label);
    panel.add(comp);
    label.setLabelFor(comp);
    return panel;
    }

    public void showEditDialog() {

    JLabel usernameLbl = new JLabel(username);
    final JTextField usernameField = new JTextField();
    usernameField.setText(username);
    JPanel usernamePnl = createLabeledComponent(usernameLbl, usernameField);

    JLabel passwordLbl = new JLabel(password);
    JPasswordField passwordField = new JPasswordField(password);
    JPanel passwordPnl = createLabeledComponent(passwordLbl, passwordField);

    Object[] fields = { "User:", usernamePnl, "Password:", passwordPnl };

    JOptionPane optionPane = new JOptionPane(fields, JOptionPane.PLAIN_MESSAGE,
        JOptionPane.OK_CANCEL_OPTION, null, null);
    JDialog dialog = optionPane.createDialog("User Data");

    dialog.addComponentListener(new ComponentAdapter() {
        @Override
        public void componentShown(ComponentEvent e) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
            usernameField.requestFocusInWindow();
            }
        });
        }
    });

    dialog.setVisible(true);
    }

    public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
        new User().showEditDialog();
        }
    });
    }

}

Any idea how to solve this?

–update

Everything running on EDT now. Sadly with the same behavior.

By the way, either using the last argument (Object initialValue) of the JOptionPane constructor doesn’t work.

  • 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-05-14T06:14:15+00:00Added an answer on May 14, 2026 at 6:14 am

    I remember having a similar problem, I used the solution found at the bottom of this page:

    https://bugs.java.com/bugdatabase/view_bug?bug_id=5018574

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Note: This problem occurs only on Mac, on Windows works fine. I have a
I have a webpage that plays a 'chime' sound when certain events occur. Problem
I have a problem that occurs only in a production environment, deployed at heroku.
i have a problem with eclipse within ubuntu 10.04 (x86). i installed eclipse by
I have problem with richfaces calendar. I want to see only year and month
Developing a heavily XML-based Java-application, I recently encountered an interesting problem on Ubuntu Linux.
The problem occurs when I do a division operation. I would like to know
The following problem occurs on a machine running Ubuntu 10.04 with the 2.6.32-22-generic kernel:
environment: Ubuntu 9.10 Eclipse eclipse-jee-galileo-SR2-linux-gtk When I login the ubuntu,and run the eclipse, it
My problem occur after getting the session token from Google SECURELY Here's what I

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.