learner
learner

Reputation: 39

How to populate JComboBox using a method that accepts string as combobox name

I would like to populate multiple JComboBox in a GUI. So I would like to use a method that accepts each JComboBox name as an argument. Is this possible?

cmbCustomers.addItem("customer name");

In the code above I would like to replace cmbCustomers with any possible string that is passed to the populate method.

I have tried to pass a string argument from a populate method to replace the cmbCustomers comboboxname dynamically, but it really doesn't seem possible.

I'm making a GUI that populates JTables with data from a database. Selecting a row in a particular JTable populates a set of JComboBoxes with the selected row's data. From here the data can be changed using the JComboBoxesand saved back to the database. Because of the multiple JComboBoxesI'd like to avoid duplicating so much code.

Upvotes: 0

Views: 429

Answers (2)

FredK
FredK

Reputation: 4084

Why do you want to dynamically create the code name for anything? It is extremely rare that there would be any reason to do so. What difference does it make if your call the combobox "box1" or "mybox" ? How will you write the code to manipulate the content of the combobox if you do not know the variable name of the combobox until run-time?

That said, there is a setName() and a getName() method for all Components, and you can certainly modify that internal name at any time. Then you can do things like:

  if ( box1.getName().equals(something) ) {
    fillCustomers( box1, customers );
  } else if ( box2.getName().equals( something ) {
    fdillCustomers( box2, customers );
  }

Admittedly, this is a rather poor design to use to modify what types of things are in a particular combobox, but it can be done.

Upvotes: 0

Code-Apprentice
Code-Apprentice

Reputation: 83527

Generally, Java variable names are only available at compile time. You cannot use a String value as a variable name at run time1. Instead, you should use general Object Oriented principles to solve your problem. In this case, you can write a method which accepts a JComboBox parameter:

private void populateComboBox(JComboBox comboBox) {
    comboBox.addItem("customer name");
    // ... do anything else you wish with the comboBox
}

Now you can call this method with any JComboBox. Note that the name of the parameter passed to the method does not matter. It can be anything you want. For example:

JComboBox cmbCustomers = new JComboBox();
populateComboBox(cmbCustomers);

or

JComboBox cmbOtherCustomers= new JComboBox();
populateComboBox(cmbOtherCustomers);

I strongly suggest that you learn as much as you can about methods and reference variables. These concepts are critical to Java programming and will help you understand my suggestion above as well as help you use them to solve similar problems in the future.

1 Technically, you can use the Reflection API to do this. Reflection is intended for code introspection in code tools, not for standard day-to-day programming. You should definitely not use it to solve common problems such as the one asked about here.

Upvotes: 1

Related Questions