简体   繁体   English

将 Jlist 添加到 JScrollPane

[英]Add a Jlist to a JScrollPane

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.我有一个 JList,我需要将它放在滚动窗格中,因为我从数据库中获取 JList 并且值会大大增加。 I need to be able to scroll them down so I wrote:我需要能够向下滚动它们,所以我写道:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;

import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
public class Checkboxlistener extends JFrame {

private JPanel jpAcc = new JPanel();
private JList checkBoxesJList;

Checkboxlistener() {
   // super("Deposit base", false, true, false, true);
    setSize(1300, 600);
    jpAcc.setLayout(null);
    jpAcc.setBackground(Color.LIGHT_GRAY);
    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);
    JScrollPane scrollPane = new JScrollPane(checkBoxesJList);

    jpAcc.add(checkBoxesJList);
    jpAcc.add(scrollPane);



    getContentPane().add(jpAcc);
    setVisible(true);
}

public static void main(String args[]) {
   Checkboxlistener cbl = new Checkboxlistener();
}
}

What Am I doing wrong because I do not see any ScrollPane?我做错了什么,因为我没有看到任何 ScrollPane?

The list is already contained inside the scrollpane, so you must not add the list to the main panel. 该列表已经包含在滚动窗格中,因此您不得将列表添加到主面板。 Only the scroll pane. 仅滚动窗格。

Another thing you're doing wrong is not using a layout manager , and setting the bounds and sizes of your components. 您做错的另一件事是不使用布局管理器 ,而是设置组件的边界和大小。 Don't do that. 不要那样做 Let the layout manager position and size the components for you. 让布局管理器为您定位和调整组件的大小。

And finally, you shouldn't use Swing components from the main thread. 最后,您不应该使用主线程中的Swing组件。 Only in the event dispatch thread . 仅在事件调度线程中

Here's a modified version of your code that works fine. 这是您的代码的修改后的版本,可以正常工作。 I removed the background colors, as this should be handled by the L&F: 我删除了背景色,因为这应该由L&F处理:

public class Checkboxlistener extends JFrame {

    private JPanel jpAcc = new JPanel();
    private JList<String> checkBoxesJList;

    Checkboxlistener() {
        jpAcc.setLayout(new BorderLayout());
        String labels[] = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" };
        checkBoxesJList = new JList<String>(labels);

        checkBoxesJList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        JScrollPane scrollPane = new JScrollPane(checkBoxesJList);

        jpAcc.add(scrollPane);

        getContentPane().add(jpAcc);
        pack();
    }

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                Checkboxlistener cbl = new Checkboxlistener();
                cbl.setVisible(true);
            }
        });
    }
}

As mentioned, the list is already added to JScrollPane , hence need not be added again.如前所述,该列表已添加到JScrollPane ,因此无需再次添加。 Also, for scroll to work, it needs to define the list method setVisibleRowCount(int) .此外,为了使滚动起作用,它需要定义列表方法setVisibleRowCount(int) I have modified the code above in CheckBoxListener method to make it work.我在CheckBoxListener方法中修改了上面的代码以使其工作。

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);

}

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

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