0

I have a Combo Bx (Dropdown box) with an index range of 0-20. If there anyways I can use that index to specify which object I want data from? All of the objects use the same naming convention obj0, obj1, obj2, etc. Basically something like this...

public abstract class Person {

    private String name;
    private String title;
    private String email;
    private String job;

    public Person(String name, String title, String email, String job){
        this.name = name;
        this.title = title;
        this.email = email;
        this.job = job;
    }
    //Getters and Setters
}



public class main extends javax.swing.JFrame {
...misc code...

private void btn_startActionPerformed(java.awt.event.ActionEvent evt) {                                          

    Person obj0 = new Person("Jon Doe",
                             "Program Coordinator",
                             "jon.doe@test.com",
                             "Faculty");

    Person obj1 = ...
    ...
    Person obj20 = ...

/*
Onclick it uses the index of the current index in the combobox (dropdown)
to specify which object to get the data from.
*/
private void btn_GetActionPerformed(java.awt.event.ActionEvent evt) {

    //Uses the obj naming convention plus the index
    string foo = "obj" + toString(combobox_Name.getSelectedIndex());

    //Fills the textbox using the above string and the getName method
    txtbox_username.setText(ToObject(foo).getName);
}
dbs1crew
  • 5
  • 1
  • 4
  • I'm a bit confused as to what you're asking. Do you have more code to show us? Not sure what a combobox is and not sure what these objects are that you're referencing. – littlespice3 Jul 07 '17 at 21:34
  • Why would you need to do this? – Daedric Jul 07 '17 at 21:34
  • Java does not recognize any sense in which objects have names. Do you mean that `obj0`, `obj1`, etc. are names of *variables* containing references to the objects of interest? In that case, no, you cannot use strings at runtime to access variables by name. You could, however, put the objects of interest into a `Map`, with the strings as the corresponding *keys*. Or put them in a `List` or array and access them directly by numeric index. – John Bollinger Jul 07 '17 at 21:34
  • Put the elements in a list, then index the list. You should really never just end variables in numbers, then try to dynamically reference variables like that. – Carcigenicate Jul 07 '17 at 21:36
  • https://stackoverflow.com/questions/19094845/item-in-jcombobox-instance-of-an-object maybe this would help you? And then you store item in an arraylist? And add classes / variables / toString as required? – Daedric Jul 07 '17 at 21:37

1 Answers1

0

I have created a basic design of what I think you want:

This code creates 20 objects, adds them to a combobox and uses their predefined name when selected to change a textfield.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

class ObjExample {

    String name;

    public ObjExample(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return name;
    }

}

public class Main extends JFrame implements ActionListener {

    JComboBox jcb = new JComboBox();
    JTextField jtf = new JTextField("Text Field");

    public Main() {

        setSize(200, 200);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        for (int i = 0; i <= 20; i++) {
            jcb.addItem(new ObjExample(Integer.toString(i)));
        }

        jcb.addActionListener(this);

        add(jcb);
        add(jtf);

        setVisible(true);
    }

    public static void main(String[] args) {
        new Main();
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        if (e.getSource() == jcb) {
            ObjExample obj = (ObjExample) jcb.getSelectedItem();
            jtf.setText(obj.toString());
        }
    }

}
Daedric
  • 502
  • 4
  • 23