/* (@)JSliderStyledAsProgressBar.java */
/* Copyright 2009 Sebastian Haufe
* Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
[url]http://www.apache.org/licenses/LICENSE-2.0[/url]
* Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
package com.ebenius;
import java.awt.BorderLayout;
import java.awt.Graphics;
import javax.swing.*;
/**
* Strange hack to paint a progress bar above a {@link JSlider}.
*
* @version $Revision$ as of $Date$
* @author Sebastian Haufe
* @since Playground-3.8
*/
public class JSliderStyledAsProgressBar extends JSlider {
private JProgressBar progressBar;
// -------------------------------------------------------------------------
// Constructors
// -------------------------------------------------------------------------
/** Creates a new <code>JSliderStyledAsProgressBar</code>. */
public JSliderStyledAsProgressBar() {
installProgressBar();
}
/**
* Creates a new <code>JSliderStyledAsProgressBar</code>.
*
* @param brm the model
* @see JSlider#JSlider(BoundedRangeModel)
*/
public JSliderStyledAsProgressBar(BoundedRangeModel brm) {
super(brm);
installProgressBar();
}
/**
* Creates a new <code>JSliderStyledAsProgressBar</code>.
*
* @param orientation orientation
* @param min minimum value
* @param max maximum value
* @param value current value
* @see JSlider#JSlider(int, int, int, int)
*/
public JSliderStyledAsProgressBar(
int orientation,
int min,
int max,
int value) {
super(orientation, min, max, value);
installProgressBar();
}
/**
* Creates a new <code>JSliderStyledAsProgressBar</code>.
*
* @param min minimum value
* @param max maximum value
* @param value current value
* @see JSlider#JSlider(int, int, int, int)
*/
public JSliderStyledAsProgressBar(int min, int max, int value) {
super(min, max, value);
installProgressBar();
}
/**
* Creates a new <code>JSliderStyledAsProgressBar</code>.
*
* @param min minimum value
* @param max maximum value
* @see JSlider#JSlider(int, int, int, int)
*/
public JSliderStyledAsProgressBar(int min, int max) {
super(min, max);
installProgressBar();
}
/**
* Creates a new <code>JSliderStyledAsProgressBar</code>.
*
* @param orientation orientation
*/
public JSliderStyledAsProgressBar(int orientation) {
super(orientation);
installProgressBar();
}
// -------------------------------------------------------------------------
// Progress bar look
// -------------------------------------------------------------------------
private void installProgressBar() {
progressBar = new JProgressBar(getModel());
progressBar.setOrientation(getOrientation());
add(progressBar);
}
@Override
public void invalidate() {
super.invalidate();
progressBar.invalidate();
}
@Override
protected void validateTree() {
super.validateTree();
progressBar.setBounds(getBounds());
progressBar.validate();
}
@Override
public void setModel(BoundedRangeModel newModel) {
super.setModel(newModel);
if (progressBar != null) { // might be null during initialization
progressBar.setModel(newModel);
}
}
@Override
public void setOrientation(int orientation) {
super.setOrientation(orientation);
if (progressBar != null) { // might be null during initialization
progressBar.setOrientation(orientation);
}
}
@Override
protected void paintComponent(Graphics g) {
progressBar.paint(g);
}
// -------------------------------------------------------------------------
// Program Entry Point
// -------------------------------------------------------------------------
/**
* Test main method.
*
* @param args ignored
*/
public static void main(String[] args) {
final JPanel contentPane = new JPanel(new BorderLayout(6, 6));
contentPane.add(new JSliderStyledAsProgressBar(0, 100, 36));
final JFrame f = new JFrame("Test Frame: JSliderStyledAsProgressBar"); //$NON-NLS-1$
f.setContentPane(contentPane);
f.pack();
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.setVisible(true);
}
}