Transparent JList in JScrollPane all in one class

后端 未结 2 865
南笙
南笙 2020-12-07 04:57

I have a JFrame in which I have drown a background:

 class ImagePanel extends JComponent {
        private Image image;
        public ImagePanel(Image image         


        
2条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-07 05:17

    you should create a simple List Cell Renderer and call it in your listName.setCellRenderer(new YourListCellRenderer());

    Eg:- Your List Cell Renderer Code should something like this

    public class MyListCellRender extends DefaultListCellRenderer {
    
            @Override
            public Component getListCellRendererComponent(JList list1, Object obj, int index, boolean isSelected, boolean isFocus) {
                super.getListCellRendererComponent(list1, obj, index, isSelected, isFocus);
                setForeground(Color.WHITE);
                setOpaque(isSelected);
                return this;
            }
    
    }
    

    then in call it in what or wherever you want like form active event or initial stage in my case I callit in initial Stage which after form create.It mean after initComponent();

        initComponents();
        jList1.setUI(new WebListUI());
        scrollBar.setOpaque(false);
        scrollBar.getViewport().setOpaque(false);
        jList1.setOpaque(false);
        jList1.setCellRenderer(new TransparentListCellRender()); 
    

    This is my Sample

提交回复
热议问题