This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

How to change color of a content pane after using borderlayout

 
Greenhorn
Posts: 4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to change the color of the contentpane but I have tried the getContentPane() function and it won't work. This is my code below: If anyone can help it is greatly appreciated.

[Added code tags - see UseCodeTags]
 
Ranch Hand
Posts: 4632
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> I have tried the getContentPane() function and it won't work.

worked OK for me - the color behind the central label changed to the selected color
 
luis eriq
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where in the program did you use the getContentPane()? I tried it at lines 80,84, and 88 but it did not work out for me. I am not sure if I am using it wrong. Thanks for your quick reply.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> Where in the program did you use the getContentPane()?

in the ButtonListener class:

//window.setBackground(Color.RED);
window.getContentPane().setBackground(Color.RED);
same for the other colors

also, you don't need a new listener for each of the buttons
create a single instance and add that to each. the if/else's
will handle which button is clicked.
or, you could add an anonymous inner class actionListener to
each button, each listener's job will be to change to a specific color.
 
luis eriq
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried the suggestion you left and it worked. I must have forgotten to add the instance of the JFrame to the set background command. Thank you soo much for your help. Could explain a little further as to how I can create a single instance of listener and add different buttons to it? Once again, thank you. Your help is greatly appreciated.
-Luis
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> Could explain a little further as to how I can create a single instance of listener and add different buttons to it?

here's one way


another way (ButtonListener class not needed)
 
Marshal
Posts: 65772
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Michael Dunn wrote: . . . another way (ButtonListener class not needed)

I would disagree about ButtonListener not being needed. I would have thought that is an ideal situation for a ButtonListener class. Only I would call it ColourListener, and you can writeYou can work out that the ColourListener class implements the ActionListener (←link) interface and has two fields, a Color and a Component. Now you can create the class: 14 lines excluding blank lines, I think. The reason I said Component is because that is the class highest in the inheritance tree which has the method for changing colour.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> I would disagree about ButtonListener
> not being needed.

it is not needed in that alternative example.

the OP can do it any way he likes, I've
simply given him options on the various
ways it can be done, as they may come in
handy in future projects.
 
luis eriq
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for your comments I will keep these in mind.
-Luis
 
This. Exactly this. This is what my therapist has been talking about. And now with a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!