0

I have combobox which is filled with some items. Each item have display member and value member:

    Vector model = new Vector();
    model.addElement(new FilterValue("10000 Hz", 0));
    model.addElement(new FilterValue("5000 Hz", 1));
    model.addElement(new FilterValue("1000 Hz", 5));
    model.addElement(new FilterValue("100 Hz", 50));
    model.addElement(new FilterValue("10 Hz", 500));
    model.addElement(new FilterValue("1 Hz", 5000));

public class FilterValue {
    private final String label;
    private final int value;

    public FilterValue(String label, int value) {
        this.label = label;
        this.value = value;
    }

    public String getLabel() {
        return label;
    }

    public int getValue() {
        return value;
    }

    public String toString()
    {
        return label;
    }
}

Initialization of JComboBox

    cbFilter1 = new JComboBox(model);       
    cbFilter1.setBounds(176, 70, 90, 20);       
    cbFilter1.setSelectedIndex(-1);
    pnlOUT1.add(cbFilter1);

    cbFilter1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JComboBox comboBox = (JComboBox)e.getSource();
            FilterValue item = (FilterValue)comboBox.getSelectedItem();
            System.out.println( item.getValue() + " : " + item.getLabel() );
        }
    });

When I select for example 5000 Hz, display text is 5000 Hz, and value is 1.

The question is how to set value for example 5 and display 1000 Hz?

I have tried with

cbFilter1.setSelectedItem(5);

But there is no effect.

Josef
  • 2,648
  • 5
  • 37
  • 73
  • So should it say 1000hz or 5? You contradict yourself. Just need the exacts – basic Jul 13 '16 at 13:04
  • This could be achieved by creating a method that iterates the combo's `FilterValue` members, casts them back to to a `FilterValue` and checks the `value` attribute. – Andrew Thompson Jul 13 '16 at 13:20
  • `cbFilter1.setBounds(176, 70, 90, 20); ` Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Jul 13 '16 at 13:21
  • So your question is that you want to change the selected row of the JComboBox programmatically ? With no user interaction ? – Kapcash Jul 13 '16 at 13:22
  • its about How to use XxxListCellRenderer, then one value is visible into JComboBox, and selection (Item/ActionListener) returns ID, unique value, whatever, many times here (search in post by camickr + Java + Swing + JComboBox + Renderer) – mKorbel Jul 15 '16 at 14:18

4 Answers4

0

So you basically want to select the items either by their value or by their label.

The first (and simplest) thing I could think of to achieve this, is to create a Map object with the FilterValue as value. With this mapping you can simply use setSelectedItem on the ComboBox.

First copy the elements from your Vector:

Map<Integer, FilterValue> valueMap = new HashMap<>();
Map<String, FilterValue> labelMap = new HashMap<>();
model.forEach(filter -> {
  valueMap.put(filter.getValue(), filter);
  labelMap.put(filter.getLabel(), filter);
});

Then you could do something like this

String label = "5000 Hz";
cbFilter1.setSelectedItem(labelMap.get(label));

or this

int value = 5;
cbFilter1.setSelectedItem(valueMap.get(value));

It is then your choice how to collect the value (or label) of the FilterValue (maybe a JTextField or whatever)

Muto
  • 140
  • 1
  • 10
0

Change your selected item:

  cbFilter1 = new JComboBox(model);       
    cbFilter1.setBounds(176, 70, 90, 20);       
    cbFilter1.setSelectedIndex(5); // EDITED
    pnlOUT1.add(cbFilter1);
cbFilter1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        JComboBox comboBox = (JComboBox)e.getSource();
        FilterValue item = (FilterValue)comboBox.getSelectedItem();
        System.out.println( item.getValue() + " : " + item.getLabel() );
    }
});
Oox Hamza
  • 9
  • 1
  • 7
0

So you want the select a row by passing an existing value or label ?

The method setSeletedIndex(int index) expects an int corresponding of the number of the row (here, from 0 to 5).

You could use the method setSelectedItem(Object obj) to select the wanted FilterValue object.

Here is a simple method to help you select the correct FilterValue object from a given value :

/* Could do the same for label, using val.getLabel()
 * and passing a String in parameter
 */
public void selectByValue(int value){
    FilterValue row = null;
    for(FilterValue val : model){ //Searching for the corresponding FilterValue
        if(val.getValue() == value){
            row = val;
        }
    }
    cbFilter1.setSelectedItem(row); //Select the corresponding row
}

When calling this method like this

this.selectByValue(5);

It will search for the FilterValue in your Vector that have the value "5" and select the row of this object. This expects you don't have any values twice or it would select the first that appears.

Tested, and worked ;)

Hope it helped !

Kapcash
  • 6,377
  • 2
  • 18
  • 40
0

Thanks to all, I've found one solution that works fine in my case:

public static void setSelectedValue(JComboBox comboBox, int value)
{
    FilterValue item;
    for (int i = 0; i < comboBox.getItemCount(); i++)
    {
        item = (FilterValue)comboBox.getItemAt(i);
        if (item.getValue() == value)
        {
            comboBox.setSelectedIndex(i);
            break;
        }
    }
}
Josef
  • 2,648
  • 5
  • 37
  • 73