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

  • Home
  • SEARCH
  • 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 8833089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:41:43+00:00 2026-06-14T08:41:43+00:00

I never had to write GUI in java. This time I can also skip

  • 0

I never had to write GUI in java. This time I can also skip it and use args as UI (user interface).
But I wonder if there is a simple way to create a small GUI to let the user select one of the options.
In other words, to implement the askUser() function where user can select from a drop-down menu and press “ok”.
I spend some time learning this topic, but not even sure that I know which type of GUI i need for this task. JFrame? JPanel? Jmenu? Thanks.

Here is an example of the desired function.

package trygui;

public class Main {

    public static void main(String[] args) {
        String[] choices = new String[]{"cats", "dogs"};
        int choice = askUser(choices);
        System.out.println("selected: " + choices[choice]);
    }

    static int askUser(String[] choices) {
        // create pop-up dialog
        return 0;
    }
}

Update: I use Netbeans, if this can make difference.

  • 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-14T08:41:44+00:00Added an answer on June 14, 2026 at 8:41 am

    The simplest option would be to use the JOptionPane API

    enter image description here

    public class TestOptionPane03 {
    
        public static void main(String[] args) {
            new TestOptionPane03();
        }
    
        public TestOptionPane03() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    JPanel panel = new JPanel();
                    panel.add(new JLabel("Please make a selection:"));
                    DefaultComboBoxModel model = new DefaultComboBoxModel();
                    model.addElement("Chocolate");
                    model.addElement("Strewberry");
                    model.addElement("Vanilla");
                    JComboBox comboBox = new JComboBox(model);
                    panel.add(comboBox);
    
                    int result = JOptionPane.showConfirmDialog(null, panel, "Flavor", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
                    switch (result) {
                        case JOptionPane.OK_OPTION:
                            System.out.println("You selected " + comboBox.getSelectedItem());
                            break;
                    }
    
                }
            });
        }
    }
    

    You can find out more by having a read through How to Make Dialogs

    UPDATED with feedback

    public class TestOptionPane03 {
    
        public static void main(String[] args) {
            String choice = ask("Chocolate", "Strewberry", "Vanilla");
            System.out.println("You choose " + choice);
        }
    
        public static String ask(final String... values) {
    
            String result = null;
    
            if (EventQueue.isDispatchThread()) {
    
                JPanel panel = new JPanel();
                panel.add(new JLabel("Please make a selection:"));
                DefaultComboBoxModel model = new DefaultComboBoxModel();
                for (String value : values) {
                    model.addElement(value);
                }
                JComboBox comboBox = new JComboBox(model);
                panel.add(comboBox);
    
                int iResult = JOptionPane.showConfirmDialog(null, panel, "Flavor", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
                switch (iResult) {
                    case JOptionPane.OK_OPTION:
                        result = (String) comboBox.getSelectedItem();
                        break;
                }
    
            } else {
    
                Response response = new Response(values);
                try {
                    SwingUtilities.invokeAndWait(response);
                    result = response.getResponse();
                } catch (InterruptedException | InvocationTargetException ex) {
                    ex.printStackTrace();
                }
    
            }
    
            return result;
    
        }
    
        public static class Response implements Runnable {
    
            private String[] values;
            private String response;
    
            public Response(String... values) {
                this.values = values;
            }
    
            @Override
            public void run() {
                response = ask(values);
            }
    
            public String getResponse() {
                return response;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've never had to do this, but I'm binding a repeater to a generic
This may sound basic for a question but I never had a formal (if
So I have never had to use cookies before but now I am making
I had never had this error before, and I didn't move any file, or
I never had any problems with Delete button on any browsers, but I had
[Update] Unfortunately I never had an opportunity to solve this problem. However, there are
I've never had this issue before, so I'm somewhat lost. I'm getting two different
I've never had the need to really ever use any of the .NET Data
Usually I worked with PostgreSQL and never had a problem, but now I need
I started coding in C# and have never had the opportunity to use callbacks

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.