views:

233

answers:

1

Is it possible to draw diagonal lines over a JTextField (or any other Swing control as well) without just putting a panel over the textbox?

+5  A: 

Create a custom JTextField and override the paint() method. Something like:

public void paint(Graphics g)
{
    super.paint(g);
    //  do custom painting here
}

Note: normally custom painting is done by overriding the paintComponent(..) method of a component. So you could also do:

public void paintComponent(Graphics g)
{
    super.paintComponent(g);
    //  do custom painting here
}

and the result will be the same because a JTextField does not have any child components added to it. However I suggested using paint(...) because this approach would work if you wanted to paint diagonal lines on a component (like JPanel) that does support child components. Please make sure you understand the difference between the two methods by reading the section from the Swing tutorial on Custom Painting.

Or another option would be to create a custom highlighter that draws diagonal lines over the selected text. The RectanglePainter can get you started with this approach.

So you have a couple of different options depending on your requirements.

camickr