Automatic packaging using MigLayout

I want to wrap JPanel when it reaches the "edge" of the screen using MigLayout. At the moment, I have a JScrollPane (which I only want to be enabled by default). JScrollPane contains any number of JPanels that are arranged horizontally - when adding a panel so that jpanel is removed from the edge that I want to add to the next line. Is it possible?

This is the code:

public void setupPanels(){ JScrollPane scrollPane = new JScrollPane(); JPanel mainPanel = new JPanel(new MigLayout("insets 2")); for (Object object : objects){ JPanel subPanel = new JPanel(new MigLayout("insets 0")); mainPanel.add(subPanel, "alignx left, gapx 2px 5px, gapy 2px 2px, top"); } scrollPane.setViewportView(mainPanel); } 

Also, to add an extra coefficient, every time it reaches the edge, I need to add a new / different panel (timeline) - so is there any way to know when it is going to turn on a new line?

Thanks,

+4
source share
1 answer

MigLayout does not have such a function. It is grid-based, and although you can use the nogrid parameter to stream components horizontally or vertically in a range of cells, you cannot force them to flow to the next row or column.

java.awt.FlowLayout contained in the JDK automatically wraps the contained components:

 JPanel mainPanel = new JPanel(new FlowLayout()); mainPanel.add(subPanel1); mainPanel.add(subPanel2); mainPanel.add(subPanel3); ... 

Recommended height is disabled, but there are ways to fix this, see WrapLayout .

Regarding the second requirement:

Also, to add an extra coefficient, every time it reaches the edge, I need to add a new / different panel (timeline) - so is there a way to find when it is going to turn on a new line?

The layout manager should compose components that have already been added to the container, and not add new components based on the layout results. Adding invisible placeholder components for the timeline after each sub-panel that will be displayed by the layout manager on demand may work.

For this, you definitely need a layout manager. For starters, I would recommend taking a FlowLayout source. In the implementation of layoutContainer there is a loop that layoutContainer through all components. After wrapping the string, check if the next component is a timeline placeholder, make it visible, and wrap it again.

+2
source

Source: https://habr.com/ru/post/1444599/


All Articles