Java-添加或删除元素后JList setCellRenderer不会更新

时间:2018-11-04 22:16:08

标签: java swing jlist listcellrenderer

我是Java新手,将JListDefaultListModelgetListCellRendererComponent结合使用,是否应该在每次在JList中添加或删除元素时设置单元格渲染?对不起,我的英文希望您能理解我的问题。

代码如下:

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;

    }


对于单元格渲染器:

    @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;
    }


setter和getter的第三类详细信息;第一次运行一切正常,但是如何添加或删除并获得相同的渲染器?

0 个答案:

没有答案