I am trying to use an action listener to fill an array, however I want to fill ONE array from two different sources: ComboBox Selection & JList selection. so I am trying to create an Array that will look similar to this format(each line is representing a different subscript)

ComboBoxData
JListData
JListData
JListData
ComboBoxData
JListData
JListData
JListData
JListData
ComboBoxData

I am almost certain there has got to be a way to do this, but I am not finding it! I am trying to work with an

if(e.getSource == ComboBoxData)
{
           String selection = (String)ComboBoxData.getSelectedItem();
           selectedComboBoxData.setText(selection);
                       if ( selection == String)
                               {
                                        Assign to array here
                                }
                        else if( selection == int)
                                  {
                                          Assign to array here, with "++
                                   }

I have so much code, right now I just tried to give the idea of what I am working with, but if there are any suggestions I would truly be greatful...please!!

Thanks,
Traci

Edited 3 Years Ago by mike_2000_17: Fixed formatting

I am trying to use an action listener to fill an array, however I want to fill ONE array from two different sources: ComboBox Selection & JList selection. so I am trying to create an Array that will look similar to this format(each line is representing a different subscript)
ComboBoxData
JListData
JListData
JListData
ComboBoxData
JListData
JListData
JListData
JListData
ComboBoxData

I am almost certain there has got to be a way to do this, but I am not finding it! I am trying to work with an

if(e.getSource == ComboBoxData)
{
String selection = (String)ComboBoxData.getSelectedItem();
selectedComboBoxData.setText(selection);
if ( selection == String)
{
Assign to array here
}
else if( selection == int)
{
Assign to array here, with "++
}
I have so much code, right now I just tried to give the idea of what I am working with, but if there are any suggestions I would truly be greatful...please!!

Thanks,
Traci

What's ComboBoxData? Is that a class? An object? Regardless, this won't work.

if(e.getSource == ComboBoxData)

If getSource is a function is a function, the function call needs parentheses.

This won't work either:

if ( selection == String)

Not sure what you are trying to do here (compare types?) selection is already defined as a String. Are you comparing it to the String "String"?

ComboBox Data is just a combo box that is accepting an array of data.

//Event handlers from combo boxes
        private class ComboBoxListener implements ActionListener
        {
                public void actionPerformed(ActionEvent e)
                {
                        //Get the selected student & Display in text box
                        if(e.getSource() == studentPicker)
                        {                       
                            String selection = (String)studentPicker.getSelectedItem();
                            selectedStudent.setText(selection);
                            int index;
                            index = studentPicker.getSelectedIndex();
//                          selection = names[index];
                            System.out.println(draft.studentNames[index]);  
                        }

//Event listeners for buttons
        private class ButtonListener implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
                {
                    Object[] selections = coursePicker.getSelectedValues();
                    //store in selected courses box
                    selectedCourses.setListData(selections);
                }
        }   

All of the above code works fine, but I want what both of these get to all get stored into one array. The first will put its data into the array & then the second will put its data into the array & then it will go back to the first action listener & put more data into the array & then the second action listener will put its data into the array. I want these to keep adding data, not replacing
Thanks, Traci

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Why use an array; that's just making your life harder than it needs to be. Use an ArrayList, which does everything an array does, plus you can just add your data to it with the add(...) method, and you never need to worry about it being big enough.

Thank you!!! I've been thinking that an array list would work better...but I'm in a group project and well...

Well, if you HAVE to use an array, you need to have an additional int variable that holds the index number of the next available array element. When you add more data, you use elements starting from there, and update the index number according to how many elements you add.

This article has been dead for over six months. Start a new discussion instead.