标签:java user-interface combobox swing jcombobox
我是JAVA GUI的新手,遇到了问题.下图显示了我的问题所在的GUI部分.
我想实现这一点,当我点击“点击切换”按钮时,将交换comboBox的内容.我尝试了不同的方式来交换两个comboBox的位置或交换两个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,user-interface,combobox,swing,jcombobox 来源: https://codeday.me/bug/20190528/1168366.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。