0

I have an ArrayList<ROI2D>, ROI2D contains a getName(); method that returns a string.

I now want these names to show up in a JComboBox so that the user may select a ROI2D based on the name.

Using ArrayList.toArray(); will get the Array[x] of ROI2Ds, but can I perform .getName(); on it somehow and pass that to JComboBox? Or is the only way to create a separate Array containing the Names, and using comparisons afterwards to see which ROI2D the user selected?

3
  • I think overwriting the toString method should do the trick Commented Apr 25, 2018 at 11:20
  • 1
    Can you show your code? Explain the situation better? Currently your question is unclear. The best description would probably be to show the code. Commented Apr 25, 2018 at 11:20
  • 2
    Use objectList.stream().map(s -> s.getName()).collect(Collectors.toList()) Commented Apr 25, 2018 at 11:21

2 Answers 2

2

If you are using Java 8 you can use Streams API.

List<String> stringList = 
             roid2dList.stream().map(n -> n.getName).collect(Collectors.toList());

String[] stringArray = new String[stringList.size()];
stringArray = stringList.toArray(stringArray);

See more examples here: https://www.mkyong.com/java8/java-8-streams-map-examples/

Sign up to request clarification or add additional context in comments.

3 Comments

You do realize that an array is explicitly needed?
Sure. But I thought that converting a List to an array in Java should be common sense. I've edited my answer and added the code.
And to further clarify, he wants the actual objects to be added to the JComboBox but only show the name
1

So what you actually need to do, is to simply overwrite the toString() method to just return the name. I made a simple example to show it:

main method:

public static void main(String[] args) {

    JFrame f = new JFrame();
    List<Client> cs = new ArrayList();

    cs.add(new Client("Hans", 1));
    cs.add(new Client("Peter", 2));
    JComboBox c = new JComboBox<Client>(cs.toArray(new Client[] {}));

    c.addItemListener(new ItemListener() {

        @Override
        public void itemStateChanged(ItemEvent e) {
            System.out.println(((Client) e.getItem()).getAge());
        }

    });

    f.add(c);
    f.pack();
    f.setVisible(true);

}

And the Client class:

public class Client {

    String name;

    int age;

    public Client(String n, int age) {
        name = n;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

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

}

This works perfectly find. Each entry in the JComboBox only shows the name

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.