Results 1 to 6 of 6
  1. #1
    snaquetime is offline Member
    Join Date
    Jun 2011
    Posts
    11
    Rep Power
    0

    Default Change JPanel background color

    Haven't done this before, but I assume the solution is fairly simple. You can see what I have tried below. I still see a gray background, and would like to have the background color I set showing:

    Java Code:
    import java.awt.Color;
    import javax.swing.BorderFactory;
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
    
    class ColorFrame extends JPanel {
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    
        private static void createAndShowGUI() {
            JFrame f = new JFrame("JFrame Demo");
            ColorFrame pan = new ColorFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new ColorFrame());
    //attempts at changing background color
            f.setForeground(Color.green);
            f.setBackground(Color.green);
            f.getContentPane().setBackground(Color.green);
            pan.setBackground(Color.yellow);
            f.pack();
            f.setSize(450, 400);
            f.setVisible(true);
        }
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default

    The ColorFrame you add to the JFrame f isn't the one on which you call setBackground.
    Java Code:
      private static void createAndShowGUI() {
        JFrame f = new JFrame("JFrame Demo");
        ColorFrame pan = new ColorFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // f.add(new ColorFrame());
        f.add(pan);
    //attempts at changing background color
        //f.setForeground(Color.green);
        //f.setBackground(Color.green);
        //f.getContentPane().setBackground(Color.green);
        pan.setBackground(Color.yellow);
        //f.pack();
        f.setSize(450, 400);
        f.setVisible(true);
      }
    db

  3. #3
    snaquetime is offline Member
    Join Date
    Jun 2011
    Posts
    11
    Rep Power
    0

    Default

    It would be helpful to know what I should do instead of what I am doing wrong, because I am not getting the results I expect or want, and therefore know that the above code is incorrect. Perhaps if you could add some more details, DarrylBurke, I might understand what I should change. Thanks

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

    Default

    Um, he already has shown you how to do it right. Perhaps you want to re-read his post and in particular the changes that he has made to your code.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,438
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by snaquetime View Post
    It would be helpful to know what I should do instead of what I am doing wrong, because I am not getting the results I expect or want, and therefore know that the above code is incorrect. Perhaps if you could add some more details, DarrylBurke, I might understand what I should change. Thanks
    It is quite beneficial to everybody if Darryl Burke answers a question; the reply is accurate, concise and to the point and also tells you how to solve your problem. The only thing you have to do is read it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    snaquetime is offline Member
    Join Date
    Jun 2011
    Posts
    11
    Rep Power
    0

    Default

    Sorry about my error. Somehow misread the reply. Thanks to everyone catching my mistakes

Similar Threads

  1. Change BufferedImage background color
    By Nosrettap in forum New To Java
    Replies: 0
    Last Post: 03-20-2011, 11:29 PM
  2. background color with jpanel
    By hannerz06 in forum New To Java
    Replies: 6
    Last Post: 03-31-2010, 03:25 AM
  3. JFrame window doesnt change background color
    By Addez in forum New To Java
    Replies: 7
    Last Post: 11-07-2009, 09:38 PM
  4. my code doesn't change background color
    By javanoobita in forum New To Java
    Replies: 5
    Last Post: 02-22-2009, 04:30 AM
  5. JButton onClick change color background
    By behrk2 in forum AWT / Swing
    Replies: 6
    Last Post: 07-09-2008, 04:54 PM

Posting Permissions

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