more-useful things you
could do with it. Let’s look at
a few.
When trying to debug
applications it’s common
to want to know the bounds
of every component in the
frame, for example, when
one of your components
seems to be invisible.
While you could print this
information to the console,
with JLayer you can simply
draw debugging information directly on top of the
components.
The example shown in
Listing 5 and Figure 3 draws the
borders of every component in orange.
It also writes the name of the component’s class. This is handy if you want
to tell the difference between many button subclasses.
The JLayer is created
only once and set on
the topmost panel.
Because the LayerUI
is recursive, it can
draw on top of child
components as deep
in the hierarchy as
necessary.
public static void main(String ... args) {
LayerUI<JPanel> overlay = new LayerUI<JPanel>() {
@Override
public void paint(Graphics g, JComponent clayer) {
super.paint(g, clayer);
drawBorders(g,clayer);
}
private void drawBorders(Graphics g, JComponent clayer) {
g.setColor(Color.ORANGE);
g.drawRect(0, 0, clayer.get Width(), clayer.getHeight());
if( clayer.getComponentCount() <= 0) {
g.drawString( clayer.getClass().getSimpleName(), 5, 15);
}
for(Component comp : clayer.getComponents()) {
if(comp instanceof JComponent) {
g.translate( comp.getX(), comp.getY());
drawBorders(g,(JComponent)comp);
g.translate( -comp.getX(), -comp.getY());
}
}
}
};
JPanel panel = new JPanel();
panel.setLayout( new BorderLayout());
panel.add(new JLabel("Header Label"), BorderLayout.NORTH);
panel.add(new JButton("Quit"), BorderLayout.SOUTH);
panel.add(new JButton("EAST"), BorderLayout.EAST);
panel.add(new JButton("West coast"), BorderLayout. WEST);
JPanel subpanel = new JPanel();
subpanel.setLayout(new BoxLayout(subpanel, BoxLayout.PAGE_AXIS));
subpanel.add(new JTextField("this is your name"));
subpanel.add(new JTextField("000-000-0000"));
subpanel.add(new JTextField("6666x"));
panel.add(subpanel,BorderLayout.CENTER);
JFrame frame = new JFrame("foo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JLayer(panel,overlay));
frame.pack();
frame.setVisible(true);
COMMUNITY
JAVA IN ACTION
ABOUT US
blog
See all listings as text
Figure 3
The code in Listing 5
again creates a LayerUI
instance with a paint
method. This time it recursively goes through the
component and its children
to draw borders. It also
writes out the simple name
of the component’s class
if the component doesn’t
have any children. (It skips
components with children
to avoid overdraw on
nested panels.)
Note that the JLayer is cre-
ated only once and set on
the topmost panel. Because
the LayerUI is recursive, it can draw on
top of child components as deep in the
hierarchy as necessary.
The code in Listing 6 is similar to what
we’ve done before, but it draws any
disabled component using a blur. That
way, the user knows it is
really disabled.
In this case, if the
component is enabled,
super() is called to draw
normally. If the component is not enabled, the
code creates an image
buffer and calls super()
on the graphics context
from the image. This
draws the component
into the image instead
of onto the screen.
Then the code applies
a simple blur operation
to the image and draws