如何用不同的反应创建JcomboBoxes?

时间:2010-12-29 18:21:44

标签: java swing jcombobox

我的问题是SyntaxPanel中的每个JComboBox都是相同的! 我以为我可以迭代comboBox的名称,但这不会改变任何东西。 也许我应该将它添加到Jlist / List,但列表不适用于JComboBox类型..

为了进一步使用,我想迭代SyntaxPanel中的任何JComboBox:

  • 以便我可以从中获取价值
  • 使用JComboBox的值设置默认值cbSyntax
  • 非常感谢您的帮助=)

    包读者;

    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.GridBagLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.LinkedList;
    import java.util.List;
    
    import javax.swing.DefaultComboBoxModel;
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    @SuppressWarnings("serial")
    public class Rename extends JFrame {
        private Integer i=0;
    
        private List<JComboBox> llSyntax = new LinkedList<JComboBox>();
    
        private JPanel jpOption;
        private JComboBox cbOption;
        private JLabel lOption;
    
        private JPanel jpControl;
        private JButton jbRemove;
        private JButton jbAdd;
        private JButton jbUmbenennen;
    
        private JPanel jpSyntax;
        private JComboBox cbSyntax;
        private DefaultComboBoxModel cbmSyntax = new DefaultComboBoxModel(new String[] {"artist","year","album"});
        //private List<JComboBox> lJComboBox = new List<JComboBox>();
    
        private DefaultComboBoxModel cbmOption = new DefaultComboBoxModel(new String[] { "artist", "album", "track" }); 
    
        Rename() {
            this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            this.setSize(500, 200);
            this.setLocation(500, 500);
            this.setPreferredSize(new Dimension(500, 500));
            this.setLayout(new BorderLayout());
    
            OptionPanel();
            SyntaxPanel();
            ControlPanel();
    
            this.setVisible(true);
        }
    
        public void OptionPanel() {
            jpOption = new JPanel();
            this.add(jpOption, BorderLayout.PAGE_START);
    
            lOption = new JLabel();
            lOption.setText("Welcher Background soll aktiv sein?");
            jpOption.add(lOption);
    
            cbOption = new JComboBox(cbmOption);
            cbOption.addActionListener(new alOption());
            jpOption.add(cbOption);
        }
    
        public void SyntaxPanel() {
            //JPanel für Syntax Buttons
            jpSyntax = new JPanel(new GridBagLayout());
            this.add(jpSyntax);
    
            for (i=0; i < 2; i++) {
                cbSyntax = new JComboBox(cbmSyntax);
                cbSyntax.setName("combobox"+i.toString());
                jpSyntax.add(cbSyntax);
    
                //Wie befüllt man comboBox mit arraylist
    //          for (int x = 0; x < alSyntax.size(); x++) {
    //              cbSyntax.addItem(alSyntax.get(x));
    //              {llSyntax.add(cbSyntax);}
    //          }
            }
    
            //System.out.println(cbSyntax.getSelectedItem());
    
        }
    
        public void ControlPanel() {
            jpControl = new JPanel();
            this.add(jpControl, BorderLayout.PAGE_END);
    
            jbAdd = new JButton();
            jbAdd.setText("Add");
            jbAdd.addActionListener(new alAdd());
            jpControl.add(jbAdd);
    
            jbRemove = new JButton();
            jbRemove.setText("Remove");
            jbRemove.addActionListener(new alRemove());
            jpControl.add(jbRemove);
    
            jbUmbenennen= new JButton();
            jbUmbenennen.setText("Umbenennen");
            jbUmbenennen.addActionListener(new alUmbenennen());
            jpControl.add(jbUmbenennen);        
    
        }
    
        private class alOption implements ActionListener {
            @Override //setzt die Umgebung
            public void actionPerformed(ActionEvent e) {
    
                int i = cbOption.getSelectedIndex();
                //Set the Background for the ComboBoxes of the Syntax Panel
            }
        }
    
        private class alAdd implements ActionListener {
            @Override
            public void actionPerformed(ActionEvent e) {
                cbSyntax = new JComboBox(cbmSyntax);
                jpSyntax.add(cbSyntax);
    
                jpSyntax.revalidate();
                jpSyntax.repaint();
    
                // add(buttonList.get(1)); // am schluss einfügen.
            }
        }
    
        private class alRemove implements ActionListener {
            @Override
            public void actionPerformed(ActionEvent e) {
    
                System.out.println("remove");
            }
        }
    
        private class alUmbenennen implements ActionListener {
            @Override
            public void actionPerformed(ActionEvent e) {
    //          for (JComboBox cb : llSyntax) 
    //                  ArrayList<String> test.add(JComboBox.getSelectedItem());
                //
    
                // add(buttonList.get(1)); // am schluss einfügen.
            }
        }
        public static void main(String[] args) {
            @SuppressWarnings("unused")
            Rename ren = new Rename();
    
        }
    
    }
    

    2 个答案:

    答案 0 :(得分:1)

    在你的循环中,

       for (i=0; i < 2; i++) {
                cbSyntax = new JComboBox(cbmSyntax);
                cbSyntax.setName("combobox"+i.toString());
                jpSyntax.add(cbSyntax);
    
            }
    

    声明一个局部变量并将其添加到jpSyntax中,如下所示:

       for (i=0; i < 2; i++) {
                JComboBox box = new JComboBox(cbmSyntax);
                box.setName("combobox"+i.toString());
                jpSyntax.add(box);   
            }
    

    此外,

    cbSyntax = new JComboBox(cbmSyntax);
    jpSyntax.add(cbSyntax);
    
    actionlistener中的

    需要相应地更改为新的局部变量。

    答案 1 :(得分:0)

    您正在添加JComboBoxes的新实例,但您只有一个ComboBox的引用,在构建面板后,它最终指向您创建的最后一个。

    而是创建一个列表并在向面板添加框的同时填充它。