Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to change color of a content pane after using borderlayout

 
luis eriq
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]
 
Michael Dunn
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)
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic