| SwingX is a new Java.net project that has been making some noise for a.) what it will be providing in terms of features, and b.) it's lofty goals in terms of implementation timelines. For more information, see the
SwingX project home page
. SwingX is a branch off of the
JDNC project
.
Now, I realize, if you looked at the website above in a timely basis from when I wrote this tip, you're probably surprised that I am writing a tutorial about a project that - as of yet - doesn't have any releases. Hey, what can I say, I'm a sucker for bleeding edge - and some of this stuff is really cool.
Ok, so I certainly can't cover everything from SwingX because a.) a lot of it isn't done and b.) there is a lot of stuff! Today, however, I want to cover just one component - JXPanel - that, for the most part, is complete. Besides, if they hold true to their schedule, their first release will be in late June which, scary as it is, is just around the corner.
To start working with SwingX, just visit the
CVS Access Options page
(requires login), and download the source. Then, bundle it into your classpath as you see fit. Enough with the configuration unpleasantries, let's get on to the JXPanel.
JXPanel is an extension of the
javax.swing.JPanel
that only does one thing - it adds translucent rendering. Long story short, that means that a JXPanel (and any components
in
a JXPanel) will render based on an 'alpha-channel' setting, which is essentially a percentage of
how
opaque things should be.
JXPanels are just like JPanels, but have the one additional property - the 'alpha' property - so really, the usage of a JXPanel is very unextrordinary. That being said, you do have to be somewhat creative to come up with a valid use case for utilizing these panels. There are many classes that extend the JXPanel that add further functionality, but most of those aren't quite done yet - so we'll get to those when they are available.
Here is what JXPanel configuration code would look like:
// JPanel code.
JPanel panel = new JPanel();
panel.add(new JButton("Button 1"));
panel.add(new JButton("Button 2"));
// JXPanel code.
JXPanel xpanel = new JXPanel();
xpanel.add(new JButton("Button 1"));
xpanel.add(new JButton("Button 2"));
xpanel.setAlpha(.5f); // 50% opaque. .35 would be 35%, .75 would be 75%, etc.
The most common use of the JXPanel will most likely be with a JLayeredPane. Because of that, I have implemented a simple class using the JLayeredPane container, that renders buttons on multiple layers. Here is a screen-shot using Java 5's Ocean UI (
note that I am pressing down the bottom button to show that components do still work
):
... and here is the same screenshot using the
JGoodies
Plastic Look and Feel (
note in this case I am mousing over the 2nd-from-the-bottom button, again, to show things still work
).
Here is my test class in total:
package com.javalobby.tnt.swingx;
import java.awt.*;
import javax.swing.*;
import org.jdesktop.swingx.JXPanel;
import com.jgoodies.looks.plastic.PlasticXPLookAndFeel;
public class JXPanelTest extends JPanel {
private static final int NUM_LAYERS = 5;
private JLayeredPane layeredPane;
public JXPanelTest() {
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
// Create and set up the layered pane.
layeredPane = new JLayeredPane();
layeredPane.setPreferredSize(new Dimension(300, 310));
// This is the origin of the first label added.
Point origin = new Point(10, 20);
// This is the offset for computing the origin for the next label.
int offset = 35;
// Add several overlapping panels to the layered pane
// using absolute positioning/sizing.
for (int i = 0; i < NUM_LAYERS; i++) {
JComponent component = createColoredBox(i,
origin);
layeredPane.add(component, new Integer(i));
origin.x += offset;
origin.y += offset;
}
add(layeredPane);
}
private JComponent createColoredBox(int layer, Point origin) {
JXPanel panel = new JXPanel();
panel.setLayout(new GridLayout());
panel.setAlpha(.5f);
panel.add(new JButton("Button on Layer: " + layer));
panel.setBounds(origin.x, origin.y, 140, 140);
return panel;
}
/**
* Create the GUI and show it. For thread safety, this method should be
* invoked from the event-dispatching thread.
*/
private static void createAndShowGUI() {
try {
UIManager.setLookAndFeel(PlasticXPLookAndFeel.class.getName());
}
catch(Exception e) { }
// Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);
// Create and set up the window.
JFrame frame = new JFrame("JXPanelTest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create and set up the content pane.
JComponent newContentPane = new JXPanelTest();
newContentPane.setOpaque(true); // content panes must be opaque
frame.setContentPane(newContentPane);
// Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
// Schedule a job for the event-dispatching thread:
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Until next time,
R.J. Lorimer |