import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class UnderlineLabelTest
{
public static void main(String args[])
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final UnderlineLabel label = new UnderlineLabel("Hello");
final JToggleButton b0 = new JToggleButton("Underline");
b0.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
label.setUnderlined(b0.isSelected());
}
});
final JToggleButton b1 = new JToggleButton("Toggle text");
b1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if (b1.isSelected())
{
label.setText("World");
}
else
{
label.setText("Hello");
}
}
});
frame.getContentPane().setLayout(new GridLayout(0,3));
frame.getContentPane().add(label);
frame.getContentPane().add(b0);
frame.getContentPane().add(b1);
frame.pack();
frame.setVisible(true);
}
}
class UnderlineLabel extends JLabel
{
private String text;
private boolean underlined = false;
public UnderlineLabel(String text)
{
super(text);
this.text = text;
}
public void setUnderlined(boolean newUnderlined)
{
if (!underlined && newUnderlined)
{
super.setText("<html><u>"+text+"</u></html>");
underlined = newUnderlined;
}
else if (underlined && !newUnderlined)
{
super.setText(text);
underlined = newUnderlined;
}
}
public void setText(String text)
{
this.text = text;
if (underlined)
{
super.setText("<html><u>"+text+"</u></html>");
}
else
{
super.setText(text);
}
}
}