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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:00:08+00:00 2026-05-31T09:00:08+00:00

I have main class with a main GUI from where I want to activate

  • 0

I have main class with a main GUI from where I want to activate and get values from a new class with a JOptionPane like the code below. Since I already have a main GUI window opened, how and where should I activate/call the class below and finally, how do I get the values from the JOptionPane? Help is preciated! Thanks!

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class OptionPaneTest {

    JPanel myPanel = new JPanel();
    JTextField field1 = new JTextField(10);
    JTextField field2 = new JTextField(10);
    myPanel.add(field1);
    myPanel.add(field2);
    JOptionPane.showMessageDialog(null, myPanel);

}

Edit:

InputNewPerson nyPerson = new InputNewPerson();
JOptionPane.showMessageDialog(null, nyPerson);
String test = nyPerson.inputName.getText();
  • 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-31T09:00:10+00:00Added an answer on May 31, 2026 at 9:00 am

    I guess looking at your question, you need something like this. I had made a small JDialog, where you will enter a UserName and Answer, this will then be passed to the original GUI to be shown in the respective fields, as you press the SUBMIT JButton.

    Try your hands on this code and ask any question that may arise :

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    /*
     * This is the actual GUI class, which will get
     * values from the JDIalog class.
     */
    public class GetDialogValues extends JFrame
    {
        private JTextField userField;
        private JTextField questionField;
    
        public GetDialogValues()
        {
            super("JFRAME");
        }
    
        private void createAndDisplayGUI(GetDialogValues gdv)
        {       
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLocationByPlatform(true);
    
            JPanel contentPane = new JPanel();
            contentPane.setLayout(new GridLayout(0, 2));
    
            JLabel userName = new JLabel("USERNAME : ");
            userField = new JTextField();
            JLabel questionLabel = new JLabel("Are you feeling GOOD ?");
            questionField = new JTextField();
    
            contentPane.add(userName);
            contentPane.add(userField);
            contentPane.add(questionLabel);
            contentPane.add(questionField);
    
            getContentPane().add(contentPane);
            pack();
            setVisible(true);
    
            InputDialog id = new InputDialog(gdv, "Get INPUT : ", true);
        }
    
        public void setValues(final String username, final String answer)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    userField.setText(username);
                    questionField.setText(answer);
                }
            });
        }
    
        public static void main(String... args)
        {
            Runnable runnable = new Runnable()
            {
                public void run()
                {
                    GetDialogValues gdv = new GetDialogValues();
                    gdv.createAndDisplayGUI(gdv);
                }
            };
            SwingUtilities.invokeLater(runnable);
        }
    }
    
    class InputDialog extends JDialog
    {
        private GetDialogValues gdv;
        private JTextField usernameField;
        private JTextField questionField;
        private JButton submitButton;
        private ActionListener actionButton = new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                if (usernameField.getDocument().getLength() > 0
                    && questionField.getDocument().getLength() > 0)
                {
                    gdv.setValues(usernameField.getText().trim()
                        , questionField.getText().trim());
                    dispose();
                }
                else if (usernameField.getDocument().getLength() == 0)
                {
                    JOptionPane.showMessageDialog(null, "Please Enter USERNAME."
                        , "Invalid USERNAME : ", JOptionPane.ERROR_MESSAGE);
                }
                else if (questionField.getDocument().getLength() == 0)
                {
                    JOptionPane.showMessageDialog(null, "Please Answer the question"
                        , "Invalid ANSWER : ", JOptionPane.ERROR_MESSAGE);
                }
            }
        };
    
        public InputDialog(GetDialogValues gdv, String title, boolean isModal)
        {
            this.gdv = gdv;
            setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            setLayout(new BorderLayout());
            setModal(isModal);
            setTitle(title);
    
            JPanel panel = new JPanel();
            panel.setLayout(new GridLayout(0, 2));
            JLabel usernameLabel = new JLabel("Enter USERNAME : ");
            usernameField = new JTextField();
            JLabel questionLabel = new JLabel("How are you feeling ?");
            questionField = new JTextField();
    
            panel.add(usernameLabel);
            panel.add(usernameField);
            panel.add(questionLabel);
            panel.add(questionField);
    
            submitButton = new JButton("SUBMIT");
            submitButton.addActionListener(actionButton);
    
            add(panel, BorderLayout.CENTER);
            add(submitButton, BorderLayout.PAGE_END);
    
            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 the following code: class Program { static void Main(string[] args) { new
I have the following code: public class Test { public static void Main() {
I have following code public class TEST { public static void main(String arg[]){ try
I have a GUI class and a database class. I do something like: Dim
Basically I have a GUI that inherits from the JFrame class and has its
I have a WPF application with the main Window class called MainWindow. Since I
I have a class library that is nested two+ layers under a main GUI
I have a GUI application. In my main class I have a method (called
I have a main class in a program that launches another class that handles
I have a question about the 'Event Dispatch Thread'. I have a Main class

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.