Sample code |
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import org.jvnet.substance.SubstanceLookAndFeel;
import org.jvnet.substance.painter.AlphaControlBackgroundComposite;
/**
* Test application that shows the use of the
* {@link SubstanceLookAndFeel#OVERLAY_PROPERTY} client property.
*
* @author Kirill Grouchnikov
* @see SubstanceLookAndFeel#OVERLAY_PROPERTY
*/
public class OverlayProperty extends JFrame {
/**
* Creates the main frame for <code>this</code> sample.
*/
public OverlayProperty() {
super("Overlay");
this.setLayout(new BorderLayout());
// Create panel with custom painting logic - simple
// diagonal fill.
JPanel samplePanel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
Graphics2D graphics = (Graphics2D) g.create();
graphics
.setPaint(new GradientPaint(0, 0, new Color(100, 100,
255), getWidth(), getHeight(), new Color(255,
100, 100)));
graphics.fillRect(0, 0, getWidth(), getHeight());
graphics.dispose();
}
};
samplePanel.setPreferredSize(new Dimension(800, 400));
samplePanel.setSize(this.getPreferredSize());
samplePanel.setMinimumSize(this.getPreferredSize());
final JScrollPane scrollPane = new JScrollPane(samplePanel);
// set background composite on the scroll pane, so that when the overlay
// property is set, the viewport contents will show through the
// scrollbars of the scroll pane
scrollPane.putClientProperty(SubstanceLookAndFeel.BACKGROUND_COMPOSITE,
new AlphaControlBackgroundComposite(0.3f, 0.5f));
this.add(scrollPane, BorderLayout.CENTER);
JPanel controls = new JPanel(new FlowLayout(FlowLayout.RIGHT));
final JCheckBox hasOverlay = new JCheckBox("scroll has overlay");
hasOverlay.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// based on the checkbox selection, set the overlay
// property on the scroll pane
scrollPane.putClientProperty(
SubstanceLookAndFeel.OVERLAY_PROPERTY,
hasOverlay.isSelected() ? Boolean.TRUE : null);
repaint();
}
});
}
});
controls.add(hasOverlay);
this.add(controls, BorderLayout.SOUTH);
this.setSize(400, 200);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
/**
* The main method for <code>this</code> sample. The arguments are
* ignored.
*
* @param args
* Ignored.
* @throws Exception
* If some exception occured. Note that there is no special
* treatment of exception conditions in <code>this</code>
* sample code.
*/
public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel(new SubstanceLookAndFeel());
JFrame.setDefaultLookAndFeelDecorated(true);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new OverlayProperty().setVisible(true);
}
});
}
}
Note that the screenshots below show the application running under
custom background composite that
allows specifying custom opacity on the scrollbars.
The screenshot below shows scroll pane in default state (this
property is not specified):
The screenshot below shows scroll pane with this property
set to Boolean.TRUE :
|