0

I have a JComboBox that have values build from two parts int-String like this:

01-one
02-two
03-three

So now I have just the String part, and want to setSelectedItem the item with this part, but i think this is not possible, because the values is not matching or not same

myComboBox.setSelectedItem("?" + myString);

So what i want to do is :

myComboBox.setSelectedItem("like myString");

Someone have an idea to set select the item that is LIKE the value in combobox or this is not possible?

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
  • Yes, it's possible -- you will have to create a method that checks if one String is *similar enough* to another String, and perhaps returns either a numeric value or true, and use that to figure out which index to set the selected item to. – Hovercraft Full Of Eels Jan 08 '17 at 21:28
  • mmm, i really don't think for this solution, thank you @HovercraftFullOfEels this can help me :) – Youcef LAIDANI Jan 08 '17 at 21:30
  • Possible duplicate of [How to compare almost similar Strings in Java? (String distance measure)](http://stackoverflow.com/questions/2084730/how-to-compare-almost-similar-strings-in-java-string-distance-measure) – Hovercraft Full Of Eels Jan 08 '17 at 21:45

2 Answers2

3

You could try using the .contains method where you see whether the first item in the combo box contains that specific word and repeat it until you find the specific index.

For example:

if (jComboBox1.getItemAt(0).toString ().contains ("two")) 
{ 
    jComboBox1.setSelectedIndex(0);
}

And repeat the step or try using a for loop, it's great if your combo box contains many items.

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
1

If you want to be able to select exactly by the string, which is what you indicate in the description, I would create a class to represent your specific item, maintaining in it the int and string as separate fields, and override toString() to return the representation you want. You can then have a method searching your item based on the string alone. For a relatively small number of items, this is efficient and simple. If you had a large number of items, I'd suggest storing them as values in a HashMap using the string as a key.

import java.awt.BorderLayout;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JComboBox;
import javax.swing.JFrame;

class Item {
  int intValue;
  String strValue;

  public Item(int intValue, String strValue) {
    this.intValue = intValue;
    this.strValue = strValue;
  }

  public String toString() {
    return intValue + " - " + strValue;
  }
}

public class TestCombo {
  private static JComboBox<Item> cb;
  public static void main(String[]args) {
    JFrame f = new JFrame();
    f.setSize(640,400);
    cb = new JComboBox<>();
    cb.addItem(new Item(1, "one"));
    cb.addItem(new Item(2, "two"));
    cb.addItem(new Item(3, "three"));
    f.getContentPane().setLayout(new BorderLayout());
    f.getContentPane().add(cb, BorderLayout.NORTH);
    f.setVisible(true);

    selectItemByString("three");
  }

  private static void selectItemByString(String s) {
    for (int i=0; i<cb.getItemCount(); i++) {
      if (cb.getItemAt(i).strValue.equals(s)) {
        cb.setSelectedIndex(i);
        break;
      }
    }
    return;
  }
}
Roberto Attias
  • 1,883
  • 1
  • 11
  • 21