Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Swing JTextArea :: Gradient Background  RSS feed

 
Marcus Hirschbine
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pseudo code warning.

I have a GUI for what is a sort of console for a series of apps I'm making.

The components that make up this SWING JForm include...

JPanel, which is centered on a Jframe. The JPanel's paintComponent method is overridden to include a gradient background.

(It's a modified gradient paint initialization as to be able to set custom colors read in from a text file for component colors).

The code is here:


The JPanel is added to the JFrame's contentPane() using BorderLayout's Center.
On the JPanel are components that include a JTextField and a JScrollPane.

They are arranged on the JPanel using GridBagLayout.

The JScrollPane has a JTextArea which is occupying the viewport.

You can find the rest of the initComponents() method here.


The problem is, how do I go about painting the backgrounds of the JTextArea and JTextField in the same manner that I did the JPanel while maintaining the visibility of text/foreground entities?

I have tried overriding the components' paintComponent() methods but the foreground entities (text, carot etc.) were not visible (or as I could imagine it, foreground entities were painted under the gradient background). How do I fix this?

Before overriding paintComponent().... (Text and carot is visible, I should note I only care about the carot in the JTextField as the JTextArea is not edible).


After overriding paintComponent().... (No text, no carot, no juice).


Quick edit: The scrollbar UI is not visible either after overriding.

Correction: The scrollbar UI is visible. Just didn't type enough to make it visible X$
 
Rob Camick
Ranch Hand
Posts: 2787
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Way too much code. Post a proper SSCCE that demonstrates the problem.

That is create a JFrame with a JTextArea and show the custom painting code you are using. The painting code doesn't have to be exactly the same as your real code. Just hard code the gradient paint Colors. Then once you get the simple example working you fix your real program. If you can't get the simple example to work, then you can post the SSCCE here.
 
Marcus Hirschbine
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Source and build of SSCCE:
build is in dist/

SSCCE:
https://www.dropbox.com/s/xdaw86w2xhrbjue/GradientSSCCE.rar?dl=0

*Necessarily elaborate commentate warning
 
Rob Camick
Ranch Hand
Posts: 2787
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The SSCCE code should be posted in forum just like your other code.

I would expected the code to be less than 50 lines since it only has a JFrame and a JTextArea with some simple custom gradient painting.
 
Marcus Hirschbine
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:The SSCCE code should be posted in forum just like your other code.

I would expected the code to be less than 50 lines since it only has a JFrame and a JTextArea with some simple custom gradient painting.


Oh, my bad.

 
Rob Camick
Ranch Hand
Posts: 2787
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at your painting code.

First you invoke super.paintComponent(), which does the default painting of the text area which is to paint the background and then paint the text.

Then you do your gradient painting, which paints over top of the text.

So the order of the code should be:

1. do the gradient painting
2. super.paintComponent();

Now to prevent the default painting code from clearing the gradient background you need to use:



So now when you invoke super.paintComponent() the default background painting code will not be executed.
 
Marcus Hirschbine
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:Take a look at your painting code.

First you invoke super.paintComponent(), which does the default painting of the text area which is to paint the background and then paint the text.

Then you do your gradient painting, which paints over top of the text.

So the order of the code should be:

1. do the gradient painting
2. super.paintComponent();

Now to prevent the default painting code from clearing the gradient background you need to use:



So now when you invoke super.paintComponent() the default background painting code will not be executed.


I made the changes to the code you had me make. I did but it did not resolve the issues regarding the text being painted over. I can see that the text area is there, when I move the cursor over the text area the cursor does change into the text cursor. But I still am not able to visibly see the carot when the text area is focused nor do I see the text. I'll provide a picture of the result of the changes I made in the code shown below.



 
Rob Camick
Ranch Hand
Posts: 2787
12
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what you expect to see since the foreground color of the text is basically the same color as the gradient color.

Use the setForeground(...) and setCaretColor(...) methods to change the color of the text and caret so it contrasts with your gradient paint.
 
Marcus Hirschbine
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:I'm not sure what you expect to see since the foreground color of the text is basically the same color as the gradient color.

Use the setForeground(...) and setCaretColor(...) methods to change the color of the text and caret so it contrasts with your gradient paint.


Yes, I ended up changing the foreground and I got the colors. I do not know why I went so damn long without realizing that.

I actually meant to post saying I had realized that I saw the text after changing the font color to white.

Thank you so much for your help, just got it working.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!