Results 1 to 20 of 30
- 12-10-2011, 03:38 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 79
- Rep Power
- 0
-
Re: Can't change the background of my content pane
It's impossible to tell what you're doing wrong based on the information provided. Consider telling and showing us more.
- 12-10-2011, 04:19 PM #3
Member
- Join Date
- Oct 2011
- Posts
- 79
- Rep Power
- 0
Re: Can't change the background of my content pane
Basically, the user chooses 3 values between 0 and 255 with JSliders. I then get the value from those sliders, use a UImanager and set the background color of the content pane to those color when he presses ok ((0,0,0) = black, (255,255,255) = white for example. )
But when he presses ok, nothing happens. so I tried setting the color with a predifined color (contentPane.setBackground(Color.white);), but even that doesn't work. The button IS pressed, but the color doesn't change
-
Re: Can't change the background of my content pane
Since you're still not showing code, well, about all I can do is say, best of luck with that.
- 12-10-2011, 04:56 PM #5
Member
- Join Date
- Oct 2011
- Posts
- 79
- Rep Power
- 0
Re: Can't change the background of my content pane
That's basically the whole problem. The color doesn't changeJava Code:public void processJButton (String buttonText) { if (buttonText.equalsIgnoreCase("choose color")) { contentPane.setBackground(Color.white); } }
-
Re: Can't change the background of my content pane
Changing the background color of a top-level window's contentPane works for me, so again your code must have a bug.
A guess based on the limited code presented so far: the variable contentPane is not referring to the actual contentPane of your GUI.
- 12-10-2011, 05:08 PM #7
Member
- Join Date
- Oct 2011
- Posts
- 79
- Rep Power
- 0
Re: Can't change the background of my content pane
I am not sure what you mean by
It is the same variable that I used to declare it and to initialize itthe variable contentPane is not referring to the actual contentPane of your GUI.
-
Re: Can't change the background of my content pane
What I mean is that I'm sure that contentPane refers to some component (else you'd have a NPE on your hands), but that there's a good chance that it doesn't refer to the component that is the current top window's contentPane.
Which means nothing to us since you're not showing us the code that demonstrates this (again!). You can make no assumptions about the veracity of your code until you've fixed the bug.It is the same variable that I used to declare it and to initialize it
- 12-10-2011, 05:22 PM #9
Member
- Join Date
- Oct 2011
- Posts
- 79
- Rep Power
- 0
Re: Can't change the background of my content pane
setting the color here works, setting it later doesn't.Java Code:public Container contentPane; public GUI() { setSize(FRAME_WIDTH,FRAME_HEIGHT); setLocationRelativeTo(null); setDefaultCloseOperation(DISPOSE_ON_CLOSE); contentPane = getContentPane(); contentPane.setLayout(null); contentPane.setBackground(Color.DARK_GRAY);
-
Re: Can't change the background of my content pane
-
Re: Can't change the background of my content pane
I'm sure that this thread is getting frustrating for you, and I know that it's frustrating for me since I constantly have to try to tease information out of you that you're reluctant to post for some reason. This would probably have all been answered quickly and accurately if you had created and posted an SSCCE to start with. Just saying.
- 12-10-2011, 05:47 PM #12
Member
- Join Date
- Oct 2011
- Posts
- 79
- Rep Power
- 0
Re: Can't change the background of my content pane
here is an SSCCE, but the problem is that it works… whereas my code doesn't. and i have not done anything different then i usually do.
Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; public class BackgroundFrame extends JFrame implements ActionListener { private JButton red,blue; private Container cp; public BackgroundFrame( ) { setSize(300,300); setLocationRelativeTo(null); setDefaultCloseOperation(EXIT_ON_CLOSE); setTitle(getClass().getName()); red = new JButton("Red"); blue = new JButton("Blue"); red.addActionListener(this); blue.addActionListener(this); cp = getContentPane(); cp.setBackground(Color.pink); cp.setLayout(new FlowLayout()); cp.add(red); cp.add(blue); } public void actionPerformed(ActionEvent e) { JButton s = (JButton) e.getSource(); //get the source of the event if ( s == red) { cp.setBackground(Color.red); } else if ( s == blue) { cp.setBackground ( Color.blue); } } public static void main (String[] args) { BackgroundFrame f = new BackgroundFrame(); f.setVisible(true); } }
-
Re: Can't change the background of my content pane
That's because in your SSCCE cp refers to the actual contentPane whereas in your program it doesn't. You'll need to figure out what's wrong. We're not dealing with the same two JFrames issue from before are we where you're changing the properties of the current JFrame but it has no effect on a second one?
- 12-10-2011, 05:57 PM #14
Member
- Join Date
- Oct 2011
- Posts
- 79
- Rep Power
- 0
Re: Can't change the background of my content pane
no we are not. the frame is one and the same.
I still don't get what you mean byother then the variables, the syntax is exactly the same..That's because in your SSCCE cp refers to the actual contentPane whereas in your program it doesn't.
-
Re: Can't change the background of my content pane
- 12-10-2011, 06:17 PM #16
Member
- Join Date
- Oct 2011
- Posts
- 79
- Rep Power
- 0
Re: Can't change the background of my content pane
this is not the same program that the other question was for. this one is rather short, maybe 500 lines total, and it's mostly GUI right now, not much logic.
I have just found something weird though. If I change the color in the ActionPerformed method, the color changes. now if I call another method from the ActionPerformed, it doesn't change. But Any other actions (like System.out.println() ) work when i call another method from the ActionPerformed.
After verification, if I use an If-then statement, the color doesn't change for some reason.Last edited by jiffi; 12-10-2011 at 06:21 PM.
- 12-10-2011, 07:04 PM #17
Member
- Join Date
- Oct 2011
- Posts
- 79
- Rep Power
- 0
Re: Can't change the background of my content pane
package.bluej.zip
I think if I give you my whole project you might be able to understand better my problem..
-
Re: Can't change the background of my content pane
That's not your project as all it contains is a single file "package.bluej" and no .java files.
- 12-11-2011, 02:14 AM #19
Member
- Join Date
- Oct 2011
- Posts
- 79
- Rep Power
- 0
Re: Can't change the background of my content pane
Attachment 2250
Sorry, my bad on this one
-
Similar Threads
-
adding multiple jpanels to content pane
By tooktook22 in forum AWT / SwingReplies: 2Last Post: 01-19-2011, 06:40 PM -
Centering things a content pane
By RKhadder in forum New To JavaReplies: 1Last Post: 09-28-2010, 01:43 AM -
Update the JFrame after change the Content Pane
By alisonchan30 in forum AWT / SwingReplies: 1Last Post: 04-26-2010, 06:22 AM -
Changing content pane of a JFrame?
By dunafrothint in forum AWT / SwingReplies: 1Last Post: 03-11-2010, 10:21 PM -
Selecting a JMenu paints over the JPanel on the content pane
By Swingset in forum AWT / SwingReplies: 3Last Post: 01-05-2008, 11:13 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks