Search code examples
javajcombobox

java refer to component by a variable name


okay, i'm trying to figure out if there's a way to do the following:

say i have:

jcombobox someCombo1 = new jcombobox();
jcombobox someCombo2 = new jcombobox();

changeSomething(someCombo1);
changeSomething(someCombo2);

i want to be able to refer to this combobox later, but by a variable from a method, say:

public void changeSomething(jcombobox inCombo){
      inCombo.addItem("something")
}

so, that when the "something" item is added, it's added to the someCombo1, someCombo2 comboboxes, is this in any way possible?

am i looking at it wrong? haha

i have some code that manipulates A LOT of comboboxes with a long method body, each time, i want to condense it


Solution

  • I think you're looking for something like,

    public static void changeSomething(JComboBox<String> inCombo){
          inCombo.addItem("something");
    }
    

    Note that it is JComboBox. Since your method doesn't depend on any instance fields I would make it static and you should not use raw-types.