How to display etched text in JLabel or on any component.
The text coloring changes when a component is enabled or disabled. But how do we show the text of a JLabel's text as etched?
I searched, looked the API, but couldn't find it. I am not sure what they call it for that type of text, I am thinking that it is etched so let me show a screenshot of what I want to explain.
See: http://nsis.sourceforge.net/File:NSISScreenshot1.png
Just see the text above the "< Back" button which has the following message: "Nullsoft Install System -- built on......"
So how can we show a text like that in swing?
[577 byte] By [
sri1025a] at [2007-10-3 4:03:49]

1. set color to white2. draw string at x+1,y+13. set color to gray4. draw string at x,y
Thank you very much,
I didn't get you. I subclassed the JLabel did the following code but couldn't get it working.
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;
public class MyLabel extends JLabel {
private static final long serialVersionUID = 1;
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.WHITE);
g.drawString(getText(), getX() + 1, getY() + 1);
g.setColor(Color.GRAY);
g.drawString(getText(), getX(), getY());
}
public static void main(String[] args) throws Throwable {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
JFrame frame = new JFrame();
JLabel label = new MyLabel();
label.setText("My Text");
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(label);
frame.setSize(100, 100);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Well that's just the mechanics of the rendering - JLabel does a lot more besides; and in any case the actual rendering is done by the UI object, not the JLabel.
It works out of the box for me if you just call setEnabled(false) on a JLabel. Which you don't seem to be doing :o) ...ditch your subclass and just disable a label.
I could disable the label at any time, but I wanted to know whether the text etching thing can be done in swing. Disabling is a different effect from etching I guess.
I'm not sure what you're trying to achieve. One of these three:
If you're rendering from first principles you need to use my first reply - take a look at BasicLabelUI and you'll see it does exactly that.
If you're using a JLabel and you want it rendered etched you can set it disabled, assuming your etching is to denote disablement, as it normally is.
If you want a label which renders etched text when enabled, then use something like this - which basically overrides the basic 'enabled' rendering with the 'disabled' rendering; adjust the colours to taste.
public class EtchedLabelUI extends BasicLabelUI
{
protected void paintEnabledText(JLabel l, Graphics g, String s, int textX, int textY)
{
int accChar = l.getDisplayedMnemonicIndex();
Color background = l.getBackground();
g.setColor(background.brighter());
BasicGraphicsUtils.drawStringUnderlineCharAt(g, s, accChar,
textX + 1, textY + 1);
g.setColor(background.darker());
BasicGraphicsUtils.drawStringUnderlineCharAt(g, s, accChar,
textX, textY);
}
}
To use it just do this.
JLabel foo = new JLabel();
foo.setUI(new EtchedLabelUI());
