gpt4 book ai didi

java - 链接组合框

转载 作者:行者123 更新时间:2023-12-02 08:34:36 25 4
gpt4 key购买 nike

我正在尝试制作链接的ComboBoxes。例如,我有三个组合框,每个组合框都有三个项目(a、b、c)。在开始时,第一个组合框“a”被选择,第二个组合框“b”被选择,第三个组合框“c”被选择。我需要为它们制作这样的 ActionListener ,其工作方式如下:如果将第二个列表中的选择从“b”更改为“a”,则第一个列表中的所选项目会自动从“a”更改到“b”。

我尝试以这种方式解决问题:

 public class MyComboBoxListener implements ActionListener {

public void actionPerformed(ActionEvent a) {
int i = 0;
int j = 0;
while (a.getSource() != valsListArray.get(i)) {
i++;
}
String selected = valsListArray.get(i).getSelectedItem().toString();
while (selected != valsListArray.get(j).getSelectedItem() && j != i) {
j++;
}
String r = chosenVals[i];// in the beginnig elements are that order a, b, c
valsListArray.get(j).setSelectedItem(chosenVals[i]);
chosenVals[j] = r;
chosenVals[i] = selected;
}
}

但这并不能解决问题。

最佳答案

你的问题很有趣。一种解决方案是扩展 JComboBox,使其保存对其先前选择的项目和索引的引用,这将允许您在需要时提取信息。例如:

import javax.swing.ComboBoxModel;
import javax.swing.JComboBox;

public class NewComboBox extends JComboBox {
private Object previousSelectedItem = null;
private int previousSelectedIndex = -1;

public Object getPreviousSelectedItem() {
return previousSelectedItem;
}

public int getPreviousSelectedIndex() {
return previousSelectedIndex;
}

NewComboBox(ComboBoxModel aModel) {
super(aModel);
}

@Override
public void setSelectedIndex(int anIndex) {
previousSelectedIndex = getSelectedIndex();
previousSelectedItem = getSelectedItem();
super.setSelectedIndex(anIndex);
}

@Override
public void setSelectedItem(Object anObject) {
previousSelectedIndex = getSelectedIndex();
previousSelectedItem = getSelectedItem();
super.setSelectedItem(anObject);
}

}

然后你就可以根据需要使用它了。例如,

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class LinkedCombos extends JPanel {
public static final String[] DATA = {"a", "b", "c"};
public static final int COMBO_COUNT = 3;
private DefaultComboBoxModel[] comboModels = new DefaultComboBoxModel[COMBO_COUNT];
private NewComboBox[] comboBoxes = new NewComboBox[COMBO_COUNT];
private MyComboListener myComboListener = new MyComboListener();

public LinkedCombos() {
for (int i = 0; i < comboModels.length; i++) {
comboModels[i] = new DefaultComboBoxModel(DATA);
comboBoxes[i] = new NewComboBox(comboModels[i]);
comboBoxes[i].setSelectedIndex(i);
comboBoxes[i].addActionListener(myComboListener);
add(comboBoxes[i]);
}
}

private class MyComboListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent evt) {
NewComboBox combo = (NewComboBox) evt.getSource();
String selection = combo.getSelectedItem().toString();

for (int i = 0; i < comboBoxes.length; i++) {
NewComboBox comboI = comboBoxes[i];
if (comboI != combo && comboI.getSelectedItem().equals(selection)) {
comboI.setSelectedItem(combo.getPreviousSelectedItem());
}
}
}
}

private static void createAndShowGui() {
LinkedCombos mainPanel = new LinkedCombos();

JFrame frame = new JFrame("LinkedCombos");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

关于java - 链接组合框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10862760/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com