Reputation: 23
I have a simple program that makes shapes of different sizes and colours.
The shapes and colours are selected from ComboBoxes, while the size is controlled by a slider. I want to store the -currently selected- shape, size and the colour in an ArrayList of a new class that I defined by pressing a button and then print the info pressing another.
private ArrayList<ShapeItem> ShapeItem;
slider = new JSlider (JSlider.HORIZONTAL,minValue,maxValue,initValue);
add(slider);
slider.addChangeListener(this);
// Instantiate a new ComboBox with options for Shape
shapeChoice = new JComboBox();
shapeChoice.addItem("Point");
shapeChoice.addItem("Square");
shapeChoice.addItem("Circle");
shapeChoice.addItem("Doughnut");
shapeChoice.addItem("Pentagon");
shapeChoice.addItem("Hexagon");
add(shapeChoice); // Add the JComboBox to the JPanel
shapeChoice.addActionListener(this); //Register the JComboBox selection with Java
// Instantiate a new ComboBox with options for Colour
colorChoice = new JComboBox();
colorChoice.addItem("Black");
colorChoice.addItem("Red");
colorChoice.addItem("Green");
Then:
readItems = new JButton("Read values");
add(readItems);
readItems.addActionListener(this);
printItems = new JButton("List all values");
add(printItems);
printItems.addActionListener(this);
ShapeItem = new ArrayList<ShapeItem>();
My best bet at achieving that was introducting
if (e.getSource() == readItems)
{
String item0 = shapeChoice.getSelectedItem();
String item1 = colorChoice.getSelectedItem();
}
// if the "List all values" button is pressed
else if (e.getSource() == printItems)
{
// loop through the ArrayList of items prnting each one out in turn to the JTextArea
for (int j=0; j<i; j++)
{
textOutput.append(itemList.get(j).shapeChoose + "\n");
textOutput.append(itemList.get(j).colourChoose + "\n");
textOutput.append("\n");
}
}
But at the
String item0 = shapeChoice.getSelectedItem();
I get an "incompatible types error"
The new class I made is this:
public class ShapeItem
{
public String shapeChoose;
public String colorChoose;
public int thesize;
}
Am I trying the right way or there is another method to achieve that?
Upvotes: 2
Views: 1347
Reputation: 7004
There is no shortcut, you'll have to iterate through the items in the JComboBox
and add each of them to the array.
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JComboBox;
public class Main {
JComboBox<String> shapeChoice = new JComboBox<>();
shapeChoice.addItem("Point");
shapeChoice.addItem("Square");
shapeChoice.addItem("Circle");
shapeChoice.addItem("Doughnut");
shapeChoice.addItem("Pentagon");
shapeChoice.addItem("Hexagon");
System.out.println(combo2Array(shapeChoice).toString());
}
public static List<String> combo2Array(JComboBox<String> combo){
List<String> list = new ArrayList<>();
for(int i=0;i<combo.getItemCount();i++){
list.add(combo.getItemAt(i));
}
return list;
}
}
Output:
[Point, Square, Circle, Doughnut, Pentagon, Hexagon]
You can also change it to generics, if you want (it doesn't have to be Strings). But the idea will be the same.
I made what I believe is the design you are looking for and posted it on gist. But it will be an awful design choice, as you are creating a list with a size much larger than what you actually need. Using 3 Lists (one for each component) will be a better design choice and that's why I will not update my answer here with the gist code.
EDIT2:
To add just what is currently selected:
public void addSelected2List(){
ShapeItem shape = new ShapeItem();
shape.setShapeChoose((String)shapeChoice.getSelectedItem());
shape.setColourChoose((String)colourChoice.getSelectedItem());
shape.setSize((int)sizeChoice.getSelectedItem());
list.add(shape);
}
Upvotes: 2