import javax.swing.*;
import java.awt.Color;
import java.awt.Font;
public class OptionButton extends JButton
{
public final static int ON = 1;
public final static int OFF = 0;
private boolean disabled = false;
private int state = OFF;
private JLabel myLabel;
private String trueText = null;
private Color[] backgroundArray = {null, null};
private Color textColor = Color.black;
public OptionButton()
{
super();
}
/**
* Sets the JLabel
that this OptionButton
* is associated with. Necessary for the button to work properly with
* text (the button does not have text inside it by default).
* @param label The JLabel
that this OptionButton
* is associated with.
*/
public void setLabel(JLabel label)
{
myLabel = label;
if (label.getText() != null)
{
trueText = label.getText();
}
}
/**
* resturns the JLabel
that is currently associated with this
* OptionButton
.
* @return the JLabel
that this OptionButton
* is associated with.
*/
public JLabel getJLabel()
{
return myLabel;
}
public void setText(String text)
{
String pass = text;
trueText = text;
pass = "" +
text + "";
myLabel.setText(pass);
}
public void setOnBackground(Color background)
{
backgroundArray[ON] = background;
}
public void setOffBackground(Color background)
{
backgroundArray[OFF] = background;
}
public void setTextColor(Color textColor)
{
this.textColor = textColor;
setText(trueText);
}
public void setFont(Font font)
{
try
{
myLabel.setFont(font);
}
catch(NullPointerException e)
{
super.setFont(font);
}
}
public void changeState()
{
if (disabled)
return;
state ^= 1;
try
{
setBackground(backgroundArray[state]);
}
catch (NullPointerException e)
{
state ^= 1;
//throw <>
System.err.println("Unconfigured State\n");
return;
}
}
public void changeState(int state)
{
if (this.state == state || disabled)
return;
this.state = state;
try
{
setBackground(backgroundArray[state]);
}
catch (NullPointerException e)
{
//throw <>
System.err.println("Unconfigured State\n");
return;
}
}
public boolean isDeactivated()
{
return disabled;
}
public void deactivate()
{
if (disabled)
return;
disabled=true;
setBackground(Color.gray);
myLabel.setVisible(false);
}
public void activate()
{
if (!disabled)
return;
disabled = false;
setBackground(backgroundArray[state]);
myLabel.setVisible(true);
}
}