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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:53:03+00:00 2026-06-12T15:53:03+00:00

I am following the Oracle tutorial on how to create a custom dialog box:

  • 0

I am following the Oracle tutorial on how to create a custom dialog box: http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html

I have two buttons: Save Object and Delete Object which when clicked should execute a certain piece of code. Unfortunately I can’t seem to add any ActionListener to the JOptionPane buttons so when they’re clicked nothing happens.

Can anyone help tell me how I can go about doing this? Here is the class I have for the dialog box so far:

class InputDialogBox extends JDialog implements ActionListener, PropertyChangeListener {
    private String typedText = null;
    private JTextField textField;

    private JOptionPane optionPane;

    private String btnString1 = "Save Object";
    private String btnString2 = "Delete Object";

    /**
     * Returns null if the typed string was invalid;
     * otherwise, returns the string as the user entered it.
     */
    public String getValidatedText() {
        return typedText;
    }

    /** Creates the reusable dialog. */
    public InputDialogBox(Frame aFrame, int x, int y) {
        super(aFrame, true);

        setTitle("New Object");

        textField = new JTextField(10);

        //Create an array of the text and components to be displayed.
        String msgString1 = "Object label:";

        Object[] array = {msgString1, textField};

        //Create an array specifying the number of dialog buttons
        //and their text.
        Object[] options = {btnString1, btnString2};

        //Create the JOptionPane.
        optionPane = new JOptionPane(array,
                JOptionPane.PLAIN_MESSAGE,
                JOptionPane.YES_NO_OPTION,
                null,
                options,
                options[0]);


        setSize(new Dimension(300,250));
        setLocation(x, y);

        //Make this dialog display it.
        setContentPane(optionPane);
        setVisible(true);

        //Handle window closing correctly.
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);

        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                /*
                 * Instead of directly closing the window,
                 * we're going to change the JOptionPane's
                 * value property.
                 */
                optionPane.setValue(new Integer(
                        JOptionPane.CLOSED_OPTION));
            }
        });

        //Ensure the text field always gets the first focus.
        addComponentListener(new ComponentAdapter() {
            public void componentShown(ComponentEvent ce) {
                textField.requestFocusInWindow();
            }
        });

        //Register an event handler that puts the text into the option pane.
        textField.addActionListener(this);

        //Register an event handler that reacts to option pane state changes.
        optionPane.addPropertyChangeListener(this);
    }

    /** This method handles events for the text field. */
    public void actionPerformed(ActionEvent e) {
        optionPane.setValue(btnString1);
        System.out.println(e.getActionCommand());
    }

    /** This method reacts to state changes in the option pane. */
    public void propertyChange(PropertyChangeEvent e) {
        String prop = e.getPropertyName();

        if (isVisible()
         && (e.getSource() == optionPane)
         && (JOptionPane.VALUE_PROPERTY.equals(prop) ||
             JOptionPane.INPUT_VALUE_PROPERTY.equals(prop))) {
            Object value = optionPane.getValue();

            if (value == JOptionPane.UNINITIALIZED_VALUE) {
                //ignore reset
                return;
            }

            //Reset the JOptionPane's value.
            //If you don't do this, then if the user
            //presses the same button next time, no
            //property change event will be fired.
            optionPane.setValue(JOptionPane.UNINITIALIZED_VALUE);
            if (btnString1.equals(value)) {
                    typedText = textField.getText();
                String ucText = typedText.toUpperCase();
                if (ucText != null ) {
                    //we're done; clear and dismiss the dialog
                    clearAndHide();
                } else {
                    //text was invalid
                    textField.selectAll();
                    JOptionPane.showMessageDialog(
                                InputDialogBox.this,
                                    "Please enter a label",
                                    "Try again",
                                    JOptionPane.ERROR_MESSAGE);
                    typedText = null;
                    textField.requestFocusInWindow();
                }
            } else { //user closed dialog or clicked delete
               // Delete the object ...

                typedText = null;
                clearAndHide();
            }
        }
    }

    /** This method clears the dialog and hides it. */
    public void clearAndHide() {
        textField.setText(null);
        setVisible(false);
    }
  • 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-12T15:53:04+00:00Added an answer on June 12, 2026 at 3:53 pm

    I think you’re missing the point of the JOptionPane. It comes with the ability to show it’s own dialog…

    public class TestOptionPane02 {
    
        public static void main(String[] args) {
            new TestOptionPane02();
        }
    
        public TestOptionPane02() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException ex) {
                    } catch (InstantiationException ex) {
                    } catch (IllegalAccessException ex) {
                    } catch (UnsupportedLookAndFeelException ex) {
                    }
    
                    JTextField textField = new JTextField(10);
    
                    String btnString1 = "Save Object";
                    String btnString2 = "Delete Object";
    
                    //Create an array of the text and components to be displayed.
                    String msgString1 = "Object label:";
                    Object[] array = {msgString1, textField};
                    //Create an array specifying the number of dialog buttons
                    //and their text.
                    Object[] options = {btnString1, btnString2};
    
                    int result = JOptionPane.showOptionDialog(null, array, "", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE, "New Object", options, options[0]);
                    switch (result) {
                        case 0:
                            System.out.println("Save me");
                            break;
                        case 1:
                            System.out.println("Delete me");
                            break;
                    }
                }
            });
        }
    }
    

    To do it manually, you’re going to have to do a little more work.

    Firstly, you’re going to have to listen to the panel’s property change events, looking for changes to the JOptionPane.VALUE_PROPERTY and ignoring any value of JOptionPane.UNINITIALIZED_VALUE…

    Once you detect the change, you will need to dispose of your dialog.

    The you will need extract the value that was selected via the JOptionPane#getValue method, which returns an Object. You will have to interrupt the meaning to that value yourself…

    Needless to say, JOptionPane.showXxxDialog methods do all this for you…

    Now if you worried about having to go through all the setup of the dialog, I’d write a utility method that either did it completely or took the required parameters…but that’s just me

    UPDATED

    Don’t know why I didn’t think it sooner…

    Instead of passing an array of String as the options parameter, pass an array of JButton. This way you can attach your own listeners.

    options – an array of objects indicating the possible choices the user
    can make; if the objects are components, they are rendered properly;
    non-String objects are rendered using their toString methods; if this
    parameter is null, the options are determined by the Look and Feel

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

