import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.*;
import java.util.Vector;
import javax.swing.*;
import org.jvnet.substance.SubstanceLookAndFeel;
import org.jvnet.substance.painter.GradientPainterInfo;
/**
* Test application that shows the use of the
* {@link SubstanceLookAndFeel#getCurrentGradientPainter()} API.
*
* @author Kirill Grouchnikov
* @see SubstanceLookAndFeel#getCurrentGradientPainter()
*/
public class GetCurrentGradientPainter extends JFrame {
/**
* Creates the main frame for <code>this</code> sample.
*/
public GetCurrentGradientPainter() {
super("Get current gradient painter");
this.setLayout(new BorderLayout());
JPanel panel = new JPanel(new FlowLayout());
// Get all gradient painter display names and set the vector as a model
// for combobox.
final JComboBox cb = new JComboBox(new Vector<String>(
SubstanceLookAndFeel.getAllGradientPainters().keySet()));
cb
.setSelectedItem(SubstanceLookAndFeel
.getCurrentGradientPainterName());
cb.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent evt) {
// Get the affected item
final Object item = evt.getItem();
if (evt.getStateChange() == ItemEvent.SELECTED) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
// Get the gradient painter info object based on
// the selected gradient painter display name
GradientPainterInfo painterInfo = SubstanceLookAndFeel
.getAllGradientPainters().get(
(String) item);
// Set the global gradient painter based on the
// gradient painter class name.
SubstanceLookAndFeel
.setCurrentGradientPainter(painterInfo
.getClassName());
SwingUtilities
.updateComponentTreeUI(GetCurrentGradientPainter.this);
} catch (Exception exc) {
}
};
});
}
}
});
panel.add(new JLabel("All gradient painters:"));
panel.add(cb);
this.add(panel, BorderLayout.CENTER);
JPanel controls = new JPanel(new FlowLayout(FlowLayout.RIGHT));
JButton getGradientPainter = new JButton("Get current gradient painter");
getGradientPainter.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(GetCurrentGradientPainter.this,
"Current gradient painter is of class "
+ SubstanceLookAndFeel
.getCurrentGradientPainter().getClass()
.getName());
}
});
controls.add(getGradientPainter);
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);
JDialog.setDefaultLookAndFeelDecorated(true);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new GetCurrentGradientPainter().setVisible(true);
}
});
}
}
The screenshot below shows the result of calling this API
on the default (standard) gradient painter. Note the class name and the
default fill:
The screenshot below shows the result of calling this API
after the specular gradient painter has been selected in the combobox.
Note the class name and the specular gradient:
|