Ok, here is a first sample. It's really small, maybe not 100% real-world, but should be useful to show what can be gained from generics.
-Florian Am Dienstag, 3. März 2009 schrieb Alexander Potochkin: > Hello Tom > > It's nice to see you here > > >> Could you please provide a complete example of a JList > >> with a custom ListCellRenderer that proves that renderer should be > >> generified > > > > I bet if you used NetBeans to find implementations of ListCellRenderer > > even within the JDK most useful implementations would cast the value > > argument. > > anyway, it is always better to have a fixed set of examples to discuss > > > (I prefer option 3, btw.) > > Thanks > alexp > > > Tom Hawtin
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package listsample1;
/**
*
* @author puce
*/
public class Participant {
private final String firstName;
private final String lastName;
private final int age;
public Participant(String firstName, String lastName, int age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return firstName + " " + lastName + " (" + age + ")";
}
}
ListSample1Frame.form
Description: XML document
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* ListSample1Frame.java
*
* Created on 03.03.2009, 23:02:38
*/
package listsample1;
import java.awt.Component;
import java.util.Vector;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
/**
*
* @author puce
*/
public class ListSample1Frame extends javax.swing.JFrame {
/** Creates new form ListSample1Frame */
public ListSample1Frame() {
initComponents();
Vector<Participant> participants = new Vector<Participant>();
// some fictional participants
participants.add(new Participant("Carl", "Smith", 25));
participants.add(new Participant("Lisa", "Collins", 27));
participants.add(new Participant("Martin", "Brooks", 34));
participantsList.setListData(participants);
participantsList.setCellRenderer(new ListCellRenderer() {
private final ListCellRenderer delegateRenderer =
new DefaultListCellRenderer();
public Component getListCellRendererComponent(JList list,
Object value, int index, boolean isSelected,
boolean cellHasFocus) {
// cast needed because ListCellRenderer doesn't use generics
Participant participant = (Participant) value;
// something different than Participant.toString()
String participantDisplayString = participant.getLastName() +
", " + participant.getFirstName();
return delegateRenderer.getListCellRendererComponent(list,
participantDisplayString,
index, isSelected, cellHasFocus);
}
});
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
participantsLabel = new javax.swing.JLabel();
participantsScrollPane = new javax.swing.JScrollPane();
participantsList = new javax.swing.JList();
participantPanel = new javax.swing.JPanel();
firstNameCaptionLabel = new javax.swing.JLabel();
lastNameCaptionLabel = new javax.swing.JLabel();
ageCaptionLabel = new javax.swing.JLabel();
firstNameLabel = new javax.swing.JLabel();
lastNameLabel = new javax.swing.JLabel();
ageLabel = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("List Sample 1");
participantsLabel.setLabelFor(participantsList);
participantsLabel.setText("Participants:");
participantsList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
participantsList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
participantsListValueChanged(evt);
}
});
participantsScrollPane.setViewportView(participantsList);
participantPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Participant"));
firstNameCaptionLabel.setText("First Name:");
lastNameCaptionLabel.setText("Last Name:");
ageCaptionLabel.setText("Age:");
javax.swing.GroupLayout participantPanelLayout = new javax.swing.GroupLayout(participantPanel);
participantPanel.setLayout(participantPanelLayout);
participantPanelLayout.setHorizontalGroup(
participantPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(participantPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(participantPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(ageCaptionLabel)
.addComponent(lastNameCaptionLabel)
.addComponent(firstNameCaptionLabel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(participantPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(firstNameLabel)
.addComponent(lastNameLabel)
.addComponent(ageLabel))
.addContainerGap(505, Short.MAX_VALUE))
);
participantPanelLayout.setVerticalGroup(
participantPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(participantPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(participantPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(firstNameCaptionLabel)
.addComponent(firstNameLabel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(participantPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lastNameCaptionLabel)
.addComponent(lastNameLabel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(participantPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(ageCaptionLabel)
.addComponent(ageLabel))
.addContainerGap(130, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(participantPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(participantsLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(participantsScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(participantsScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(participantsLabel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(participantPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void participantsListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_participantsListValueChanged
if (!evt.getValueIsAdjusting()) {
if (participantsList.isSelectionEmpty()) {
firstNameLabel.setText("");
lastNameLabel.setText("");
ageLabel.setText("");
} else {
// cast needed because JList doesn't use generics
Participant participant = (Participant) participantsList.
getSelectedValue();
firstNameLabel.setText(participant.getFirstName());
lastNameLabel.setText(participant.getLastName());
// cast needed because ListModel doesn't use generics
Participant participantFromModel = (Participant) participantsList.getModel().
getElementAt(participantsList.getSelectedIndex());
ageLabel.setText(Integer.toString(participantFromModel.getAge()));
}
}
}//GEN-LAST:event_participantsListValueChanged
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ListSample1Frame().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel ageCaptionLabel;
private javax.swing.JLabel ageLabel;
private javax.swing.JLabel firstNameCaptionLabel;
private javax.swing.JLabel firstNameLabel;
private javax.swing.JLabel lastNameCaptionLabel;
private javax.swing.JLabel lastNameLabel;
private javax.swing.JPanel participantPanel;
private javax.swing.JLabel participantsLabel;
private javax.swing.JList participantsList;
private javax.swing.JScrollPane participantsScrollPane;
// End of variables declaration//GEN-END:variables
}
