aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes How to change color of a content pane after using borderlayout Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to change color of a content pane after using borderlayout" Watch "How to change color of a content pane after using borderlayout" New topic
Author

How to change color of a content pane after using borderlayout

luis eriq
Greenhorn

Joined: Oct 05, 2012
Posts: 4
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

Joined: Jun 09, 2003
Posts: 4632
> 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

Joined: Oct 05, 2012
Posts: 4
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

Joined: Jun 09, 2003
Posts: 4632
> 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

Joined: Oct 05, 2012
Posts: 4
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

Joined: Jun 09, 2003
Posts: 4632
> 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

Joined: Oct 13, 2005
Posts: 38818
    
  23
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

Joined: Jun 09, 2003
Posts: 4632
> 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

Joined: Oct 05, 2012
Posts: 4
Thank you all for your comments I will keep these in mind.
-Luis
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to change color of a content pane after using borderlayout