如何将多个侦听器添加到同一个JList

时间:2014-05-10 21:47:48

标签: java user-interface actionlistener

我在Java中创建了一个GUI,但现在我必须以这样的方式向JList的元素添加侦听器,以便根据JList中的选定项来更改GUI的一部分。我能怎么做?

这就是我现在所拥有的。 http://www.mediafire.com/view/7e96723qdig9jq4/2.png

这是该计划应该做的。 http://www.mediafire.com/view/11tate8v9b6a0q9/2m.png

正如您可以通过选择左侧的Claire项目从图像中看到的那样,右侧字段(JTextField)将会更改。 目前,要在右侧字段中输入的值为" static",将从数据库中获取以下值。

这是我的代码:

public class Inbox {

    JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    JPanel titlePanel = new JPanel();
    JPanel centerPanel = new JPanel();
    JPanel listPanel = new JPanel();
    JPanel listRicPanel = new JPanel();
    JPanel listInvPanel = new JPanel();
    JPanel messPanel = new JPanel();
    JPanel labelPanel = new JPanel();
    JPanel inputPanel = new JPanel();
    JPanel buttonPanel = new JPanel();

    Hashtable<String, String[]> subItems = new Hashtable<String, String[]>();
    JComboBox<String> tendina;
    JList<String> subList;

    JTextField mittente = new JTextField(40);
    JTextField oggetto = new JTextField(40);
    JTextArea body = new JTextArea(); 
    JLabel mittDestL = new JLabel("Mittente:");

    GridBagConstraints c;

    String ricevuti[] = {"Alex", "Ben", "Claire", "Dana", "Ellen", "Felicia", "Gary", "Hailey", "Imogen", "Jay", "Kate", "Alex", "Ben", "Claire", "Dana", "Ellen", "Felicia", "Gary", "Hailey", "Imogen", "Jay", "Kate"};
    //JList ricList = new JList(ricevuti);

    String inviati[] = {"Lara", "Megan", "Nikole", "Oscar", "Paige", "Quentin", "Ralph", "Sara", "Thelma", "Victoria", "William"};
    //JList invList = new JList(inviati);

    String ricInv[] = {"Seleziona un item..", "Ricevuti", "Inviati"};

    Inbox() {
        frame.setTitle("Inbox");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(panel);
      frame.setVisible(true);

        panel.setLayout(new BorderLayout());
        centerPanel.setLayout(new BorderLayout());
        listPanel.setLayout(new GridBagLayout());
        messPanel.setLayout(new GridBagLayout());

        panel.add(titlePanel, BorderLayout.NORTH);
        panel.add(buttonPanel, BorderLayout.SOUTH);
        panel.add(centerPanel, BorderLayout.CENTER);

        centerPanel.add(listPanel, BorderLayout.WEST);
        centerPanel.add(messPanel, BorderLayout.CENTER);

        /*** Tendina ***/
        tendina = new JComboBox<String>(ricInv);
        tendina.setEditable(false);
        c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridwidth = 1;
        c.gridx = 0;
        c.gridy = 0;
        c.insets = new Insets(-295, 10, 0, 0); 
        c.weightx = 1;
        listPanel.add(tendina, c);

        /*** Lista dinamica ***/
        subList = new JList<String>();
        subList.setVisibleRowCount(16);
        subList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        JScrollPane scrollList = new JScrollPane(subList);
        subItems.put(ricInv[1], ricevuti);
        subItems.put(ricInv[2], inviati);
        c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridwidth = 1;
        c.gridx = 0;
        c.gridy = 0;
        c.insets = new Insets(30, 10, 0, 0); //top, left, bottom, right
        c.weightx = 1;
        listPanel.add(scrollList, c);

         /*** Messaggio ***/

        //mittente o destinatario
        c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridwidth = 1;
        c.gridx = 0;
        c.gridy = 1; //0
        c.insets = new Insets(0, 32, 0, 0);
        c.weightx = 1;
        messPanel.add(mittDestL, c);

        mittente.setEditable(false);
        c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridwidth = 3;
        c.gridx = 1;
        c.gridy = 1;
        c.insets = new Insets(5, 10, 5, 10);
        c.weightx = 1;
        messPanel.add(mittente, c);

        //oggetto
        JLabel oggL = new JLabel("Oggetto:");
        c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridwidth = 1;
        c.gridx = 0;
        c.gridy = 2;
        c.insets = new Insets(-3, 34, 0, 0);
        c.weightx = 1;
        messPanel.add(oggL, c);

        oggetto.setEditable(false);
        c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridwidth = 3;
        c.gridx = 1;
        c.gridy = 2;
        c.insets = new Insets(5, 10, 5, 10);
        c.weightx = 1;
        messPanel.add(oggetto, c);

        //messaggio
        JLabel messL = new JLabel("Messaggio:");
        c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridwidth = 1;
        c.gridx = 0;
        c.gridy = 3; //3
        c.insets = new Insets(-235, 18, 0, 0);
        c.weightx = 1;
        messPanel.add(messL, c);

        body.setEditable(false);
        JScrollPane scroll = new JScrollPane(body);

        body.setLineWrap(true);
        body.setWrapStyleWord(true);
        scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        scroll.setPreferredSize(new Dimension(250, 250)); 
        c = new GridBagConstraints();
        c.anchor = GridBagConstraints.NORTH;
        c.fill = GridBagConstraints.BOTH;
        c.gridwidth = 3;
        c.gridx = 1;
        c.gridy = 3;//3
        c.insets = new Insets(5, 10, 5, 10); 
        c.weightx = 1;
        messPanel.add(scroll, c);


        frame.pack();

        //listener
        ricInv();
    }

    public void ricInv() {
        tendina.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String item = (String)tendina.getSelectedItem();
                Object o = subItems.get(item);
                if(o == null)
                    subList.setModel(new DefaultComboBoxModel());
                else
                    subList.setModel(new DefaultComboBoxModel((String[])o));
            }
        });
    }

}

这看起来很复杂,因为我使用了很多不同的面板。

最初我有两个不同的JList(ricList和invList)因为我没有JComboBox所以我一起展示了两个JList。 但现在我有一个JList,其内容根据JComboBox中选择的内容而变化。

如何根据JComboBox中的选择为JList的每个元素添加一个侦听器?

我希望我已经清楚地解释了我会得到什么,如果没有,我道歉并准备提供更多细节。

谢谢

0 个答案:

没有答案