I thought it would be really cool if JOptionPane could be extended to accept an array of strings that represent messages, and have the method return an array of strings that represent user answers to the messages which are entered into the dialog box. This would make getting GUI input a little easier. Unfortunately I have no idea how the JOptionPane class works under the hood so I have no Idea where to start. Here is an example of the method I would start work on.

    import javax.swing.JOptionPane;
    import java.awt.Component;

    public class EXTJOptionPane extends JOptionPane {

        public static Object[] showInputDialog(Component parentComponent, 
                Object message[]){
            return new Object[3];
        }//end method
    }//end class

Edited 3 Years Ago by overwraith

Ignore anyone who tells you that extending a class is a good way to reuse the implementation of that class. The only good reason to extend a class is so that code can use objects of your new class as though they were objects of the class you are extending. Unless you are planning on pretending that objects of your class actually are JOptionPane objects, you are just creating trouble.

Your description of what you are trying to accomplish isn't clear to me, but you should probably just have EXTJOptionPane create a JOptionPane as necessary and use that JOptionPane to create the effect that you want. Then you don't need to know how JOptionPane works under the hood. And if JOptionPane can't be made to do what you need, then use a javax.swing.JDialog and you should be able to do anything you want.

Edited 3 Years Ago by bguild

another good reason is to keep the main part of a class's functionality, but to alter the functionality of, let's say: one method.

but extending JOptionPane makes no sense, since about all it's methods are static.

Would making a completely seperate class with the proposed functionality be better/make more sense?

if you really want to have "complete control" over your Dialog, what's stopping you from diving into your Swing knowledge and creating your own? for those cases where you don't need a special one, you can still fall back on the existing JOptionDialog boxes.

I thought this was going to be harder than what it turned out to be. After looking around online some, and some coding here is what I came up with.

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.Component;
import java.awt.GridLayout;

public class EXTJOptionPane extends JOptionPane {

    public static void main(String[] args) {
           String messages[] = {"str1", "str2", "str3", "str4", "str5"};

           showInputDialog(null, messages);
    }//end main

   public static String[] showInputDialog(Component parentComponent, String messages[]){
       JTextField textFields[] = new JTextField[messages.length];
       JPanel panel = new JPanel();
       String input[] = new String[messages.length];

       panel.setLayout(new GridLayout(messages.length, 2, 0, 0));

       for(int i = 0 ; i < messages.length ; i++){
           panel.add(new JLabel(messages[i]));
           textFields[i] = new JTextField();
           panel.add(textFields[i]);
       }

       JOptionPane.showConfirmDialog(parentComponent, panel, 
               "Input", JOptionPane.OK_CANCEL_OPTION);

       for(int i = 0 ; i < messages.length ; i++)
           input[i] = textFields[i].getText();

       return input;
   }//end method

}//end class

again, extending JOptionPane won't do any good. almost all it's members are static, which are not inherited.

I see what you mean now, a class shouldnt be extended unless there is a reason for extending it. I fixed the code as follows.

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.Component;
import java.awt.GridLayout;

public class JOptionPaneArrayInputExample{

    public static void main(String[] args) {
           String messages[] = {"str1", "str2", "str3", "str4", "str5"};

           showInputDialog(null, messages);
    }//end main

   public static String[] showInputDialog(Component parentComponent, String messages[]){
       JTextField textFields[] = new JTextField[messages.length];
       JPanel panel = new JPanel();
       String input[] = new String[messages.length];

       panel.setLayout(new GridLayout(messages.length, 2, 0, 0));

       for(int i = 0 ; i < messages.length ; i++){
           panel.add(new JLabel(messages[i]));
           textFields[i] = new JTextField();
           panel.add(textFields[i]);
       }

       JOptionPane.showConfirmDialog(parentComponent, panel, 
               "Input", JOptionPane.OK_CANCEL_OPTION);

       for(int i = 0 ; i < messages.length ; i++)
           input[i] = textFields[i].getText();

       return input;
   }//end method

}//end class
This question has already been answered. Start a new discussion instead.