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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:32:03+00:00 2026-05-29T06:32:03+00:00

This problem is solved. I’m am developing a Java Swing based projected, and the

  • 0

This problem is solved.

I’m am developing a Java Swing based projected, and the look & feel of the application is completely customized. We are trying to maintain a consistent appearance throughout the program, and the default Java dialog windows are not the same.

The current issue requires a control blocking call to the user prompt. Similar to JOptionPane.showConfirmDialog() In this case, the static call produces a window, and halts the flow of the program until the user selects an option. It also returns the value of the option. Note that the GUI itself is not locked up logically, but the user cannot interact with the rest of it.

int n = JOptionPane.showConfirmDialog(this,
    "Are you sure?",
    "Confirm"
    JOptionPane.YES_NO_OPTION);

I would like to duplicate this functionality with a customized appearance, and using a string. Ideally, my code would appear as follows:

String result = CustomPrompt.showPrompt(this,
    "Please enter your name", //Text
    "Prompt.",                //Title
    "John Smith");            //Default

This will generally be used for password input, and I understand the return type of a password is different, but this is an example. This can be accomplished by using a series of button and event listeners, in several classes, but the readability of the code and reliability of the application diminishes.

The Frame would be built via NetBeans and customized from there. I do understand that such a prompt already exists in Swing, but it’s look & feel is completely and entirely different.

The question summarized: How to use a customized frame to prompt the user for input in a blocking manner.

The solution to this problem is as follows:

public class PromptForm extends JDialog
{
    transient char[] result;

    public char[] getResult()
    {
        return result;
    }
    public PromptForm(Frame parent)
    {
        super(parent, true);
        initComponents();
    }
    public void prompt()
    {
        this.setVisible(true);
    }
    public static char[] promptForPassword(Frame parent)
    {
        PromptForm pf = new PromptForm(parent);
        pf.prompt();
        return pf.getResult();
    }
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
    {
        result = jPasswordField1.getPassword();
        setVisible(false);
        dispose();
    }
    private void initComponents() {...}

    private javax.swing.JButton jButton1;
    private javax.swing.JPasswordField jPasswordField1;
}

Called by:

    char [] ret = PromptForm.promptForPassword(this);
    JOptionPane.showMessageDialog(this, new String(ret), "Neat", JOptionPane.QUESTION_MESSAGE);
  • 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-29T06:32:03+00:00Added an answer on May 29, 2026 at 6:32 am

    Make CustomPrompt extends JDialog, have it’s constructor call super passing the owner Window and the desired ModalityType.

    public class CustomPrompt extends JDialog {
    
      public static String showPrompt(Window parent, String title, String text, 
             String defaultText) {
        final CustomPrompt prompt = new CustomPrompt(parent);
        prompt.setTitle(title);
        // set other components text
        prompt.setVisible(true);
    
        return prompt.textField.getText();
      }
    
      private JTextField textField;
    
      // private if you only want this prompt to be accessible via constructor methods
      private CustomPrompt(Window parent) {
        super(parent, Dialog.DEFAULT_MODALITY_TYPE); 
        // Java >= 6, else user super(Frame, true) or super(Dialog, true);
        initComponents(); // like Netbeans
      }
    
      // initComponents() and irrelevant code. 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This problem has been solved thanks to your suggestions. See the bottom for details.
This problem has been solved! Thanks a lot to Brad, Denis and junkie! You're
EDIT: This problem has been solved. See below. Hey all. I'm building an iPhone
I have solved this problem, I just need to know what to do. I
I'm sure this problem has been solved before and I'm curious how its done.
I'm curious how others have solved this problem, and what problems might lurk behind
I know I've solved this problem before, but I can't remember or find the
NOTE: I have solved the majority of this problem but have run into a
Update II Problem Solved but Why? This has been the biggest headache ever. My
I have run into this problem before but never quite solved it. I have

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.