gpt4 book ai didi

java - JCombo + 覆盖 toString

转载 作者:太空宇宙 更新时间:2023-11-04 14:30:32 24 4
gpt4 key购买 nike

好吧,我正在开发一个相当大的程序。

我会尽量只输入您需要的内容。

现在,comboBox正在填充(使用覆盖toString),并且第一个项目被选中。

当我选择不同的参数并强制更改 comboBox 的内容时,新列表将放入 comboBox 中,并再次选择第一项。

我可以看到更新,因此我知道它已正确填充。

问题是,当我在comboBox中选择任何内容时,什么也没有发生。

第一个项目保持选中状态,我的任何 System.out.println 行都没有打印,因此没有执行任何操作。

当我删除覆盖 toString 时,一切都会按预期进行。

奇怪的是,除此之外,当删除此重写toString时,它会依赖于拥有重写toString的父类>.

发生了什么事?

据我了解,toString确实改变了显示的内容,但它不会改变数据。

我正在将对象添加到comboBox,但显示一些信息。

public class Belt extends Part{
//variable initialization and methods
@override
public String toString(){
String display = this.getCode() + " - " + this.color;
return display;
}

public final class Something implements ActionListener{
//variable initialization and methods
//there are several methods that call the fillBeltCombo()

GridBagConstraints c = new GridBagConstraints();
private void pad(GridBagConstraints c){
c.anchor = GridBagConstraints.NORTHWEST;
c.weightx = 1;
c.insets.left = 10;
c.insets.right = 10;
c.insets.top = 5;
c.insets.bottom = 5;
}

beltCombo = new JComboBox();
beltCombo.setVisible(true);
c.gridwidth = 2;
c.gridx = 4;
c.gridy = 9;
beltCombo.addActionListener((ActionEvent eventBelt) -> {
JComboBox beltCodeCombo1 = (JComboBox) eventBelt.getSource();
if(beltCombo.getItemCount()>0){
currentProduct.setBelt((Belt)beltCodeCombo1.getSelectedItem());
}else{/*do nothing*/}
});
pane.add(beltCombo, c);

public static void fillBeltCombo(ArrayList<Belt> list){
beltCombo.removeAllItems();
int size = list.size();
for(int x=0; x<size; x++){
beltCombo.addItem(list.get(x));
}
}
}

最佳答案

因此,根据标记空间的建议,我创建了一个 MCVE(在保持程序正在执行的操作的同时尽我所能)。下面的所有内容都是在我的真实程序中完成的,尽管更复杂。我还不确定我的程序做错了什么,但是我确实发现我想做的事情是可能的,我可以在使用 Override toString 时从第二个组合框中选择一个项目在子类中。现在我只需要在实际代码中找到问题......

package minitest;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class MainClass {
JFrame frame;
JPanel pane;
JLabel label1, label2;
JComboBox comboA, comboB;
ArrayList<Parent> parents = new ArrayList<>();
ArrayList<Grandchild> grandkids = new ArrayList<>();
Parent parent1 = new Parent(1);
Parent parent2 = new Parent(2);
Grandchild grandkid1 = new Grandchild(1,1,"oneone");
Grandchild grandkid2 = new Grandchild(1,2,"onetwo");
Grandchild grandkid3 = new Grandchild(2,1,"twoone");
Grandchild grandkid4 = new Grandchild(2,2,"twotwo");
public class Parent{
protected int num;
public Parent(int num){
this.num = num;
}
@Override public String toString(){
return String.valueOf(num);
}
}
public class Child extends Parent{
private double dbl;
public Child(int num, double dbl){
super(num);
this.dbl = dbl;
}
}
public class Grandchild extends Child{
private String str;
public Grandchild(int num, double dbl, String str){
super(num, dbl);
this.str = str;
}
@Override public String toString(){
return this.num + " - " + this.str;
}
}
//Fill passed combo with each item from passed ArrayList
public void fillCombo(ArrayList list, JComboBox target){
target.removeAllItems();
int size = list.size();
for(int x = 0; x < size; x++){
target.addItem(list.get(x));
}
}
//remove all items from combobox except selected item
public void comboUpdate(JComboBox combo){
if(combo.getSelectedItem()==null){
/*donothing*/
}else{
int size = (combo.getItemCount()-1);
System.out.println("size: "+size);
for (int x=size; x>=0; x--) {
if(combo.getSelectedItem().equals(combo.getItemAt(x))){
/*donothing*/
System.out.println(combo.getSelectedItem()+" is selected");
}else{
System.out.println(combo.getItemAt(x)+" is now removed");
combo.removeItemAt(x);
}
}
}
}
//Add each item from passed ArrayList if not equal to the selected item
public void removeDuplicate(JComboBox combo, ArrayList array){
System.out.println("removeDuplicate: "+array);
if(combo.getSelectedItem()==null){
System.out.println("combo.getSelectedItem==null");
fillCombo(array, combo);
}else{
boolean validChoice = false;
int arraySize = array.size();
for(int x = 0; x < arraySize; x++){
if(combo.getSelectedItem().equals(array.get(x))){
System.out.println(combo.getSelectedItem()+" == "+array.get(x));
validChoice = true;
}else{
System.out.println(combo.getSelectedItem()+" != "+array.get(x));
combo.addItem(array.get(x));
}
}
if(validChoice == false){
System.out.println("removeItemAt(0)");
combo.removeItemAt(0);
}
}
}
//fill comboB with grandchild objects whos int match the selected partent objects int
public void fillComboB(){
System.out.println("grandkids: "+grandkids);
ArrayList<Grandchild> temp = copyArrayList(grandkids);
System.out.println("temp: "+temp);
comboUpdate(comboB);
int size = (temp.size()-1);
for(int x=size; x>=0; x--){
if(temp.get(x).num==((Parent)comboA.getSelectedItem()).num){
/*donothing*/
System.out.println("donothing: temp.get("+x+")="+temp.get(x));
}else{
System.out.println("temp.remove("+x+")="+temp.get(x));
temp.remove(x);
}
}
removeDuplicate(comboB, temp);
}
public ArrayList copyArrayList(ArrayList list){
ArrayList<Object> temp = new ArrayList<>();
int size = list.size();
for(int x=0; x<size; x++){
temp.add(list.get(x));
}
return temp;
}
public MainClass(){
parents.add(parent1);
parents.add(parent2);
grandkids.add(grandkid1);
grandkids.add(grandkid2);
grandkids.add(grandkid3);
grandkids.add(grandkid4);
frame = new JFrame("frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pane = new JPanel();
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
comboA = new JComboBox();
fillCombo(parents, comboA);
c.gridx = 0;
c.gridy = 0;
comboA.addActionListener((ActionEvent eventComboA) -> {
System.out.println("eventComboA");
JComboBox comboA1 = (JComboBox) eventComboA.getSource();
if(comboA.getItemCount()>0){
System.out.println("comboA.getItemCount>0: "+(Parent)comboA1.getSelectedItem());
fillComboB();
}
});
pane.add(comboA, c);

comboB = new JComboBox();
c.gridx = 0;
c.gridy = 1;
comboB.addActionListener((ActionEvent eventComboB) -> {
System.out.println("eventComboB");
JComboBox comboB1 = (JComboBox) eventComboB.getSource();
if(comboB.getItemCount()>0){
System.out.println("comboB.getItemCount>0: "+(Grandchild)comboB1.getSelectedItem());
}
});
pane.add(comboB, c);

frame.setContentPane(pane);
frame.setSize(150,150);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private static void runGUI(){
JFrame.setDefaultLookAndFeelDecorated(false);
MainClass create = new MainClass();
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(() -> {
runGUI();
});
}
}

关于java - JCombo + 覆盖 toString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26222295/

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