How to maximise the effect of Box glues? / How to

2019-03-03 11:14发布

Setup:

  • On the right side of the screen, there is a list from top to bottom.
  • On the left side of the screen, there are manipulators for this list;
    • one manipulator consists of two elements, one beside the other.
    • at first only one manipulator exists, but further ones may appear;
      • those would then be placed below each other.

Here, the manipulators' elements are JComboBoxes: sourceAMask and sourceBMask.

To ensure that they appear side by side, I enclosed them in the Box mask_initialPair laid out along the BoxLayout.X_AXIS. They should appear left-aligned, so I padded the right with a Box.createHorizontalGlue.

To allow mask_initialPair to be vertically unstretched, I enclosed that in the Box maskPairs laid out along the BoxLayout.Y_AXIS and added a Box.createVerticalGlue below it.

To put this vertical list beside the other one, targetScroller, I enclosed both in the Box maskPage laid out along the BoxLayout.X_AXIS. This construct should appear centered, so I padded both left and right with Box.createHorizontalGlue.

My (ideal) assumptions:

  1. sourceAMask and sourceBMask are left-aligned and of minimal width necessary (as per greatest length from source[A|B]Package), as the horizontal glue to the right takes up all further space assigned to maskPairs.
  2. mask_initialPair (and all further pairs) has the minimal height necessary (as per current size of font used), as the vertical glue below takes up all further space.
  3. The left and right margins of maskPage are of equal width.
  4. maskPairs and targetScroller are of equal width.

Only 3 holds.

4 is violated as maskPairs is far wider than targetScroller, because (violating 1), the horizontal glue within mask_initialPair receives equal width to the glues in maskPage, as if inserted between maskPairs and targetScroller, instead of as part of maskPairs.

2 is violated as mask_initialPair has equal height to the glue below it.

Example

Question:

  • I understand how the horizontal misalignment happens.
  • I do not understand why the stretching (in both dimensions) of visible components happens, despite there always being glue to take up the excess space.

In both cases: How can I fix it?
Ideally without overriding all the classes and inflating my code by its size again.

import javax.swing.*;

class Test
{
  public static void main(String[] args)
  {
    String[] sourceAPackage = { "A::entry1", "A::entry2", "A::entry3" };
    String[] sourceBPackage = { "B::entry1", "B::entry2" };
    String[] targetPackage = {};

    JList<String> targetList = new JList<String>(targetPackage);
    JScrollPane targetScroller = new JScrollPane(targetList);

    JComboBox sourceAMask = new JComboBox<String>(sourceAPackage);
    JComboBox sourceBMask = new JComboBox<String>(sourceBPackage);
    sourceAMask.setEditable(true);
    sourceBMask.setEditable(true);

    Box mask_initialPair = new Box(BoxLayout.X_AXIS);
    mask_initialPair.add(sourceAMask);
    mask_initialPair.add(sourceBMask);
    mask_initialPair.add(Box.createHorizontalGlue());

    Box maskPairs = new Box(BoxLayout.Y_AXIS);
    maskPairs.add(mask_initialPair);
    maskPairs.add(Box.createVerticalGlue());

    Box maskPage = new Box(BoxLayout.X_AXIS);
    maskPage.add(Box.createHorizontalGlue());
    maskPage.add(maskPairs);
    maskPage.add(targetScroller);
    maskPage.add(Box.createHorizontalGlue());

    JFrame frame = new JFrame();
    frame.add(maskPage);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }
}

0条回答
登录 后发表回答