如何更改具有相同索引的两个组合框的项-java



我有两个组合框,一个用于选择'number',另一个用于选择'name'。当我从编号中选择一个项目时,必须根据编号的索引更改名称,反之亦然

点击这里查看图片

 private void nameItemStateChanged(java.awt.event.ItemEvent evt) 
    {                                                
         int i = name.getSelectedIndex();
         number.setSelectedIndex(i);
        //It will changes number according to name  
        //Used ItemEvent        
    }  

private void numberItemStateChanged(java.awt.event.ItemEvent evt) 
   {                                                
         int i = number.getSelectedIndex();
         name.setSelectedIndex(i);
         //It will change name according to number
         //used ItemEvent
    }  

这些代码都是例外,因为两者都使用itemstatechange事件..所以建议另一个方法..

您似乎为类提供了两个具有相同签名的方法,并且编译不允许您这样做。如果是这样,那么解决方案就是不要这样做。如果您绝对需要两个jcombobox以一对一的关系紧密地绑在一起(这是我不建议您这样做的),那么要么为每个组合框提供自己的ChangeListener,或者更好的是,让它们共享相同的模型,但为每个组合框使用不同的呈现器。

要获得更具体的答案,请更新您的问题,包括发布有效的最小示例程序或SSCCE(请查看链接)。

例如,这里有两个共享相同模型的jcombobox,一个包含"Customer"对象的模型。Customer类只有两个私有实例字段,一个是int"id"字段,一个是String"name"字段。所以两个组合框都保存着相同的数据

但是它们通过使用不同的单元格呈现器以不同的方式显示数据,一个单元格呈现器只显示id,另一个单元格呈现器只显示名称。您只需要向其中一个添加一个ChangeListener,并且如果其中一个组合框的选择被更改,它将被激活,因为更改一个组合框会更改另一个组合框(它们再次共享相同的模型)。

import java.awt.Component;
import java.awt.event.ItemEvent;
import javax.swing.*;
@SuppressWarnings("serial")
public class LinkedComboEg extends JPanel {
    private DefaultComboBoxModel<Customer> comboModel = new DefaultComboBoxModel<>();
    private JComboBox<Customer> idCombo = new JComboBox<>(comboModel);
    private JComboBox<Customer> nameCombo = new JComboBox<>(comboModel);
    public LinkedComboEg() {
        comboModel.addElement(new Customer(1001, "Doc"));
        comboModel.addElement(new Customer(1002, "Donald Trump"));
        comboModel.addElement(new Customer(1003, "Bashful"));
        comboModel.addElement(new Customer(1004, "Grumpy"));
        comboModel.addElement(new Customer(1005, "Sneezy"));
        comboModel.addElement(new Customer(1006, "Happy"));
        comboModel.addElement(new Customer(1007, "Sleepy"));
        idCombo.setRenderer(new IdRenderer());
        nameCombo.setRenderer(new NameRenderer());
        nameCombo.setSelectedIndex(-1); // none selected to start
        // add an item listener to just one combo box. Either will do
        idCombo.addItemListener(ie -> {
            Customer selection = (Customer) ie.getItem();
            if (ie.getStateChange() == ItemEvent.SELECTED && selection != null) {
                System.out.printf("Selected id: %s;   Selected name: %s%n", 
                        selection.getId(),
                        selection.getName());
            }
        });
        add(new JLabel("ID Number:"));
        add(idCombo);
        add(Box.createHorizontalStrut(20));
        add(new JLabel("Name:"));
        add(nameCombo);
    }
    // renderer to add to the idCombo so that it displays the Customer's ID
    private class IdRenderer extends DefaultListCellRenderer {
        @Override
        public Component getListCellRendererComponent(JList<?> list, Object value, int index,
                boolean isSelected, boolean cellHasFocus) {
            if (value == null) {
                value = "";
            } else {
                value = ((Customer) value).getId();
            }
            return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
        }
    }
    // renderer to add to the nameCombo so that it displays the Customer's Name
    private class NameRenderer extends DefaultListCellRenderer {
        @Override
        public Component getListCellRendererComponent(JList<?> list, Object value, int index,
                boolean isSelected, boolean cellHasFocus) {
            if (value == null) {
                value = "";
            } else {
                value = ((Customer) value).getName();
            }
            return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
        }
    }
    private static void createAndShowGui() {
        LinkedComboEg mainPanel = new LinkedComboEg();
        JFrame frame = new JFrame("Linked Combo Example");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_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();
            }
        });
    }
}

public class Customer {
    private int id;
    private String name;
    public Customer(int id, String name) {
        this.id = id;
        this.name = name;
    }
    public int getId() {
        return id;
    }
    public String getName() {
        return name;
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + id;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Customer other = (Customer) obj;
        if (id != other.id)
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }
}

相关内容

  • 没有找到相关文章