Providing JPanel with percentage width

What's the simplest way to do JPanel

that that takes up a fixed percentage of its parent container, in width?

Its width should update when its parent container changes.

I tried to use Box.createHorizontalStrut()

but this doesn't update when the parent container is resized JPanel

.

+3


source to share


3 answers


You want a GridBagLayout . ( How to use? )

Using a GridbagLayout allows you to define GridBagConstraints for every single component you add.

These limitations include weightx

which does exactly what it says about the gesture. It is a relative value calculated for all components in this "grid-row", similar to the parts of the distribution.



Keep in mind that the weighting factor only starts to play when the preferred component size is exceeded. Be aware that this can lead to interesting errors when setting minimumSize (), see also this SO answer.

Anyway: you can get the desired value for weightx by specifying it as a percentage of the components in the current line or with any of them.

This means the component needs 50% of the width of the parent container available , use weightx = 0.5

. make sure the weightx values ​​of the rows are relevant1.0

+5


source


Another option is to use SpringLayout

:



import java.awt.*;
import javax.swing.*;

public class SpringLayoutTest {
  private JComponent makeUI() {
    SpringLayout layout = new SpringLayout();

    JPanel p = new JPanel(layout);
    p.setBorder(BorderFactory.createLineBorder(Color.GREEN, 10));

    Spring pw = layout.getConstraint(SpringLayout.WIDTH,  p);
    Spring ph = layout.getConstraint(SpringLayout.HEIGHT, p);

    JLabel l = new JLabel("label: 5%, 5%, 90%, 55%", SwingConstants.CENTER);
    l.setOpaque(true);
    l.setBackground(Color.ORANGE);
    l.setBorder(BorderFactory.createLineBorder(Color.RED, 1));

    JButton b = new JButton("button: 50%, 65%, 40%, 30%");

    setPercentage(layout.getConstraints(l), pw, ph, .05f, .05f, .90f, .55f);
    setPercentage(layout.getConstraints(b), pw, ph, .50f, .65f, .40f, .30f);

    p.add(l);
    p.add(b);
    return p;
  }

  private static void setPercentage(
      SpringLayout.Constraints c, Spring pw, Spring ph,
      float sx, float sy, float sw, float sh) {
    c.setX(Spring.scale(pw, sx));
    c.setY(Spring.scale(ph, sy));
    c.setWidth(Spring.scale(pw,  sw));
    c.setHeight(Spring.scale(ph, sh));
  }

  public static void main(String... args) {
    EventQueue.invokeLater(new Runnable() {
      @Override public void run() {
        createAndShowGUI();
      }
    });
  }
  public static void createAndShowGUI() {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.getContentPane().add(new SpringLayoutTest().makeUI());
    frame.setSize(320, 240);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}

      

+2


source


You can use Relative Layout . This layout was designed for this purpose, as none of the layouts from the JDK support this directly (and easily).

As a simple example, you could have 3 panels with 25%, 25% and 50% width:

RelativeLayout rl = new RelativeLayout(RelativeLayout.X_AXIS);
rl.setFill( true );
JPanel panel = new JPanel( rl );
panel.add(red, new Float(25);
panel.add(green, new Float(25));
panel.add(blue, new Float(50));

      

+2


source







All Articles