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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:47:48+00:00 2026-06-05T22:47:48+00:00

I have a JFrame with some a JTextField and a JButton. I want it

  • 0

I have a JFrame with some a JTextField and a JButton. I want it to behave like JOptionPane.showInputDialog(). Basically, I want to construct it, then call .start() or something which will make it visible and then wait for a button press and then return the contents of the JTextField. I have heard that wait()/notify() might do this, but I don’t know if that’s right for this and if it is, could I see a short example of how to use it?

  • 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-05T22:47:53+00:00Added an answer on June 5, 2026 at 10:47 pm

    Here try this code example using JDialog :

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class DialogExample extends JFrame
    {
        private JLabel nameLabel;
    
        public DialogExample()
        {
            super("Dialog Example");
        }
    
        private void createAndDisplayGUI()
        {       
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            JPanel contentPane = new JPanel();
            nameLabel = new JLabel();
            contentPane.add(nameLabel);
    
            setContentPane(contentPane);
            setSize(200, 100);
            setLocationByPlatform(true);
            setVisible(true);
    
            MyDialog dialog = new MyDialog(this, "Credentials : ", true);
            dialog.createAndDisplayGUI();
        }
    
        public void setName(String name)
        {
            if (name.length() > 0)
                nameLabel.setText(name);
            else
                nameLabel.setText("Empty string received.");
        }
    
    
        public static void main(String... args)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    new DialogExample().createAndDisplayGUI();
                }
            });
        }
    }
    
    class MyDialog extends JDialog
    {
        private JTextField nameField;
        private JFrame frame;
    
        public MyDialog(JFrame f
                , String title, boolean isModal)
        {
            super(f, title, isModal);
            frame = f;
        }
    
        public void createAndDisplayGUI()
        {
            JPanel contentPane = new JPanel();
            contentPane.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
            JLabel nameLabel = new JLabel("Please Enter your Name : ");
            nameField = new JTextField(10);
            JButton submitButton = new JButton("SUBMIT");
            submitButton.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent ae)
                {
                    if (nameField.getDocument().getLength() > 0)
                        frame.setName(nameField.getText());
                    else
                        frame.setName("");
                    MyDialog.this.dispose();    
                }
            });
    
            contentPane.add(nameLabel);
            contentPane.add(nameField);
            contentPane.add(submitButton);
    
            add(contentPane);
            pack();
            setVisible(true);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JFrame and some JButtons , JLabels and a JTextfield . I
I have a JPanel that I want to add some components. in particular JButton
I have in a JFrame some components that I want to refer into another
I was playing with some code to make a "closure like" construct ( not
I have a JTextField as well as a JTextArea in a JFrame. When the
I have a program with some gui, on the JFrame I set, setBackground( new
I have a JFrame panel. It opens several times. However, I want the previous
I have a JFrame that is the main class of the program. I want
I have a problem. I have a JFrame with some JTextFields, JLabels, Jlists &
I have a Main app window (JFrame) with JPanel. On JPanel I have some

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.