I am new to the JAVA GUI and have encountered a problem. The following figure shows the part of the GUI where my problem is.

I want to achieve this, when I press the "click on the switch" button, the contents of the comboBox will be exchanged. I tried different ways to either exchange the position of two comboBox or exchange the contents of two ComboBox, but it didn’t work.
The following is part of my code related to this problem.
Class1:
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);
}
}
class2:
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) {
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]);
}
}
}
Hope someone can kindly help me with this. Thanks!
source
share