Page 1 of 2 12 LastLast
Results 1 to 20 of 30
  1. #1
    jiffi is offline Member
    Join Date
    Oct 2011
    Posts
    79
    Rep Power
    0

    Default Can't change the background of my content pane

    Hello,
    I have come across this stupid little bug: I can't change the background color of my content pane.

    I wanted to change it depending on RGB sliders, but even using the setBackground(Color.A_COLOR) doesn't work.
    Any suggestions?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  3. #3
    jiffi is offline Member
    Join Date
    Oct 2011
    Posts
    79
    Rep Power
    0

    Default 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

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  5. #5
    jiffi is offline Member
    Join Date
    Oct 2011
    Posts
    79
    Rep Power
    0

    Default Re: Can't change the background of my content pane

    Java Code:
    public void processJButton (String buttonText)
    {
    if (buttonText.equalsIgnoreCase("choose color"))
            {
                contentPane.setBackground(Color.white);
            }
    }
    That's basically the whole problem. The color doesn't change

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  7. #7
    jiffi is offline Member
    Join Date
    Oct 2011
    Posts
    79
    Rep Power
    0

    Default Re: Can't change the background of my content pane

    I am not sure what you mean by
    the variable contentPane is not referring to the actual contentPane of your GUI.
    It is the same variable that I used to declare it and to initialize it

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Can't change the background of my content pane

    Quote Originally Posted by jiffi View Post
    I am not sure what you mean by
    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.

    It is the same variable that I used to declare it and to initialize it
    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.

  9. #9
    jiffi is offline Member
    Join Date
    Oct 2011
    Posts
    79
    Rep Power
    0

    Default Re: Can't change the background of my content pane

    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);
    setting the color here works, setting it later doesn't.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Can't change the background of my content pane

    Quote Originally Posted by jiffi View Post
    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);
    setting the color here works, setting it later doesn't.
    In your code above, the contentPane variable refers to the top level window's contentPane. Elsewhere in your code it may not.

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default 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. #12
    jiffi is offline Member
    Join Date
    Oct 2011
    Posts
    79
    Rep Power
    0

    Default 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);
        }
    
    }

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default 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?

  14. #14
    jiffi is offline Member
    Join Date
    Oct 2011
    Posts
    79
    Rep Power
    0

    Default 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 by
    That's because in your SSCCE cp refers to the actual contentPane whereas in your program it doesn't.
    other then the variables, the syntax is exactly the same..

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Can't change the background of my content pane

    Quote Originally Posted by jiffi View Post
    no we are not. the frame is one and the same.

    I still don't get what you mean by
    other then the variables, the syntax is exactly the same..
    I'm sorry but I can't see code that reproduces the error, so I can't say what's causing it. You'll probably want to do some debugging of your original program. How big is it by the way?

  16. #16
    jiffi is offline Member
    Join Date
    Oct 2011
    Posts
    79
    Rep Power
    0

    Default 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 07:21 PM.

  17. #17
    jiffi is offline Member
    Join Date
    Oct 2011
    Posts
    79
    Rep Power
    0

    Default 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..

  18. #18
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  19. #19
    jiffi is offline Member
    Join Date
    Oct 2011
    Posts
    79
    Rep Power
    0

    Default Re: Can't change the background of my content pane

    Attachment 2250

    Sorry, my bad on this one

  20. #20
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Can't change the background of my content pane

    Your latest attachment is not valid. It needs to be a zip file.

Page 1 of 2 12 LastLast

Similar Threads

  1. adding multiple jpanels to content pane
    By tooktook22 in forum AWT / Swing
    Replies: 2
    Last Post: 01-19-2011, 07:40 PM
  2. Centering things a content pane
    By RKhadder in forum New To Java
    Replies: 1
    Last Post: 09-28-2010, 02:43 AM
  3. Update the JFrame after change the Content Pane
    By alisonchan30 in forum AWT / Swing
    Replies: 1
    Last Post: 04-26-2010, 07:22 AM
  4. Changing content pane of a JFrame?
    By dunafrothint in forum AWT / Swing
    Replies: 1
    Last Post: 03-11-2010, 11:21 PM
  5. Replies: 3
    Last Post: 01-06-2008, 12:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •