java - 如何从文本文件填充Java中的两个组合框?

标签 java swing combobox jcombobox

我正在尝试从数据填充两个组合框,但不知道如何在两个组合框之间划分该数据。数据现在填充在两个组合框中。

这是我的数据文本文件:

[Gates]
value1
value2
value3

[Mids]
customer1
customer2

这是我在 Java Swing Gui 应用程序中的代码:

private void populateCombos() throws FileNotFoundException {

    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));
    fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    int result = fileChooser.showOpenDialog(frmGottApplication);

    BufferedReader input=new BufferedReader(new FileReader(fileChooser.getSelectedFile()));
    if (result == JFileChooser.APPROVE_OPTION) {
        selectedFile = fileChooser.getSelectedFile();
        textFieldLoadConfig.setText(selectedFile.getAbsolutePath());
        lblConfRes.setText("Loaded " + selectedFile.getAbsolutePath().toString());
    } else {
        lblConfRes.setText("You didn't load...");
    }
    List<String> strings = new ArrayList<String>();
    try {
        String line = null;
        try {
            while ((line = input.readLine()) != null) {
                strings.add(line);
            }
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    } finally {
        try {
            input.close();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }
    String[] lineArrayGates = strings.toArray(new String[] {});

    comboBoxGate.removeAllItems();
    comboBoxMid.removeAllItems();

    for (String str : lineArrayGates) {
        comboBoxGate.addItem(str);
        comboBoxMid.addItem(str);
    }
}

从代码中可以看出,我正在从外部文本文件读取数据,然后尝试将其加载到两个不同的组合框中。但是如何编写将门的值划分为第一个组合并将中间的值划分为第二个组合的代码。 有什么想法可以建议吗? 谢谢

最佳答案

问题始于文件及其内容,用更舒适的格式定义属性文件...您可以使用 json、xml、yaml,或者只是属性...

我将使用老式的 java 属性来做这个例子

文件:

Gates=value1,value2,value3

Mids=customer1,customer2

然后将其读取为属性,将其拆分为 StringArray 并用其填充框

public static void main(String[] args) {
Properties prop = new Properties();
InputStream input = null;

try {
    input = new FileInputStream("./file2.txt");
    prop.load(input);
    String[] gates = prop.getProperty("Gates").split(",");
    String[] mids = prop.getProperty("Mids").split(",");
    JFrame myJFrame = new JFrame();
    myJFrame.setTitle("Example");
    myJFrame.setSize(250, 250);
    JPanel panel = new JPanel();

    JComboBox<String> myComboGates = new JComboBox<>(gates);
    JComboBox<String> myComboMids = new JComboBox<>(mids);
    panel.add(myComboGates);
    panel.add(myComboMids);

    myJFrame.add(panel);
    myJFrame.setVisible(true);

} catch (IOException ex) {
    ex.printStackTrace();
} finally {
    if (input != null) {
    try {
        input.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    }
}

}

结果是 2 个组合,其中包含来自 Prop 的 2 种不同类型的信息。文件:enter image description here

关于java - 如何从文本文件填充Java中的两个组合框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43963993/

相关文章:

java - JDialog 具有透明背景,模糊了下面的东西

c# - 添加项目后组合框不刷新 C# WPF

javascript - SAPui5 创建简单的 sap.m.ComboBox

Qt QML ComboBox 覆盖滚轮事件

java - 了解Android的webview addjavascriptinterface

java - 如何在 Swing/Nimbus 中的一个组件上停用焦点矩形

java - 为什么 JPQL 表达式无法导航到作为集合的关系字段之外?

java - 如何检查 JTextField 中输入的值并将输入的字符串传输到字符数组?

java - 如何使用子关联属性作为 JPA 父实体中的映射键

java - 如何将数据库中的数据检索到JTable中?