Forums Register Login

better performance with a custom jtextfield

+Pie Number of slices to send: Send
I originally posted this on the java.sun forums, http://forums.sun.com/thread.jspa?threadID=5361214&tstart=45, but was looking for a bit more help. Basically I'm drawing a custom jtextfield with a gradient background. The problem comes when text is actually being put into the textfield the cpu jumps and the performance is awful. Im assuming its from the textfield being redrawn with the gradient each time a letter is entered. I dont know the proper way to get around this, and in my previous post on the java.sun forums they had mentioned using a bufferedImage to draw onto, however I fairly new to swing and do not know how to implement that with a jtextfield...here is the code:


If anyone could show me a better way of doing this, or simply how to solve this problem using a bufferedImage i would appreciate it.

Sincerely,
Christopher Dancy
+Pie Number of slices to send: Send
just a thought..
JPanel (Borderlayout, or gridlayout(1,1)), with your gradient background
JTextfield, transparent, added to the JPanel.
+Pie Number of slices to send: Send
I don't see any reaosn to use Graphics.create() and dispose(); just draw on the Graphics object you're given. That will save some time.

You could also cache the GradientPaint object -- i.e., save it in a member variable. You probably want to discard it if the component is resized.
+Pie Number of slices to send: Send
Ernest: The amount of time saved from those things is marginal compared to what is happening. I've tried What Michael Dunn proposed and that seems to work good for now, though it feels very hackish. Either way its much much better than the other way.
+Pie Number of slices to send: Send
 

Michael Dunn wrote:just a thought..
JPanel (Borderlayout, or gridlayout(1,1)), with your gradient background
JTextfield, transparent, added to the JPanel.



Along the same lines, you could cache the gradient in a BufferedImage (updated only when the field is resized) and draw it with drawImage().

I guess this works better if the field is not transparent.
+Pie Number of slices to send: Send
I've solved the issue and here is how I did it using a BufferedImage:

+Pie Number of slices to send: Send
 

Chris Dancy wrote:I've solved the issue and here is how I did it using a BufferedImage



I'm glad my suggestion worked out for you.
+Pie Number of slices to send: Send
If I didn't say it before... thank you for the tip.
I am going to test your electrical conductivity with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 2681 times.
Similar Threads
drawing a good circle!
cursor gone from custom jtextfield..
custom repaint on JButton on mouserollover
textfield not taking in input...Occasionally
Compositing every component on Component?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 14:06:53.