Limiting the text area in a JTextField

I'm trying to add an icon to the right side of a JTextField, similar to the way the RSS/Security icons sometimes appear on the right side of the address bar in Firefox. Getting the icon to display was simple enough, but the problem is that if there's enough text in the field, it'll display underneath the icon. Is there some way to tell JTextField to only use a portion of itself to display text?

I tried overriding the getHorizontalVisibility() method, but didn't have much luck. The only other method I can think of would be to paint the field normally, then paint a fake, longer right side on top of the field and put the icon in that. But hopefully there's a better way.

[693 byte] By [Rannica] at [2007-11-26 17:00:28]
# 1
Use a panel with a BorderLayout. Add the text field to the center and the icon to the east. You can use the border of the text field to be the border of the panel so it looks like a single component.
camickra at 2007-7-8 23:28:14 > top of Java-index,Desktop,Core GUI APIs...
# 2
One bit I should've mentioned is that the icon is transparent in parts, so the background of the field matters. I want it to look like the icon is inside the field. I also need the component to subclass JTextField.
Rannica at 2007-7-8 23:28:14 > top of Java-index,Desktop,Core GUI APIs...
# 3
Use a JTextPane. You can use the insertIcon() method to add an image.
camickra at 2007-7-8 23:28:14 > top of Java-index,Desktop,Core GUI APIs...
# 4
I certainly do appreciate the input, but I'm looking to do specifically what I said in the original post: make a JTextField only display text in a certain part of its area. The bit about the icon was just background.
Rannica at 2007-7-8 23:28:14 > top of Java-index,Desktop,Core GUI APIs...
# 5

Well your requirement isn't clear (to me at least).

Try setting the layout of the text field to use a BorderLayout. Then create a JLabel and add the icon to the label. Set the label opaque and set the background of the label the same as the background of the text field. Then add the label to the East of the text field.

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the code retains its original formatting.

camickra at 2007-7-8 23:28:14 > top of Java-index,Desktop,Core GUI APIs...
# 6

My question is more academic than practical... I'm sure we can come up with a hundred different ways to display an icon next to a text field. Hopefully this little guy will illuminate what I'm talking about.

import java.awt.AlphaComposite;

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Graphics2D;

import javax.swing.JFrame;

import javax.swing.JTextField;

public class LimitedTextField extends JTextField {

@Override

protected void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D g2d = (Graphics2D) g.create();

g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,

(float) .5));

g2d.setColor(Color.RED);

g2d.fillRect(getWidth() - 16, 0, 16, getHeight());

g2d.dispose();

}

public static void main(String[] args) {

JFrame f = new JFrame("Text Field Test");

LimitedTextField field = new LimitedTextField();

f.getContentPane().add(field);

f.pack();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setVisible(true);

}

}

This will draw a red square over the right side of a JTextField. I want to figure out a way to prevent text from being drawn under that square. In other words, I want the text field to behave like it's width is 16 pixels less than what it actually is. I would think there would be some way to accomplish that by scrolling the text or something, but I can't seem to make it happen.

Rannica at 2007-7-8 23:28:14 > top of Java-index,Desktop,Core GUI APIs...
# 7

Create a compound border with the outer border being the original border and the inner border painting your icon. The important thing is to paint the textfield's background before painting the icon itself (in the Border.paintBorder). If you define proper insets (0,0,0,icon.getIconWidth()), you'll get the "illusion" of transparent icon.

kirillga at 2007-7-8 23:28:14 > top of Java-index,Desktop,Core GUI APIs...
# 8
Clever! That worked perfectly. You retroactively get duke stars. :)Thanks!
Rannica at 2007-7-8 23:28:14 > top of Java-index,Desktop,Core GUI APIs...
# 9
Glad it works. Now, if only i knew what to do with these stars...
kirillga at 2007-7-8 23:28:14 > top of Java-index,Desktop,Core GUI APIs...
# 10
Use them to put down people with fewer stars? Are they good for anything else?
Rannica at 2007-7-8 23:28:14 > top of Java-index,Desktop,Core GUI APIs...
# 11
I think i saw an interview somewhere on java.sun.com with a guy who got the most stars for that particular month. Eternal glory, in other words :)
kirillga at 2007-7-8 23:28:14 > top of Java-index,Desktop,Core GUI APIs...