java - 如何切换两个JComboBox的位置或交换内容?

标签 java swing user-interface combobox jcombobox

我是 JAVA GUI 的新手,遇到了一个问题。下图显示了我的问题所在的 GUI 部分。

enter image description here

我想实现的是,当我点击“点击切换”按钮时,comboBox的内容会被交换。我尝试了不同的方法来交换两个组合框的位置或交换两个组合框的内容,但都没有成功。

以下是我的代码中与此问题相关的部分。

第 1 类:

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;

public class FilePathComboBox implements ActionListener {
    List<String> strings;
    BufferedReader input;
    JComboBox comboBox;
    JPanel jpFilePath;
    JButton testJB;

    public FilePathComboBox(String filePathOfSyncTool) {
        strings = new ArrayList<String>();
        FileReader fr;

        try {
            fr = new FileReader(filePathOfSyncTool);
        } catch (FileNotFoundException e1) {
            fr = null;
            e1.printStackTrace();
        }

        input = new BufferedReader(fr);
        try {
            String line = null;
            while ((line = input.readLine()) != null) {
                strings.add(line);
            }
        } catch (FileNotFoundException e) {
            System.err.println("Error, file " + filePathOfSyncTool + 
            "didn't exist.");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        String[] lineArray = strings.toArray(new String[] {});

        comboBox = new JComboBox(lineArray);
        testJB = new JButton("click to add item");
        testJB.addActionListener(this);
        jpFilePath = new JPanel();
        jpFilePath.add(comboBox);
        jpFilePath.add(testJB);

    }

    public JComboBox getJComboBox(){
        return this.comboBox;
    }

    public void setJComboBox(JComboBox jcb){
        this.comboBox = jcb;
    }

    public JPanel getjpFilePath(){
        return jpFilePath;
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        String s1 = "E:\\home\\joe\\foo";
        comboBox.insertItemAt(s1, 0);
        comboBox.setSelectedIndex(0);

    }
}

类2:

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;

public class SwitchComboBox implements ActionListener {
    JPanel switchOverall;
    JButton switchButton;
    FilePathComboBox fpcb;
    FilePathComboBox fpcb2;
    public SwitchComboBox(){
        fpcb = new FilePathComboBox("E:\\pathRecord.txt");
        fpcb2 = new FilePathComboBox("E:\\pathRecord2.txt");
        switchButton = new JButton("click to switch");
        switchOverall = new JPanel();
        switchButton.addActionListener(this);
        switchOverall.add(fpcb.getjpFilePath());
        switchOverall.add(fpcb2.getjpFilePath());
        switchOverall.add(switchButton);
    }

    public JPanel getSwitchOverall(){
        return this.switchOverall;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //Here should be the code to switch the content 
        //or position of the two comboBox
        Component[] stringArray = fpcb.getJComboBox().getComponents();
        Component[] stringArray2 = fpcb2.getJComboBox().getComponents();
        fpcb.setJComboBox(new JComboBox());
        for(int i =0; i < stringArray2.length; i++){
            fpcb.getJComboBox().add(stringArray2[i]);
        }
        fpcb2.setJComboBox(new JComboBox());
        for(int i =0; i < stringArray.length; i++){
            fpcb2.getJComboBox().add(stringArray[i]);
        }
    }
}

希望有人能帮我解决这个问题。谢谢!

最佳答案

您的意思是组合框中保存的数据会交换吗?如果是这样,只需交换模型:

ComboBoxModel model1 = fpcb.getJComboBox().getModel();
ComboBoxModel model2 = fpcb2.getJComboBox().getModel();

fpcb.getJComboBox().setModel(model2);
fpcb2.getJComboBox().setModel(model1);

关于java - 如何切换两个JComboBox的位置或交换内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34111036/

相关文章:

java - Jaxb 解码返回空值

java - 如何在 Android 中读取文件上传的服务器响应(JSON)?

java - 使用默认类型对列表进行 Jackson 反序列化

java - JPopupMenu 的 MenuItems 不响应更改...为什么?

java - JFrame布局: Header and 2 Text Areas

iphone - IOS中UI实现查询

java - 恢复看似完全消失的 .java 文件

JavaFX应用程序不在浏览器上运行,它抛出错误?

java - 更改 Netbeans 平台主窗口背景颜色

python - 主线程不在主循环 tkinter 中,具有多个 pysimplegui 或 tkinter 对象的多线程 [已解决]