简体   繁体   English

在按钮上单击加载JList

[英]Load a JList on a button click

I want to show a JList whenever user clicks on a button. 每当用户单击按钮时,我想显示一个JList 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 . 假设你使用的是默认FlowLayoutjPanel7 (和GroupLayoutJFrame布局),在JList不会出现,因为它不包含任何元素,因此它的首选大小将是0x0

To allow the JList content to be scrollable you should place it in a JScrollPane . 要使JList内容可滚动,应将其放在JScrollPane This will make it appear even when it is initially empty: 即使最初为空,这也会使其出现:

jPanel7.add(new JScrollPane(myJList));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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