Frage

I want to add each item of List[i] into a different Jlist , for example the first jList would have Hello,bye,good,bad,nice,Ses,Iteme

I want to make 4 different JList,

  • 1st one with Hello,bye,good,bad......
  • 2nd one with 569.99,551.59,678.99,....
  • 3rd one 55,52,72,.... and
  • the last one with jlas,byew,good2

class List
{

    Info[] List;

    public void createList()
    {

        List = new Info[7];

        List[0] = new Info("Hello",569.99,55,"jlas");

        List[1] = new Info("bye",551.59,52,"byew");
        List[2] = new Info("good",678.99,72,"good2");
        List[3] = new Info("bad",4547.959,151,"bad2");
        List[4] = new Info("nice",3554.99,235,"wii-U.jpg");
        List[5] = new CInfo("Ses",1140.99,4,"das");
        List[6] = new Info("Iteme",584.95,5,"sade");    


    }
}
War es hilfreich?

Lösung

Take all the values add them to a ListModel

DefaultListModel model = new DefaultListModel();
for (Info info : List) {
    model.addElement(model);
}

Then add this model to each of your JLists...

JList list1 = new JList(model);
JList list2 = new JList(model);
JList list3 = new JList(model);
JList list4 = new JList(model);

Now, here comes the funky part, create a ListCellRenderer for each different way you want to display the data...

import java.awt.Component;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JList;

public class ListTest {

    public static void main(String[] args) {

    }

    public class InfoNameListCellRenderer extends DefaultListCellRenderer {

        @Override
        public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            if (value instanceof Info) {
                Info info = (Info)value;
                value = info.getName(); // Or what ever getter you have available for such things
            }
            return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); 
        }



    }

}

And apply it to the appropriate list...

list1.setCellRenderer(new InfoNameListCellRenderer());

Remember, it's the models responsibility to model the data, it's the renderer's responsibility to renderer it. Don't change the model to meet your rendering requirements.

Take a look at Concepts: Editors and Renderers and Writing a Custom Cell Renderer for more details

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top