Tip 1: Use EmptyBorder to make gap

iiley July 11th, 2008

Assume we have a dialog looks like below:
gap1
It is created by code(snippted):

AsWingManager.initAsStandard(this);
var okButton:JButton = new JButton("Conform");
var cancelButton:JButton = new JButton("Cancel");
var tipButton:JButton = new JButton("Tip");
var pane:JPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 4, 4));
pane.appendAll(tipButton, okButton, cancelButton);

var window:JFrame = new JFrame(null, "Gap");
window.getContentPane().append(pane, BorderLayout.SOUTH);
window.setComBoundsXYWH(10, 10, 200, 200);
window.show();

The three button is layouted by a FlowLayout, they have same 4 pixel gap between each other. If i want to increase only the gap between Tip and Conform buttons, it’s not easy to do it with little change of above code except use a EmptyBorder.
If i add this setence below tipButton creating:
tipButton.setBorder(new EmptyBorder(null, new Insets(0, 0, 0, 20)));
The final dialog will be looks like this:
gap2
You see, it’s easy.

EmptyBorder performance a border with blank graphics. So it is usually used to create gaps. And depond on AsWing Border system, border can be nested, so you even can make a gap between two border of one component, for exampel.
yourCompnent.setBorder(new LineBorder(new EmptyBorder(new BevelBorder(), new Insets(…))));

Trackback URI | Comments RSS

Leave a Reply

Bad Behavior has blocked 1194 access attempts in the last 7 days.