Adding elements to a JList

后端 未结 1 911
抹茶落季
抹茶落季 2020-12-10 18:19

I have an array of objects that contain the name of customers, like this: Customers[]

How I can add those elements to an existing JList automatically af

相关标签:
1条回答
  • 2020-12-10 19:13

    The add method you are using is the Container#add method, so certainly not what you need. You need to alter the ListModel, e.g.

    DefaultListModel<String> model = new DefaultListModel<>();
    JList<String> list = new JList<>( model );
    
    for ( int i = 0; i < customers.length; i++ ){
      model.addElement( customers[i].getName() );
    }
    

    Edit:

    I adjusted the code snippet to add the names directly to the model. This avoids the need for a custom renderer

    0 讨论(0)
提交回复
热议问题