Creating transparent and shaped windows using Java Swing/AWT
The Java AWT offer various possibilities about window manipulation. Today I'll talk about two possibilities using class AWTUtilities.
Building a transparent window
To make a Window (JFrame, JDialog ...) with alpha effect, you should invoke the method AWTUtilities.setWindowOpacity. This method parameters are: the window to apply transparency and transparency degree, that can be between 0 (zero) and 1, being 0 invisible and 1 totally visible.
JFrame window = new JFrame("My Window");
//70% of transparency
AWTUtilities.setWindowOpacity(window, .7f);
window.setSize(800,600);
window.setVisible(true);
Changing window shape
To change the window shape you should use the method AWTUtilities.setWindowShape. This method parameters are: window that will be changed and the new shape (java.awt.Shape) of the window.
The most efficiently form to use is to implement the method componentResized(), cause you may recompute the window and components size
Here I'll use a triangle with 70% of transparency.
final JFrame window = new JFrame("My Window");
try {
//Add the ComponentListener to implement componentResized method
window.addComponentListener(new ComponentAdapter(){
@Override
//building componentResized
public void componentResized(ComponentEvent e) {
int[] x = {0,400,800}; //Pontos X do polígono
int[] y = {600,0,600}; //Pontos Y do polígono
//Triangle with 800 w x 600 h
Shape shape = new Polygon(x, y, 3);
AWTUtilities.setWindowShape(window, shape);
//70% of transparency
AWTUtilities.setWindowOpacity(window, 0.7f);
}
});
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
window.setUndecorated(true); //removing title bar
window.setSize(800,600);
window.setVisible(true);
Notice that the method setUndecorated( ) was invoked with true as value. This method are responsible by hide the title bar (one with icon and the maximize, minimize and close buttons). This is really necessary, because with title bar visible the window are not be shaped.
I hope you enjoy...
Setting maximum number of characters in JTextField
The default implementation of JTextField not allow set maximum number of characters. To enable this resource you need implements a Document, overriding insertString method.
public class MaxLengthTextDocument extends PlainDocument {
//Store maximum characters permitted
private int maxChars;
@Override
public void insertString(int offs, String str, AttributeSet a)
throws BadLocationException {
if(str != null && (getLength() + str.length() > maxChars)){
str = str.substring(0, maxChars);
}
super.insertString(offs, str, a);
}
//getter e setter omitted
}
Here we defined one class called MaxLengthTextDocument that extends PlainDocument. In insertString attribute, we checked if quantity of characters greater than maxChars attribute, cutting String if true.
After this, only insert our implementation in JTextField, this way:
... MaxLengthTextDocument maxLength = new MaxLengthTextDocument(); maxLength.setMaxChars(50);//50 is a maximum number of character jTextField.setDocument(maxLength); ...
And voilà!
See ya!
Introducing padding in a JLabel
To introduce padding in a JLabel can we use an EmptyBorder, where the attribute width will be our padding. Like this:
...
JLabel jLabel = new JLabel("My JLabel");
//Border used as padding
Border paddingBorder = BorderFactory.createEmptyBorder(10,10,10,10);
jLabel.setBorder(BorderFactory.createCompoundBorder(border,paddingBorder));
...
Here, the JLabel contains a padding with 10 pixels in top, right, bottom and left respectively.

If you want to put border around the JLabel, can use a CompoundBorder, inserting Border and EmptyBorder (padding). Like this:
...
JLabel jLabel = new JLabel("Meu JLabel");
//Border used as padding
Border paddingBorder = BorderFactory.createEmptyBorder(10,10,10,10);
//JLabel will be involved for this border
Border border = BorderFactory.createLineBorder(Color.BLUE);
jLabel.setBorder(BorderFactory.createCompoundBorder(border,paddingBorder));
...

Download the source code of this sample here.

