I'm new in Java, using JList
with DefaultListModel
and getListCellRendererComponent
, should we set cell rendering every time we add or remove element in JList
? Sorry for my English hope you understand my question.
Here is the code:
private JList <Detail>' createListdetail(String s) {
model = new DefaultListModel<>();
try {
DbConnection.connDB();
PreparedStatement psm=DbConnection.getCon().prepareStatement("SELECT * FROM produit WHERE CONVERT(NVARCHAR(MAX),[designation]) LIKE ?+'%'");
psm.setString(1, s);
rs=psm.executeQuery();
while (rs.next()) {
model.addElement(new Detail(rs.getString(1).trim(), rs.getString(3).trim(),"Réf: "+rs.getString(2).trim(), rs.getString(6)));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
list = new JList<Detail>(model);
list.setValueIsAdjusting(true);
list.setCellRenderer(new Produitdetail());
validate();
repaint();
return list;
}
And for cell renderer:
@Override
public Component getListCellRendererComponent(JList<? extends Detail> list, Detail detail, int arg2, boolean isselected,
boolean hasfocus) {
// TODO Auto-generated method stub
txtdesignation.setText(detail.getDesign());
txtpa.setText(detail.getDernierPA());
txtquantite.setText(detail.getQuantite());
txtid.setText(detail.getId());
return this;
}
And third class detail for setter and getter; the first time I run everything its okay but how to add or remove and get the same renderer?