*****************
Custom AWT Slider
* LabeledCostSlider.java. A numeric slider class with attached label.
* CostSlider.java. A slider class that lets you read numeric values. Used in the LabeledCostSlider class.
* Slider.java. A slider class: a combination of Scrollbar and TextField. Used in the CostSlider class.
* ScrollbarPanel.java A Panel with adjustable top and bottom insets, used by the Slider class to change the thickness of the Slider.
*****************
LabeledCostSlider.java. A numeric slider class with attached label.
>>>>>>>>>>>>>>>>>
import java.awt.*;
/** A CostSlider with a label centered above it.
*************
public class LabeledCostSlider extends Panel {
public LabeledCostSlider(String labelString,
Font labelFont,
int minValue, int maxValue,
int initialValue,
Everest app) {
setLayout(new BorderLayout());
Label label = new Label(labelString, Label.CENTER);
if (labelFont != null) {
label.setFont(labelFont);
}
add(label, BorderLayout.NORTH);
CostSlider slider = new CostSlider(minValue,
maxValue,
initialValue,
app);
add(slider, BorderLayout.CENTER);
}
}
<<<<<<<<<<<<<<<<<<<
>>>>>>>>>>>>>>>>>>>
CostSlider.java. A slider class that lets you read numeric values. Used in the LabeledCostSlider class.
>>>>>>>>>>>>>>>>>>>
/** A Slider that takes an Everest applet as an argument,
* calling back to its setCostField when the slider value
* changes.
*
*******************
public class CostSlider extends Slider {
private Everest app;
public CostSlider(int minValue, int maxValue,
int initialValue, Everest app) {
super(minValue, maxValue, initialValue);
this.app = app;
}
public void doAction(int value) {
app.setCostField(value);
}
}
<<<<<<<<<<<<<<<<<<<<<
>>>>>>>>>>>>>>>>>>>>>
Slider.java. A slider class: a combination of Scrollbar and TextField. Used in the CostSlider class.
>>>>>>>>>>>>>>>>>>>>>
import java.awt.*;
import java.awt.event.*;
/** A class that combines a horizontal Scrollbar and a TextField
* (to the right of the Scrollbar). The TextField shows the
* current scrollbar value, plus, if setEditable(true) is set,
* it can be used to change the value as well.
*
********************
public class Slider extends Panel implements ActionListener,
AdjustmentListener {
private Scrollbar scrollbar;
private TextField textfield;
private ScrollbarPanel scrollbarPanel;
private int preferredWidth = 250;
/** Construct a slider with the specified min, max and initial
* values. The "bubble" (thumb) size is set to 1/10th the
* scrollbar range.
*/
public Slider(int minValue, int maxValue, int initialValue) {
this(minValue, maxValue, initialValue,
(maxValue - minValue)/10);
}
/** Construct a slider with the specified min, max,and initial
* values, plus the specified "bubble" (thumb) value. This
* bubbleSize should be specified in the units that min and
* max use, not in pixels. Thus, if min is 20 and max is 320,
* then a bubbleSize of 30 is 10% of the visible range.
*/
public Slider(int minValue, int maxValue, int initialValue,
int bubbleSize) {
setLayout(new BorderLayout());
maxValue = maxValue + bubbleSize;
scrollbar = new Scrollbar(Scrollbar.HORIZONTAL,
initialValue, bubbleSize,
minValue, maxValue);
scrollbar.addAdjustmentListener(this);
scrollbarPanel = new ScrollbarPanel(6);
scrollbarPanel.add(scrollbar, BorderLayout.CENTER);
add(scrollbarPanel, BorderLayout.CENTER);
textfield = new TextField(numDigits(maxValue) + 1);
textfield.addActionListener(this);
setFontSize(12);
textfield.setEditable(false);
setTextFieldValue();
add(textfield, BorderLayout.EAST);
}
/** A place holder to override for action to be taken when
* scrollbar changes.
*/
public void doAction(int value) {
}
/** When textfield changes, sets the scrollbar */
public void actionPerformed(ActionEvent event) {
String value = textfield.getText();
int oldValue = getValue();
try {
setValue(Integer.parseInt(value.trim()));
} catch(NumberFormatException nfe) {
setValue(oldValue);
}
}
/** When scrollbar changes, sets the textfield */
public void adjustmentValueChanged(AdjustmentEvent event) {
setTextFieldValue();
doAction(scrollbar.getValue());
}
/** Returns the Scrollbar part of the Slider. */
public Scrollbar getScrollbar() {
return(scrollbar);
}
/** Returns the TextField part of the Slider */
public TextField getTextField() {
return(textfield);
}
/** Changes the preferredSize to take a minimum width, since
* super-tiny scrollbars are hard to manipulate.
*/
public Dimension getPreferredSize() {
Dimension d = super.getPreferredSize();
d.height = textfield.getPreferredSize().height;
d.width = Math.max(d.width, preferredWidth);
return(d);
}
/** This just calls preferredSize */
public Dimension getMinimumSize() {
return(getPreferredSize());
}
/** To keep scrollbars legible, a minimum width is set. This
* returns the current value (default is 150).
*/
public int getPreferredWidth() {
return(preferredWidth);
}
/** To keep scrollbars legible, a minimum width is set. This
* sets the current value (default is 150).
*/
public void setPreferredWidth(int preferredWidth) {
this.preferredWidth = preferredWidth;
}
/** This returns the current scrollbar value */
public int getValue() {
return(scrollbar.getValue());
}
/** This assigns the scrollbar value. If it is below the
* minimum value or above the maximum, the value is set to
* the min and max value, respectively.
*/
public void setValue(int value) {
scrollbar.setValue(value);
setTextFieldValue();
}
/** Sometimes horizontal scrollbars look odd if they are very
* tall. So empty top/bottom margins can be set. This returns
* the margin setting. The default is four.
*/
public int getMargins() {
return(scrollbarPanel.getMargins());
}
/** Sometimes horizontal scrollbars look odd if they are very
* tall. So empty top/bottom margins can be set. This sets
* the margin setting.
*/
public void setMargins(int margins) {
scrollbarPanel.setMargins(margins);
}
/** Returns the current textfield string. In most cases this
* is just the same as a String version of getValue, except
* that there may be padded blank spaces at the left.
*/
public String getText() {
return(textfield.getText());
}
/** This sets the TextField value directly. Use with extreme
* caution since it does not right-align or check if value
* is numeric.
*/
public void setText(String text) {
textfield.setText(text);
}
/** Returns the Font being used by the textfield.
* Courier bold 12 is the default.
*/
public Font getFont() {
return(textfield.getFont());
}
/** Changes the Font being used by the textfield. */
public void setFont(Font textFieldFont) {
textfield.setFont(textFieldFont);
}
/** The size of the current font */
public int getFontSize() {
return(getFont().getSize());
}
/** Rather than setting the whole font, you can just set the
* size (Monospaced bold will be used for the family/face).
*/
public void setFontSize(int size) {
setFont(new Font("Monospaced", Font.BOLD, size));
}
/** Determines if the textfield is editable. If it is, you can
* enter a number to change the scrollbar value. In such a
* case, entering a value outside the legal range results in
* the min or max legal value. A non-integer is ignored.
*/
public boolean isEditable() {
return(textfield.isEditable());
}
/** Determines if you can enter values directly into the
* textfield to change the scrollbar.
*/
public void setEditable(boolean editable) {
textfield.setEditable(editable);
}
// Sets a right-aligned textfield number.
private void setTextFieldValue() {
int value = scrollbar.getValue();
int digits = numDigits(scrollbar.getMaximum());
String valueString = padString(value, digits);
textfield.setText(valueString);
}
// Repeated String concatenation is expensive, but this is
// only used to add a small amount of padding, so converting
// to a StringBuffer would not pay off.
private String padString(int value, int digits) {
String result = String.valueOf(value);
for(int i=result.length(); i>>>>>>>>>>>>>>>>>>>
ScrollbarPanel.java A Panel with adjustable top and bottom insets, used by the Slider class to change the thickness of the Slider.
>>>>>>>>>>>>>>>>>>>
import java.awt.*;
/** A Panel with adjustable top/bottom insets value.
* Used to hold a Scrollbar in the Slider class.
*
*******************
public class ScrollbarPanel extends Panel {
private Insets insets;
public ScrollbarPanel(int margins) {
setLayout(new BorderLayout());
setMargins(margins);
}
public Insets insets() {
return(insets);
}
public int getMargins() {
return(insets.top);
}
public void setMargins(int margins) {
this.insets = new Insets(margins, 0, margins, 0);
}
}
<<<<<<<<<<<<<<<<<<<<<
Similar Posts