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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:54:51+00:00 2026-06-02T21:54:51+00:00

I have a JDialog which has two fields, username and password. I want to

  • 0

I have a JDialog which has two fields, username and password. I want to make the form like normal ones in which pressing enter will be like pressing continue.

I have already tried getRootPane().setDefaultButton(myButton);, but only that does not seem to 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-06-02T21:54:52+00:00Added an answer on June 2, 2026 at 9:54 pm

    I have already tried getRootPane().setDefaultButton(myButton);, but only that does not seem to work.

    than you have to invoke code for this button with method

    JButton#doClick();

    but better would be use KeyBindings

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.Timer;
    import javax.swing.border.EmptyBorder;
    
    public class Test {
    
        private static final long serialVersionUID = 1L;
        private JDialog dialog = new JDialog();
        private final JPanel contentPanel = new JPanel();
        private Timer timer1;
        private JButton killkButton = new JButton("Kill JDialog");
        private JButton okButton = new JButton("OK");
    
        public Test() {
            contentPanel.setLayout(new FlowLayout());
            contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
            JPanel buttonPane = new JPanel();
            okButton.setActionCommand("OK");
            buttonPane.add(okButton);
            killkButton.addActionListener(new ActionListener() {
    
                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }
            });
            killkButton.setActionCommand("Kill JDialog");
            buttonPane.add(killkButton);
            dialog.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
            dialog.addWindowListener(new WindowListener() {
    
                public void windowOpened(WindowEvent e) {
                }
    
                public void windowClosing(WindowEvent e) {
                    startTimer();
                }
    
                public void windowClosed(WindowEvent e) {
                }
    
                public void windowIconified(WindowEvent e) {
                }
    
                public void windowDeiconified(WindowEvent e) {
                }
    
                public void windowActivated(WindowEvent e) {
                }
    
                public void windowDeactivated(WindowEvent e) {
                }
            });
            dialog.setLayout(new BorderLayout());
            dialog.getRootPane().setDefaultButton(okButton);
            dialog.add(buttonPane, BorderLayout.SOUTH);
            dialog.add(contentPanel, BorderLayout.CENTER);
            dialog.pack();
            dialog.setLocation(100, 100);
            dialog.setVisible(true);
            setKeyBindings();
        }
    
        private void setKeyBindings() {
            okButton.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
                    KeyStroke.getKeyStroke("ENTER"), "clickENTER");
            okButton.getActionMap().put("clickENTER", new AbstractAction() {
    
                private static final long serialVersionUID = 1L;
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    dialog.setVisible(false);
                    startTimer();
                }
            });
        }
    
        private void startTimer() {
            timer1 = new Timer(1000, new AbstractAction() {
    
                private static final long serialVersionUID = 1L;
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    SwingUtilities.invokeLater(new Runnable() {
    
                        @Override
                        public void run() {
                            dialog.setVisible(true);
                        }
                    });
                }
            });
            timer1.setDelay(500);
            timer1.setRepeats(false);
            timer1.start();
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    Test test = new Test();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Undecorated Modal JDialog which I want to setVisible(false) when the user
Hi I have only one JDialog box in my Java application.I want to make
I have created a JDialog which displays two different tables. When the user selects
I have a jdialog and want close it on confirmation after that store the
I have a JDialog that consists of two JPanels, one above the other. Currently,
I have a jDialog with a splitter inside it. One side has a tree
I have a Panel which is a contentPane of a JDialog . That Panel
I have a Main Frame and a JDialog ,I make an object in the
We have an application which, as its first UI action, displays a modal JDialog
I have created a class which extends JDialog, where I have some checkboxes and

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.