Get your preferred multiline stream size

I cannot determine how to resize some components in the swing GUI. Some custom labels are added to FlowLayout, which does not behave as it should when resizing a dialog box. A panel is created using the jgoodies form structure.

If you use this, where FlowLayout is added to xy (3, y)

FormLayout layout = new FormLayout("r:d, 5px, f:d:g", // columns "p, p, 5px, p, 5px, p") // rows 

Expands FlowLayout and displays a scrollbar
tags_scroll

Using

 FormLayout layout = new FormLayout("r:d, 5px, f:10:g", // columns "p, p, 5px, p, 5px, p") // rows 

FlowLayout uses the available space, and the elements on the second line disappear tags_vanish

I would like to increase the height of each row containing FlowLayout to the current height of the component. Unfortunately, the preferred size always corresponds to the height for one row.
Would a layout be more appropriate? The bold text on the left should be aligned on the right, and then FlowLayout.

Sources

[edit] After you have tried to figure out how to do this in a few weeks, you can resume the urgent question: A set of labels is added to JPanel. This JPanel should use all available spaces horizontally (dialog size minus tag name tag width) and expand vertically as needed. If the JPanel's height becomes larger than the dialog, a vertical scrollbar should appear (the horizontal scrollbar is never visible). A dialog can display several JPanels, which will be displayed one after the other (vertically).

Here's an attempt to use GridBagLayout and WrapLayout :

 public class GridBagLayoutTagPanel extends JPanel { private static final long serialVersionUID = -441746014057882848L; private final int NB_TAGS = 5; public GridBagLayoutTagPanel() { setLayout(new GridLayout()); JPanel pTags = new JPanel(new GridBagLayout()); pTags.setBackground(Color.ORANGE); GridBagConstraints c = new GridBagConstraints(); c.ipadx = 5; c.ipady = 5; int rowIndex = 0; for (int i = 0; i < NB_TAGS; i++) { //add tag name JLabel lTagName = new JLabel(String.format("Tag %s:", i)); lTagName.setFont(lTagName.getFont().deriveFont(Font.BOLD)); c.fill = GridBagConstraints.NONE; c.gridx = 0; c.gridy = rowIndex++; pTags.add(lTagName, c); //add tag values JPanel pTag = new JPanel(new BorderLayout()); pTag.add(new JLabel("+"), BorderLayout.LINE_START); //label used to add new tags pTag.add(getWrapPanel(), BorderLayout.CENTER); //the list of tag values c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 1; pTags.add(pTag, c); } //JScrollPane sp = new JScrollPane(pTags); //sp.setBorder(BorderFactory.createEmptyBorder()); add(pTags); } private static JPanel getWrapPanel() { JPanel p = new JPanel(new WrapLayout(FlowLayout.LEFT, 5, 0)); for (int i = 0; i < 50; i++) { p.add(new JLabel("t" + i)); } return p; } public static void main(String[] args) { JFrame f = new JFrame(); f.getContentPane().add(new GridBagLayoutTagPanel()); f.setSize(new Dimension(500, 300)); f.setVisible(true); } } 
+7
source share
3 answers

With Rob and using WrapPanel and ScrollablePanel it now works exactly as it should

 public class GridBagLayoutTagPanel extends JPanel { private final int NB_TAGS = 5; private final int NB_TAGVALUES = 50; public GridBagLayoutTagPanel() { setLayout(new GridLayout()); ScrollablePanel pTags = new ScrollablePanel(new GridBagLayout()); pTags.setScrollableWidth(ScrollablePanel.ScrollableSizeHint.FIT); pTags.setBackground(Color.ORANGE); GridBagConstraints c = new GridBagConstraints(); c.ipadx = 5; c.ipady = 5; int rowIndex = 0; for (int i = 0; i < NB_TAGS; i++) { // add tag name JLabel lTagName = new JLabel(String.format("Tag %s:", i)); lTagName.setFont(lTagName.getFont().deriveFont(Font.BOLD)); c.fill = GridBagConstraints.NONE; c.gridx = 0; c.gridy = rowIndex++; c.weightx = 0; // keep minimum size of the cell containing the label when resizing c.weighty = 0; pTags.add(lTagName, c); // add tag values JPanel pTag = new JPanel(new BorderLayout()); pTag.add(new JLabel("+"), BorderLayout.LINE_START); // label used to add new tags pTag.add(getWrapPanel(), BorderLayout.CENTER); // the list of tag values c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 1; c.weightx = 1; // fill horizontally c.weighty = 1; pTags.add(pTag, c); } JScrollPane sp = new JScrollPane(pTags); sp.setBorder(BorderFactory.createEmptyBorder()); add(sp); } private JPanel getWrapPanel() { JPanel p = new JPanel(new WrapLayout(FlowLayout.LEFT, 5, 0)); for (int i = 0; i < NB_TAGVALUES; i++) { p.add(new JLabel("t" + i)); } p.setSize(400, 1); return p; } public static void main(String[] args) { JFrame f = new JFrame(); f.getContentPane().add(new GridBagLayoutTagPanel()); f.setSize(new Dimension(500, 300)); f.setVisible(true); } } 
+3
source

I would like to increase the height of each row containing FlowLayout to the current height of the component. Unfortunately, the preferred size always matches the height for one row.

Wrap Layout handles this.

+11
source

Have you tried MigLayout ? This is the only layout manager that I use, and I never found a layout that I could not get to do.

+2
source

All Articles