Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fixed widths into a GridBagLayout

enter image description here

I'm using GridBagLayout to make a StatusBar which looks like in the picture. I have 4 areas, so I have a button in the first one, then info messages in the second one, and then I want two more (and i also have a fifth one to make the corner).

The button area fits perfectly, because the content is always a button with the same width. Same with the corner area. The info area must get all the space available. 3rd and 4th areas must have a fixed value, independent from the screen size.

How can I do that?

My current code is:

public MyStatusBar() {
        setLayout(new GridBagLayout());
        setPreferredSize(new Dimension(getWidth(), 23));
        GridBagConstraints c = new GridBagConstraints();

        botonDispositivo = new JButton("");
        this.setText(0, "Disconnected");
        URL imageUrl = getClass().getResource("resources/22x22/dispositivo01.png");
        this.setButtonImg(imageUrl);
        this.setButtonEnabled(false);


        c.gridx = 0;
        c.gridy = 0;
        c.fill = GridBagConstraints.BOTH;
        c.anchor = GridBagConstraints.WEST;
        c.gridwidth = 1;
        c.gridheight = 1;
        this.add(botonDispositivo, c);

        c.insets= new Insets(0,10,0,0);
        c.gridx ++;
        c.gridy = 0;
        c.weightx = 0.7;
        c.fill = GridBagConstraints.HORIZONTAL;
        msgLabel = new JLabel("");
        msgLabel.setPreferredSize(new Dimension(500, msgLabel.getHeight()));
        this.add(msgLabel, c);
        this.setText(1, "Waiting for potentiostat conection");

        c.gridx ++;
        c.gridy = 0;
        c.weightx = 0.0;
        c.fill = GridBagConstraints.NONE;
        this.add(new SeparatorPanel(Color.GRAY, Color.WHITE), c);

        c.gridx ++;
        c.gridy = 0;
        c.fill = GridBagConstraints.NONE;
        c.weightx = 0.0;
        overErrorLabel = new JLabel("");
        overErrorLabel.setSize(new Dimension(150, overErrorLabel.getHeight()));
        this.add(overErrorLabel, c);
        //this.setText(2, "");

        c.gridx ++;
        c.gridy = 0;
        c.weightx = 0.0;
        c.fill = GridBagConstraints.NONE;
        this.add(new SeparatorPanel(Color.GRAY, Color.WHITE), c);

        c.gridx ++;
        c.gridy = 0;
        c.weightx = 0.0;
        c.fill = GridBagConstraints.NONE;
        c.anchor = GridBagConstraints.EAST;
        timeLabel = new JLabel("");
        timeLabel.setMinimumSize(new Dimension(150, timeLabel.getHeight()));
        //timeLabel.setMaximumSize(new Dimension(150, timeLabel.getHeight()));
        this.add(timeLabel, c);
        //this.setText(3, "");


        JPanel rightPanel = new JPanel(new GridBagLayout());
        c.gridx ++;
        c.gridy = 0;
        c.weightx = 0.0;
        c.fill = GridBagConstraints.BOTH;
        rightPanel.add(new JLabel(new AngledLinesWindowsCornerIcon()), c);
        rightPanel.setOpaque(false);

        c.gridx ++;
        c.gridy = 0;
        c.fill = GridBagConstraints.BOTH;
        this.add(rightPanel, c);

        setBackground(SystemColor.control);
      }
like image 513
Roman Rdgz Avatar asked Jul 27 '11 10:07

Roman Rdgz


People also ask

How are the elements of a GridBagLayout organized?

The GridBagLayout manages each component's minimum and preferred sizes in order to determine the component's size. GridBagLayout components are also arranged in the rectangular grid but can have many different sizes and can occupy multiple rows or columns.

What is the difference between GridLayout and GridBagLayout?

A GridLayout puts all the components in a rectangular grid and is divided into equal-sized rectangles and each component is placed inside a rectangle whereas GridBagLayout is a flexible layout manager that aligns the components vertically and horizontally without requiring that the components be of the same size.

What is insets in GridBagLayout?

Insets insets. Controls padding between the component and neighboring components. To make a set of constraints for a component or components, create a new instance of GridBagConstraints and set these public variables to the appropriate values. (There is also a large constructor that takes all 11 arguments.)

What is Weightx GridBagLayout?

weightx and weighty are used to determine how to distribute space among columns and among rows. This values are important for specifying resizing behavior. If you do not specify any of weightx or weighty, all the components will clump together in the center of their container.


2 Answers

You set preferred, minimum, maximul etc. size for different labels. I think all you need is set weight X>0 and fill param=HORIZONTAL of the constraint for the label you want to be resized and weightX=0 and fill-NONE for the labels you don't want to be resized.

Also use ipadx to specify min size for labels.

like image 88
StanislavL Avatar answered Sep 18 '22 17:09

StanislavL


for better and easiest output to the GUI is better look for Borderlayout,

JPanel#setPrefferedSize(200, 30); for JButton with Icon and JLabel put to the WEST area

JPanel#setPrefferedSize(200, 30); (rightPanel) to the EAST area

put another JPanel to CENTER area

like image 25
mKorbel Avatar answered Sep 21 '22 17:09

mKorbel