Thursday, April 18, 2024

Java 17 JList using ListModel and JComboBox using BasicComboBoxRenderer to wrap data (hide id)

Source code: 

com.dedetok.tutorialcustomjlist.Person.java

package com.dedetok.tutorialcustomjlist;

public class Person {
    int id;
    String name;
    
    public Person(int id, String name) {
        this.id = id;
        this.name = name;
    }
}

com.dedetok.tutorialcustomjlist.MyListModel.java

package com.dedetok.tutorialcustomjlist;

import java.util.Vector;
import javax.swing.ListModel;
import javax.swing.event.ListDataListener;

public class MyListModel implements ListModel<String> {
    Vector<Person> myData = new Vector<>();

    public MyListModel(Vector<Person> myData) {
        this.myData = myData;
    }
    
    @Override
    public int getSize() {
        return myData.size();
    }

    // to render in JList
    @Override
    public String getElementAt(int index) {
        Person tmpP = myData.get(index);
        return tmpP.name;
    }

    @Override
    public void addListDataListener(ListDataListener l) {
        // TODO Auto-generated method stub
       
    }

    @Override
    public void removeListDataListener(ListDataListener l) {
        // TODO Auto-generated method stub
       
    }

}

com.dedetok.tutorialcustomjlist.MyCBRenderer.java

package com.dedetok.tutorialcustomjlist;

import java.awt.Component;

import javax.swing.JList;
import javax.swing.plaf.basic.BasicComboBoxRenderer;

public class MyCBRenderer extends BasicComboBoxRenderer {

    // to render to JComboBox
    @Override
    public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected,
            boolean cellHasFocus) {
        // TODO Auto-generated method stub
        super.getListCellRendererComponent(list, value, index, isSelected,
            cellHasFocus);
        Person mPerson = (Person) value;
        setText(mPerson.name);
        return this;
    }
}

com.dedetok.tutorialcustomjlist.TutorialJList.java

package com.dedetok.tutorialcustomjlist;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.SpringLayout;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.JComboBox;

public class TutorialJList {

    private JFrame frame;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    TutorialJList window = new TutorialJList();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public TutorialJList() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        SpringLayout springLayout = new SpringLayout();
        frame.getContentPane().setLayout(springLayout);
       
        Vector<Person> myList = new Vector<>();
        Person tmp = new Person(1,"one");
        myList.add(tmp);
        tmp = new Person(2,"two");
        myList.add(tmp);
        tmp = new Person(3,"Three");
        myList.add(tmp);
       
        MyListModel myDataList = new MyListModel(myList); // implements ListModel<String>
       
        JList<String> list = new JList<>(myDataList);
        list.addListSelectionListener(new ListSelectionListener( ) {

            @Override
            public void valueChanged(ListSelectionEvent e) {
                // TODO Auto-generated method stub
                if (!e.getValueIsAdjusting()) {
                    int selectInt = list.getSelectedIndex();
                    Person tmpP = myDataList.myData.get(selectInt);
                    System.out.println(tmpP.id+" "+tmpP.name);
                }
            }
           
        });


        springLayout.putConstraint(SpringLayout.NORTH, list, 0, SpringLayout.NORTH, frame.getContentPane());
        springLayout.putConstraint(SpringLayout.WEST, list, 0, SpringLayout.WEST, frame.getContentPane());
        springLayout.putConstraint(SpringLayout.EAST, list, 436, SpringLayout.WEST, frame.getContentPane());
        frame.getContentPane().add(list);

        JComboBox<Person> comboBox = new JComboBox<>(myList);
        MyCBRenderer myCBRenderer = new MyCBRenderer();
        comboBox.setRenderer(myCBRenderer);
        ActionListener myAL = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                Person myPerson = (Person) comboBox.getSelectedItem();
                System.out.println(myPerson.id+" "+myPerson.name);
            }
           
        };
        comboBox.addActionListener(myAL);

        springLayout.putConstraint(SpringLayout.WEST, comboBox, 0, SpringLayout.WEST, list);
        springLayout.putConstraint(SpringLayout.SOUTH, comboBox, 0, SpringLayout.SOUTH, frame.getContentPane());
        frame.getContentPane().add(comboBox);
       
    }
}


No comments:

Post a Comment