Skip to main content

Simple Java Problem help about how to underline

1 reply [Last post]
glycerides
Offline
Joined: 2010-12-14
Points: 0

im new in java programming and a current student want to know how can i add underline and also strikethrough in my program..
heres my code:
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JCheckBox;

public class CheckBoxFrame extends JFrame
{
private JTextField textField;
private JTextField textField2;
private JCheckBox boldJCheckBox;
private JCheckBox italicJCheckBox;
private JCheckBox strikethroughtJCheckBox;
private JCheckBox underlineJCheckBox;

public CheckBoxFrame()
{
super("JCheckBOx Test");
setLayout(new FlowLayout());

textField = new JTextField("Watch the font style change", 20);
textField2 = new JTextField("Watch the font style change", 20);
textField.setFont( new Font("Serif", Font.PLAIN, 14));
textField2.setFont( new Font("Serif", Font.PLAIN, 14));
add( textField);
add( textField2);

boldJCheckBox = new JCheckBox("Bold");
italicJCheckBox = new JCheckBox("Italic");
underlineJCheckBox = new JCheckBox("Underline");
strikethroughtJCheckBox = new JCheckBox("StrikeThrought");
add(boldJCheckBox);
add(italicJCheckBox);
add(underlineJCheckBox);
add(strikethroughtJCheckBox);

CheckBoxHandler handler = new CheckBoxHandler();
boldJCheckBox.addItemListener(handler);
italicJCheckBox.addItemListener(handler);

}
private class CheckBoxHandler implements ItemListener
{
private int valBold = Font.PLAIN;
private int valItalic = Font.PLAIN;

public void itemStateChanged(ItemEvent event)
{

if(event.getSource()==boldJCheckBox)
valBold = boldJCheckBox.isSelected()? Font.BOLD: Font.PLAIN;

if (event.getSource()==italicJCheckBox)
valItalic = italicJCheckBox.isSelected()? Font.ITALIC: Font.PLAIN;

textField.setFont( new Font("Serif", valBold + valItalic , 14));
textField2.setFont( new Font("Serif", valBold + valItalic , 14));
}
}
}

//plzz help me improve my program..tnxx

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
javinpaul
Offline
Joined: 2011-01-10
Points: 0

You can use java.awt.font.TextAttribute class for doing underline or strke through . look java doc of this class and you will be able to understand it.
static TextAttribute TRANSFORM : Attribute key for the transform of a font.
static TextAttribute UNDERLINE: Attribute key for underline adornments.

Thanks
Javin
FIX Protocol tutorial