简体   繁体   中英

Dynamically create list of jpanels swing/java

I am attempting to create panel, PluginListPanel (extending JPanel), which will show a list of plugin panels which will respect the preferred height of the plugin's panel, but will force the width. I have a solution, but it is slow and has a weird bug. In the screenshot, there are two such panels, one to the left and one to the right:

在此处输入图片说明

I don't know different layout manager systems very well, but I know that the TOP field in the BorderLayout does what I want. So, I came up with this 'recursive' solution:

public PluginListPanel(List<PanelContainer> items) {        
    JPanel body = this;
    for (PanelContainer item : items) {
        body.setLayout(new BorderLayout(0, 0));
        JPanel panel = new PluginOnePanel(item);
        body.add(panel, BorderLayout.NORTH);

        JPanel newBody = new JPanel();
        body.add(newBody, BorderLayout.CENTER);
        body = newBody;
    }
}

The problem I encounter is when I scroll, the system responds somewhat slowly, and the colour of the SamplePluginPanels is different (see picture below), even if the number of SamplePluginPanels is as low as 8.

The question is, how can I make this more elegant, not slow down the program and not miscolour the panels?

Any insights are highly appreciated.

在此处输入图片说明

I think a vertical Box is the answer (actually 2, on both columns):

Box leftBox = Box.createVerticalBox();
Box rightBox = Box.createVerticalBox();

/* put them together */

setLayout(new GridLayout(2,1));
add(leftBox);
add(rightBox);

Also make sure, that the content of the scroll pane implements Scrollable and returns true , to scrollableTracksViewportWidth() . This will force equal widths.

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