Add a Jlist to a JScrollPane

后端 未结 2 1257
执念已碎
执念已碎 2021-01-07 04:23

I have a JList that I need to Place inside a scroll Pane because I am getting the JList from the database and the values can increase greatly. I need to be able to scroll th

2条回答
  •  借酒劲吻你
    2021-01-07 04:52

    As mentioned, the list is already added to JScrollPane, hence need not be added again. Also, for scroll to work, it needs to define the list method setVisibleRowCount(int). I have modified the code above in CheckBoxListener method to make it work.

    Checkboxlistener() 
    {   
        setSize(1300, 600);
    
        String labels[] = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"};
        checkBoxesJList = new JList(labels);
        checkBoxesJList.setBounds(10, 30, 80, 600);
        checkBoxesJList.setBackground(Color.LIGHT_GRAY);
        checkBoxesJList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    
        checkBoxesJList.setVisibleRowCount(5);
    
        JScrollPane scrollPane = new JScrollPane(checkBoxesJList);  
    
        jpAcc.add(scrollPane);
    
        add(jpAcc);
    
        setVisible(true);
    
    }
    

提交回复
热议问题