Use type parameters.

This commit is contained in:
Michael Bayne
2025-01-30 13:52:22 -08:00
parent d91abf6649
commit e9efe7ca49
2 changed files with 7 additions and 9 deletions
@@ -16,9 +16,7 @@ import javax.swing.ListModel;
* can be collapsed. Each section of a list uses a {@link JList} instance * can be collapsed. Each section of a list uses a {@link JList} instance
* to render the section elements. * to render the section elements.
*/ */
@SuppressWarnings({ "unchecked", // we build with 1.5 which did not have parameterized JList public class CollapsibleList<E> extends JPanel
"rawtypes" })
public class CollapsibleList extends JPanel
{ {
/** /**
* Constructs an empty collapsible list. * Constructs an empty collapsible list.
@@ -36,7 +34,7 @@ public class CollapsibleList extends JPanel
/** /**
* Constructs a collapsible list with the supplied section labels and models. * Constructs a collapsible list with the supplied section labels and models.
*/ */
public CollapsibleList (List<String> sections, List<ListModel> models) public CollapsibleList (List<String> sections, List<ListModel<E>> models)
{ {
this(); // set up our layout manager this(); // set up our layout manager
@@ -61,10 +59,10 @@ public class CollapsibleList extends JPanel
* *
* @return the index of the newly added section. * @return the index of the newly added section.
*/ */
public int addSection (String label, ListModel model) public int addSection (String label, ListModel<E> model)
{ {
add(new JLabel(label)); add(new JLabel(label));
add(new JList(model)); add(new JList<E>(model));
return getSectionCount()-1; return getSectionCount()-1;
} }
@@ -80,9 +78,9 @@ public class CollapsibleList extends JPanel
/** /**
* Returns the list object associated with the specified section. * Returns the list object associated with the specified section.
*/ */
public JList getSectionList (int index) public JList<E> getSectionList (int index)
{ {
@SuppressWarnings("unchecked") JList list = (JList)getComponent(index*2+1); @SuppressWarnings("unchecked") JList<E> list = (JList<E>)getComponent(index*2+1);
return list; return list;
} }
@@ -33,7 +33,7 @@ public class TestComboButtonBox
JFrame frame = new JFrame("Test ComboButtonBox"); JFrame frame = new JFrame("Test ComboButtonBox");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DefaultComboBoxModel model = new DefaultComboBoxModel(); DefaultComboBoxModel<Image> model = new DefaultComboBoxModel<Image>();
model.addElement(createImage(Color.blue)); model.addElement(createImage(Color.blue));
model.addElement(createImage(Color.green)); model.addElement(createImage(Color.green));
model.addElement(createImage(Color.red)); model.addElement(createImage(Color.red));