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.
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?
GridPane
. I love using swing too, but JavaFX is objectively better: API, events & bnding, CSS styles, and so on. Disadvantage: the somewhat obfuscating lambda usage. \$\endgroup\$