java - 将组合框中的当前项目存储在 ArrayList 中

标签 java arraylist combobox slider

我有一个简单的程序,可以制作不同尺寸和颜色的形状。

形状和颜色从组合框中选择,而大小由 slider 控制。我想通过按一个按钮将当前选定的形状、大小和颜色存储在我定义的新类的 ArrayList 中,然后按另一个按钮打印信息。

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");

然后:

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>();  

实现这一目标的最佳选择是引入

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");
        }
    }

但是在

字符串 item0 = shapeChoice.getSelectedItem();

我收到“不兼容类型错误”

我制作的新类(class)是这样的:

public class ShapeItem

{

public String shapeChoose;
public String colorChoose;
public int thesize;

}

我正在尝试正确的方法还是有其他方法可以实现这一目标?

最佳答案

没有捷径,您必须遍历 JComboBox 中的项目并将它们添加到数组中。

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;
    }
}

输出:

[Point, Square, Circle, Doughnut, Pentagon, Hexagon]

如果需要,您还可以将其更改为泛型(不一定是字符串)。 但想法是一样的。

我做了我认为是您正在寻找的设计并将其发布在 gist 。 但这将是一个糟糕的设计选择,因为您创建的列表的大小比您实际需要的要大得多。 使用 3 个列表(每个组件一个)将是更好的设计选择,这就是为什么我不会用 gist code 更新我的答案。 .

编辑2:

仅添加当前选择的内容:

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);
}

关于java - 将组合框中的当前项目存储在 ArrayList 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25317744/

相关文章:

java 。如何从两个字符串数组或列表中删除相同的字符串对象

combobox - 如何从mono Gtk.ComboBox获取选定的值?

java - 如何根据未知列数创建动态sql字符串

java - java中Map对象的连接列表

java - 我想逐行读取文件并拆分字符串以用作新对象的属性

python - 如何修改ttk Combobox字体?

vb.net - ComboBox MeasureItem 事件未触发

java - 处理中的多个类

java - Gradle 如何将大量 jar 文件的平面目录转换为 .m2/repository 结构?

java - 从 JSON 响应中获取所有记录