Sidebar

Related Questions

I'm trying to adapt the following example: http://docs.oracle.com/javase/tutorial/uiswing/examples/zipfiles/components-ScrollDemoProject.zip The purpose of i want to
I'm following Oracle's ServerSide Socket tutorial at http://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html . I use the source as
I'm using the following code for practising, http://docs.oracle.com/javase/tutorial/uiswing/examples/layout/BorderLayoutDemoProject/src/layout/BorderLayoutDemo.java I also add frame.setSize(frame.getMaximumSize()); in createAndShowGUI()
The following code exist on http://docs.oracle.com/javase/tutorial/networking/urls/readingWriting.html Which is the oracle tutorial website. My problem
I'm running the following tutorial: http://download.oracle.com/javase/6/docs/technotes/guides/security/jgss/tutorials/BasicClientServer.html I'm getting the following prompts: Connected to server
http://docs.oracle.com/javase/tutorial/java/generics/genmethods.html Quoting from there- A more realistic use of generic methods might be something
The following article http://docs.oracle.com/javaee/1.4/tutorial/doc/JAXPXSLT3.html says that XPATH considers the following to be nodes: Root
I've read this tutorial: http://docs.oracle.com/javase/tutorial/jdbc/basics/transactions.html but I think I'm still missing something. Let us
Following this tutorial http://netbeans.org/kb/docs/javaee/maven-osgiservice-cdi.html I have managed to create a simple OSGI bundle and
I am following this tutorial, I'm using netbeans 6.5.1 http://www.netbeans.org/kb/docs/java/gui-db-custom.html When I get to

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.