Asked  6 Months ago    Answers:  5   Viewed   17 times

I have following data (String):

Course1: A1
Course1: A2
Course2: B1
Course2: B2
Course2: B3
Course2: B4
Course3: C1
Course3: C2

I'd like to create two JComboBox (JComboBox1, JComboBox2) so that JComboBox1 contains Course1,Course2,Course3, etc.

If I select, say, Course2 from JComboBox1 then corresponding B1,B2,B3,B4 should be populated in JComboBox2.

How to implement this? Many Thanks.

 Answers

45

Yes, simply create a DefaultComboBoxModel for each set, and do setModel() on JComboBox2 when JComboBox1 changes.

Addendum: For example,

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ComboTest extends JPanel implements ActionListener, Runnable {

    private final JComboBox combo1 = new JComboBox(
        new String[]{"Course 1", "Course 2", "Course 3"});
    private final JComboBox combo2 = new JComboBox();
    private ComboBoxModel[] models = new ComboBoxModel[3];

    public ComboTest() {
        models[0] = new DefaultComboBoxModel(
            new String[]{"A1", "A2"});
        models[1] = new DefaultComboBoxModel(
            new String[]{"B1", "B2", "B3", "B4"});
        models[2] = new DefaultComboBoxModel(
            new String[]{"C1", "C2"});

        combo2.setModel(models[0]);
        this.add(combo1);
        this.add(combo2);
        combo1.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        int i = combo1.getSelectedIndex();
        combo2.setModel(models[i]);
    }

    @Override
    public void run() {
        JFrame f = new JFrame("ComboTest");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new ComboTest());
    }
}
Tuesday, June 1, 2021
 
TheLovelySausage
answered 6 Months ago
76

You have to use fetchxml to get distinct values, that is the only option.

<fetch top="50" distinct="true" >
  <entity name="leadaddress" >
    <attribute name="stateorprovince" />
    <filter type="and" >
      <condition attribute="stateorprovince" operator="not-null" />
    </filter>
  </entity>
</fetch>

You can use web api endpoint and use the above fetchxml like below:

https://crmdev.crm.dynamics.com/api/data/v9.1/leadaddresses?fetchXml=<fetch top="50" distinct="true" >   <entity name="leadaddress" >     <attribute name="stateorprovince" />     <filter type="and" >       <condition attribute="stateorprovince" operator="not-null" />     </filter>   </entity> </fetch>
Saturday, May 29, 2021
 
Farnabaz
answered 7 Months ago
75

You have to complete all these steps:

  1. An entity must be Activities enabled for it to appear in the Regarding lookup. Enable a custom entity to appear in the Regarding lookup.

  2. An entity must also be enabled for Unified Interface for it to appear in the Regarding lookup. Read more.

  3. Open the App designer, Add > Entities and then select the entity you want to add and complete the addition by including all assets. Reference

enter image description here

enter image description here

enter image description here

Saturday, August 21, 2021
 
Naveen
answered 4 Months ago
26
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.plaf.basic.*;

public class ComboBoxItem extends JFrame implements ActionListener
{
    public ComboBoxItem()
    {
        Vector model = new Vector();
        model.addElement( new Item(1, "car" ) );
        model.addElement( new Item(2, "plane" ) );
        model.addElement( new Item(3, "train" ) );
        model.addElement( new Item(4, "boat" ) );
        model.addElement( new Item(5, "boat aadf asfsdf a asd asd" ) );

        JComboBox comboBox;

        //  Easiest approach is to just override toString() method
        //  of the Item class

        comboBox = new JComboBox( model );
        comboBox.addActionListener( this );
        comboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
        getContentPane().add(comboBox, BorderLayout.NORTH );

        //  Most flexible approach is to create a custom render
        //  to diplay the Item data

        comboBox = new JComboBox( model );
        comboBox.setRenderer( new ItemRenderer() );
        comboBox.addActionListener( this );
        getContentPane().add(comboBox, BorderLayout.SOUTH );
    }

    public void actionPerformed(ActionEvent e)
    {
        JComboBox comboBox = (JComboBox)e.getSource();
        Item item = (Item)comboBox.getSelectedItem();
        System.out.println( item.getId() + " : " + item.getDescription() );
    }

    class ItemRenderer extends BasicComboBoxRenderer
    {
        public Component getListCellRendererComponent(
            JList list, Object value, int index,
            boolean isSelected, boolean cellHasFocus)
        {
            super.getListCellRendererComponent(list, value, index,
                isSelected, cellHasFocus);

            if (value != null)
            {
                Item item = (Item)value;
                setText( item.getDescription().toUpperCase() );
            }

            if (index == -1)
            {
                Item item = (Item)value;
                setText( "" + item.getId() );
            }


            return this;
        }
    }

    class Item
    {
        private int id;
        private String description;

        public Item(int id, String description)
        {
            this.id = id;
            this.description = description;
        }

        public int getId()
        {
            return id;
        }

        public String getDescription()
        {
            return description;
        }

        public String toString()
        {
            return description;
        }
    }

    public static void main(String[] args)
    {
        JFrame frame = new ComboBoxItem();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setVisible( true );
     }

}
Sunday, August 22, 2021
 
bruce
answered 4 Months ago
43

Simply add an ActionListener to the combo box. When actionPerformed is called, you can look up the selected value and call what ever methods you need to.

For example:

chooser.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
        Object selectedValue = chooser.getSelectedValue();
        // carry on with what ever you need
    }
});

Have a look at ...

  • How to write action listeners
  • How to use combo boxes

For more details

Tuesday, August 31, 2021
 
e_i_pi
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share