tags:

views:

219

answers:

2

Hi I am trying to access an array and show it in a dropdown list using joptionpane. I have it sort of working using:

for (int i = 0; i < data.length; r++)
{

    Object list = JOptionPane.showInputDialog(null, "Select Code",
    "Code", JOptionPane.QUESTION_MESSAGE, null, data[i][0], data[0][0]);
}

This brings up a dropdown list...but with only one piece of data and displays it about 5times (array length). How could I get this to display each data just in one dropdown?

Edit - Forgot to say when using data[i][0] it brings an error, as it only lets me use data[i] ? - "cannot find symbol"

Thanks

+2  A: 

It shouldn't be a loop.

public static Object showInputDialog(Component parentComponent,
                                     Object message,
                                     String title,
                                     int messageType,
                                     Icon icon,
                                     Object[] selectionValues,
                                     Object initialSelectionValue)

are you sure you are passing the selectionValues object array and initialSelectionValue Object?

What is the type of data? Assuming data[0] is an array, try

JOptionPane.showInputDialog(null, "Select Code",
    "Code", JOptionPane.QUESTION_MESSAGE, null, data, data[0]);

Can you tell what exactly is your data's type? This example from JDK

Object[] possibleValues = { "First", "Second", "Third" };
Object selectedValue = JOptionPane.showInputDialog(null,    
            "Choose one", "Input",   
            JOptionPane.INFORMATION_MESSAGE, null,
            possibleValues, possibleValues[0]);

works perfectly well for me.

Murali VP
Data contains a list of about 5 items, I am trying to show the first element for all 5
Elliott
its string when using the code you just added, it only displays the first element. Thanks
Elliott
this shows [Ljava.lang.String;@7a7893 about 20times
Elliott
this is a short example of my array:String data[][] = new String[][] { {"SO35", "1", "2", "09.35", "10.18"},
Elliott
I am using a multidimensional array
Elliott
Sorted it myself - Thanks for the help
Elliott
A: 

This is from the Java API:

public static Object showInputDialog(Component parentComponent,
                                     Object message,
                                     String title,
                                     int messageType,
                                     Icon icon,
                                     Object[] selectionValues,
                                     Object initialSelectionValue)

Note that the selectionValues is a single dimensional array. When you feed in data[i][0], you are giving it a single value. When you feed in data[i] you are giving in a single row of the array, as a one dimensional array. (Recall that java doesn't have n-Dimensional arrays, only arrays of arrays.)

You probably need to build a new one dimensional array, and feed that in.

Alternatively, if possible, convert the rows of your arrays to some object, and implement the toString() method in that class to display what you want.

chris
Thanks I was thinking of creating an new array of the data array, just looking if it is poissble any other ways.
Elliott
Used a new array thanks.
Elliott