简体   繁体   中英

Load a JList on a button click

I want to show a JList whenever user clicks on a button. Here is my code on button click event:

public void loadListBtnActionPerformed(java.awt.event.ActionEvent evt){
       JList myJList = new javax.swing.JList();
        myJList.setVisibleRowCount(10);
        jPanel7.add(myJList);
        jPanel7.revalidate();
        jPanel7.repaint();
}

The problem is it is not showing any list on button click. How to add the list on button click?

Assuming that you're using the default FlowLayout for jPanel7 (and GroupLayout for the JFrame layout), the JList will not appear as it doesn't contain any elements so its preferred size will be 0x0 .

To allow the JList content to be scrollable you should place it in a JScrollPane . This will make it appear even when it is initially empty:

jPanel7.add(new JScrollPane(myJList));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM