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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:20:10+00:00 2026-06-04T00:20:10+00:00

Is it possible to create a JDialog in swing that would return an object

  • 0

Is it possible to create a JDialog in swing that would return an object when OK button is clicked?

For example the Dialog box has text fields that have components that make up an adress ( street name, country etc.)

When the OK button is clicked an Address object is returned.

Why I thought this to be possible was because of this. But what I want is something like I mentioned above.

Any pointers on how to get this done would be very helpful.

  • 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-04T00:20:12+00:00Added an answer on June 4, 2026 at 12:20 am

    Like the previous people suggested: JOptionPane can help you do what you want. But if you’re determined to implement this from scratch, here is an SSCCE that does exactly what you want (well, it returns a String, but it can be easily modified to suit your needs):

    import java.awt.Font;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    
    class MyDialog
    {
        private JFrame parent;
        private JDialog dialog;
        private String information;
    
        MyDialog (JFrame parent)
        {
            this.parent = parent;
        }
    
        private JPanel createEditBox ()
        {
            JPanel panel = new JPanel ();
    
            JLabel dialogtitlelabel = new JLabel ("Enter Info");
            panel.add (dialogtitlelabel);
            dialogtitlelabel.setFont (new Font ("Arial", Font.BOLD, 20));
    
    
            final JTextArea informationtxt = new JTextArea ();
            informationtxt.setEditable (true);
            informationtxt.setLineWrap (true);
            informationtxt.setWrapStyleWord (true);
    
            JScrollPane jsp = new JScrollPane (informationtxt);
            jsp.setVerticalScrollBarPolicy (ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
            jsp.setHorizontalScrollBarPolicy (ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
            jsp.setPreferredSize (new Dimension (180, 120));
            panel.add (jsp);
    
            JButton btnok = new JButton ("OK");
            panel.add (btnok);
    
            JButton btncancel = new JButton ("Cancel");
            panel.add (btncancel);
    
            btnok.addActionListener (new ActionListener ()
            {
                @Override public void actionPerformed (ActionEvent a)
                {
                    if (informationtxt.getText () == null || informationtxt.getText ().isEmpty ())
                    {
                        return;
                    }
    
                    information = informationtxt.getText ();
    
                    dialog.dispose ();
                }
            });
    
            btncancel.addActionListener (new ActionListener ()
            {
                @Override public void actionPerformed (ActionEvent a)
                {
                    dialog.dispose ();
                }
            });
    
            return panel;
        }
    
        void display ()
        {
            final int DWIDTH = 200;
            final int DHEIGHT = 240;
    
            dialog = new JDialog (parent, "Information", true);
            dialog.setSize (DWIDTH, DHEIGHT);
            dialog.setResizable (false);
            dialog.setDefaultCloseOperation (JDialog.DISPOSE_ON_CLOSE);
    
            dialog.setContentPane (createEditBox ());
    
            dialog.setLocationRelativeTo (parent);
            dialog.setVisible (true);
        }
    
        String getInformation ()
        {
            return information;
        }
    }
    
    public class ReturningDialogTest
    {
        public static void main (String[] args)
        {
            SwingUtilities.invokeLater (new Runnable ()
            {
                public void run ()
                {   
                    final JFrame frame = new JFrame ();
                    frame.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
    
                    final JPanel panel = new JPanel ();
    
                    JButton btn = new JButton ("show dialog");
    
                    panel.add (btn);
    
                    final JLabel lab = new JLabel ("");
    
                    panel.add (lab);
    
                    frame.add (panel);
    
                    btn.addActionListener (new ActionListener ()
                    {
                        public void actionPerformed (ActionEvent e)
                        {
                            MyDialog diag = new MyDialog (frame);
                            diag.display ();
    
                            String info = diag.getInformation ();
    
                            lab.setText (info);
    
                            frame.pack ();
                        }
                    });
    
                    frame.setLocationRelativeTo (null);
                    frame.pack ();
                    frame.setVisible (true);
                }
            });
        }
    }
    

    What you enter in that text-area is displayed on the main window when you press OK, just to prove it works 🙂 .

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

Sidebar

Related Questions

Is it possible to create an image with a link that also has a
is it possible to create a MySQL Username that has permissions to only access
Is it possible to create a UIWebView that has an HTML5 offline appcache pre-populated
It is possible to create getters and setters in javascript as shown by Object.defineProperty
i would like to know if this is possible: create a shared git repository
Possible Duplicate: Create new C++ object at specific memory address? I am writing what
Is it possible to create an SSIS package that can be passed a file
Is it possible to create a Command Link button in Visual C++ (CLR/Windows Forms
is that possible create an abstract class from DbQuery and IDbSet? I'm trying to
Possible Duplicate: Create a date with T-SQL I've a data table that stores each

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.