my dog learned polymorphism*
The moose likes Swing / AWT / SWT and the fly likes better performance with a custom jtextfield Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "better performance with a custom jtextfield" Watch "better performance with a custom jtextfield" New topic
Author

better performance with a custom jtextfield

Chris Dancy
Ranch Hand

Joined: Feb 14, 2006
Posts: 136
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


("Anger is not an emotion, its a symptom of fear.")
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
just a thought..
JPanel (Borderlayout, or gridlayout(1,1)), with your gradient background
JTextfield, transparent, added to the JPanel.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Chris Dancy
Ranch Hand

Joined: Feb 14, 2006
Posts: 136
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.
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
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.
Chris Dancy
Ranch Hand

Joined: Feb 14, 2006
Posts: 136
I've solved the issue and here is how I did it using a BufferedImage:

Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
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.
Chris Dancy
Ranch Hand

Joined: Feb 14, 2006
Posts: 136
If I didn't say it before... thank you for the tip.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: better performance with a custom jtextfield
 
Similar Threads
textfield not taking in input...Occasionally
custom repaint on JButton on mouserollover
Compositing every component on Component?
drawing a good circle!
cursor gone from custom jtextfield..