Question

I have container jpanel in which i used a boxlayout manager and what i do is i add another panels inside in which the added panel contains a label and textfield using flowlayout manager. everytime i add a panel inside it creates an annoying big space after another added panel. I want to reduce the spacing of the panels i have tried using setsize and setpreferredsize method to adjust it. Here is my code:

  JPanel global = new JPanel();
  global.setLayout(new BoxLayout(global, BoxLayout.Y_AXIS));
  global.setPreferredSize(new Dimension(500,312));
  global.setSize(500,312);
  global.setBounds(8, 5, 500, 312);
  global.setBorder(BorderFactory.createLineBorder(Color.black));
  global.setBackground(Color.white);
  //Elements of global

  JLabel label1 = new JLabel("Global Settings");
  label1.setAlignmentX(Component.CENTER_ALIGNMENT);
  label1.setFont(new Font("tahoma", Font.BOLD, 17));
  global.add(label1);
  global.add(new JSeparator());


  //Name Field
  JPanel c = new JPanel();
  c.setSize(100, 1);
  c.setPreferredSize(new Dimension(100,1));
  c.setLayout(new FlowLayout());
  JLabel label = new JLabel("Display Name");
  JTextField text = new JTextField(20);
  text.setPreferredSize(new Dimension(20,25));
  c.add(label);
  c.add(text);
  global.add(c);

  //Hostname Field
  JPanel c1 = new JPanel();
  c1.setSize(100, 1);
  c1.setPreferredSize(new Dimension(100,1));
  c1.setLayout(new FlowLayout());
  JLabel label2 = new JLabel("Host Name");
  JTextField text1 = new JTextField(20);
  text1.setPreferredSize(new Dimension(20,25));
  c1.add(label2);
  c1.add(text1);
  global.add(c1);
Was it helpful?

Solution

BoxLayout is a pretty aggressive LayoutManager and doesn't always honour the preferred size of components within it. Instead, we must set the maximum size of BoxLayout components to prevent them from being stretched. Additionally, we need to add a Box via Box.createVerticalGlue() - this is special component that gets stretched (rather than the other components).

Here is the rewritten code:

    JPanel global = new JPanel();
    global.setLayout(new BoxLayout(global, BoxLayout.Y_AXIS));
    global.setPreferredSize(new Dimension(500, 312));
    global.setSize(500, 312);
    global.setBounds(8, 5, 500, 312);
    global.setBorder(BorderFactory.createLineBorder(Color.black));
    global.setBackground(Color.white);
    // Elements of global

    JLabel label1 = new JLabel("Global Settings");
    label1.setAlignmentX(Component.CENTER_ALIGNMENT);
    label1.setFont(new Font("tahoma", Font.BOLD, 17));
    global.add(label1);
    JSeparator sep = new JSeparator();
    sep.setMaximumSize(new Dimension((int) sep.getMaximumSize().getWidth(), 50));
    global.add(sep);

    // Name Field
    JPanel c = new JPanel();
    c.setMaximumSize(new Dimension((int) c.getMaximumSize().getWidth(), 50));
    JLabel label = new JLabel("Display Name");
    JTextField text = new JTextField(20);
    text.setPreferredSize(new Dimension(20, 25));
    c.add(label);
    c.add(text);
    global.add(c);

    // Hostname Field
    JPanel c1 = new JPanel();
    c1.setMaximumSize(new Dimension((int) c1.getMaximumSize().getWidth(), 50));
    JLabel label2 = new JLabel("Host Name");
    JTextField text1 = new JTextField(20);
    text1.setPreferredSize(new Dimension(20, 25));
    c1.add(label2);
    c1.add(text1);
    global.add(c1);
    global.add(Box.createVerticalGlue());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top