Skip to main content
deleted 4 characters in body
Source Link
toolic
  • 16.4k
  • 6
  • 29
  • 221

I appreciate the GridBagLayout of Java very much. But, it has one huge disadvantage: very veryit is very much boilerplate code. You have to type too much!.

I would like to use the GridBagLayout of Java in the most comfortable way. I want to type as lesslittle as possible, but also have good code readability.

Still, I feel there might be some room for improvement, shorter API or better code readability. Do you have suggestions how to improve the API, above?

I appreciate the GridBagLayout of Java very much. But, it has one huge disadvantage: very very very much boilerplate code. You have to type too much!

I would like to use the GridBagLayout of Java in the most comfortable way. I want to type as less as possible, but also have good code readability.

Still, I feel there might be some room for improvement, shorter API or better code readability. Do you have suggestions how to improve the API, above?

I appreciate the GridBagLayout of Java very much. But, it has one huge disadvantage: it is very much boilerplate code. You have to type too much.

I would like to use the GridBagLayout of Java in the most comfortable way. I want to type as little as possible but also have good code readability.

Still, I feel there might be some room for improvement, shorter API or better code readability. Do you have suggestions how to improve the API above?

Source Link

Wrap java.awt.GridBagLayout

I appreciate the GridBagLayout of Java very much. But, it has one huge disadvantage: very very very much boilerplate code. You have to type too much!

I would like to use the GridBagLayout of Java in the most comfortable way. I want to type as less as possible, but also have good code readability.

My current state is like this:

    // New Layout with IGrid

    // Here are definitions of components matchView, commentsSp, commandsSp, autorollCp, and matchMenu.

    var grid = IGrid.create();
    
    grid.rect(0, 0, 2, 1);
    grid.weight().x(10);
    grid.weight().y(3);
    grid.fill().both();
    grid.add(matchView);
    
    grid.rect(2, 0, 1, 1);
    grid.weight().x(1);
    grid.weight().y(1);
    grid.add(commentsSp);

    grid.rect(3, 0, 1, 1);
    grid.weight().x(0);
    grid.weight().y(0);
    grid.add(commandsSp);

    grid.rect(0, 1, 1, 1);
    grid.fill().none();
    grid.insets().left(15);
    grid.add(autorollCb);

    grid.rect(1, 1, 3, 1);
    grid.weight().x(1);
    grid.insets().top(15);
    grid.insets().left(15);
    grid.insets().right(15);
    grid.insets().bottom(15);
    grid.add(matchMenu);

    setContentPane(grid.asComponent());

The API therefore is as follows:

import javax.swing.JComponent;

public interface IGrid {
    public static IGrid create() {
        return new Grid();
    }

    JComponent asComponent();
    void add(JComponent comp);
    void x(int v);
    void y(int v);
    void w(int v);
    void h(int v);
    void rect(int x, int y, int w, int h);
    public IWeight weight();
    public IFill fill();
    public IInsets insets();

    interface IWeight {
        void x(int v);
        void y(int v);
    }

    interface IFill {
        void none();
        void horizontal();
        void vertical();
        void both();
    }

    interface IInsets {
        void top(int v);
        void left(int v);
        void right(int v);
        void bottom(int v);
    }
}

Still, I feel there might be some room for improvement, shorter API or better code readability. Do you have suggestions how to improve the API, above?