wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes underline the text Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "underline the text" Watch "underline the text" New topic
Author

underline the text

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi there!
I am trying to underline text when user select "Underline" style,
text must remain underlined until user changes his choice.
In my dummy example I try to redraw the line after font size increases, but it works only when I again select "Underline" button.
I do not want to use JTextPane and StyledDocument, all I need to do just dynamically display the sample for selected font family/size/style/color.
Thank you for help
Here is the code:
import java.awt.*;
import java.awt.event.*;
import com.sun.java.swing.event.*;
import javax.swing.*;
import javax.swing.text.*;
import java.util.*;

public class Test1 extends JFrame {
private JLabel textField = new JLabel("Underline me");

private JButton drawLine=new JButton("DrawLine");
private JButton remLine=new JButton("RemoveLine");
private JButton FontIncr=new JButton("IncreaseFontSize");
private boolean UNDERLINE=false;
public Test1() {
Container contentPane = getContentPane();
//setAttributes();
textField.setFont(new Font("Dialog", Font.PLAIN, 8));
textField.setOpaque(true);
textField.setBackground(Color.cyan);
textField.setForeground(Color.orange);
contentPane.add(drawLine,BorderLayout.EAST);
contentPane.add(remLine,BorderLayout.WEST);
contentPane.add(textField,BorderLayout.NORTH);
contentPane.add(FontIncr,BorderLayout.SOUTH);
//create jbutton's action listener
ActionListener buttonActionListener=new ActionListener(){
public void actionPerformed(ActionEvent event) {
Object object = event.getSource();
if(object==drawLine){
UNDERLINE=true;
underlineText();
}
else if (object==remLine){
UNDERLINE=false;
cleanLine();
}
else if (object==FontIncr){
Font f=textField.getFont();
int i=f.getSize()+2;
textField.setFont(new Font("Dialog", Font.PLAIN, i));
validate();
if (UNDERLINE){
//redraw line after increasing the font size
underlineText();
repaint(50);
}
}
}
};
drawLine.addActionListener(buttonActionListener);
remLine.addActionListener(buttonActionListener);
FontIncr.addActionListener(buttonActionListener);
}
public static void main(String args[]) {
GJApp.launch(new Test1(),"setting Attributes",300,300,450,300);
}
public void paint(Graphics g){
super.paint(g);

}
protected Rectangle getSampleTextRectangle(Graphics g){
Insets i=getInsets();
String val=textField.getText();
FontMetrics fm=g.getFontMetrics();
//int h=fm.getMaxAscent()+fm.getDescent();
Rectangle r=textField.getBounds();
//calculate width of leading blanck space
int ix1=fm.stringWidth(val.substring(0,val.indexOf("U")));
//int x1=i.left+ix1;
int x1=ix1;
//calculate lower y position of the line relatively to height of the container
System.out.println("getSampleTextRectangle g.getFontMetrics(g.getFont()).getDescent() =" +g.getFontMetrics(g.getFont()).getDescent());
int y1=r.height - g.getFontMetrics(g.getFont()).getDescent();
//calculate length of the text/line
int x2=java.lang.Math.min(fm.stringWidth(val),textField.getBounds().width-i.right);
//int y2=h-2;
Rectangle rr=new Rectangle(x1,y1,x2,y1);
return rr;
}
protected void cleanLine(){
Graphics g=textField.getGraphics();
g.setColor(Color.cyan);
Rectangle r=getSampleTextRectangle(g);
//repaint line area
System.out.println("Remove the line "+r);
g.fillRect(r.x,r.y,r.width,r.y);
}
//protected void underlineText(Graphics g){
protected void underlineText(){
Graphics g=textField.getGraphics();
g.setColor(Color.orange);
Rectangle r=getSampleTextRectangle(g);
//draw a horizontal line,y1 and y2 coordinates are identical
g.drawLine(r.x,r.y,r.width,r.y);
System.out.println("Draw the line "+r);
}
}
class GJApp extends WindowAdapter {

private GJApp() {}
public static void launch(final JFrame f, String title,
final int x, final int y,
final int w, int h) {
f.setTitle(title);
f.setBounds(x,y,w,h);
f.setVisible(true);
f.setDefaultCloseOperation( WindowConstants.DISPOSE_ON_CLOSE);
f.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
System.exit(0);
}
});
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: underline the text
 
Similar Threads
Having touble adding graphics to JPanel
problems using paint(Graphics g)
2D graphic repaint() logic
How to draw straight line instead of curved line??
how to highlight(change color) of the button and its neighbours when it is clicked