tags:

views:

71

answers:

2

First, i'm new at Java-programming and my native lang is not english, but still i hope to get some help from you all. What I try to do is a simple java-interface with a jComboBox and a jList. I want to poplate to jComboBox with Object-names and when the user select one of the names get the object-id which i will use to populate the jList. It's probably simple but i have bin stuck with this problem all day.

private void loadComboBox() {
        biz.Object object = new biz.Object();
        try {
            ArrayList<biz.Object> arrayOfObjects= object.getAllObjects();// ArrayList of objects
            for (biz.Object o:arrayOfObjects)
            {
                 if (o != null)
                     cbm.addElement(o); //`toString-method


            }
 cb.setModel(cbm); //JComboBox  
+1  A: 

In most of the cases, a swing component can be seen as a multi-level model-view-controller implementor.

From what you said, I understand that you want, when one of your objects is selected in JComboBox, put that obejct in your JList.

First, I would suggest you to take a look at the Swing tutorial for JComboBox.

Then, you'll see that you have some possibilites for handling events sent by JComboBox.

  • Adding an ActionListener to your JComboBox. it will be notified each time an action is performed on your JComboBox, and as a consequence quite intensively. As a consequence, it may not be the best fit.
  • Adding an ItemListener to your JComboBox. it will be notified each time selected item changes. But the number of time it gets called depends upon previous selection status.

I suspect the second alternative is preferable to the first, since it works with combo box model data, instead of relying solely on visible component status (what first do, to a certain extend - less than a MouseListener, of course, but more than second).

Riduidel
+1, for referrencing the tutorial, which should always be the first response. Not only does the tutorial have a working example that is similiar to the current requirement, it will server as a resource for future questions.
camickr
+1  A: 

I'm not 100% sure whether I understand your question -- but it may well be that you don't even need to implement your own CellRenderer. Perhaps the following code is helpful to you?

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class Test extends JPanel implements ItemListener {
    private JComboBox comboBox;
    private JList list;

    public Test() {
        comboBox = new JComboBox();
        list = new JList(new DefaultListModel());

        /* initialize combo box */
        loadComboBox();

        /* listen for combo box selections */
        comboBox.addItemListener(this);

        /* simple layout */
        setLayout(new BorderLayout());
        add(comboBox, BorderLayout.NORTH);
        add(new JScrollPane(list), BorderLayout.CENTER);
    }

    /**
     * Invoked when an item has been selected or deselected by the user.
     */
    public void itemStateChanged(ItemEvent e) {
        if (e.getStateChange() == ItemEvent.SELECTED) {
            /* add item to list */
            ((DefaultListModel) list.getModel()).addElement(e.getItem());
        }
    }

    private void loadComboBox() {
        /* let's fake some content here */
        Object[] objects = { "foo", "bar", "baz", "qux",
                             "quux", "corge", "grault",
                             "garply", "waldo", "fred",
                             "plugh", "xyzzy", "thud" };

        /* put the objects into the combo box */
        comboBox.setModel(new DefaultComboBoxModel(objects));
    }

    public static void main(String[] args) {
        Test test = new Test();

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(test);

        frame.setSize(300, 400);

        frame.setVisible(true);
    }
}
Thomas