Results 1 to 5 of 5
  1. #1
    mevets is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Default Change JPanel background after its been set once

    I am trying to change the background color of my JPanel inside of my applet on button press. I have tried panel.setBackground(Color.GREEN). But nothing changes, so I figured I had to add repaint(), but still nothing.

    In my example I havent added a button handler much, but you can see the p11.setBackground() in the NewJApplet class at the end of init.

    How can I successfully change the backgrounds of my JPanels?
    Java Code:
    import java.awt.Color;
    import java.awt.GridLayout;
    import javax.swing.JApplet;
    import javax.swing.JButton;
    
    public class NewJApplet extends JApplet {
        public void init() {
            setLayout(new GridLayout(3, 2));
            PaintPanel p11 = new PaintPanel();
            PaintPanel p21 = new PaintPanel();
            PaintPanel p12 = new PaintPanel();
            PaintPanel p22 = new PaintPanel();
    
            JButton reset = new JButton("Reset");
            JButton quit = new JButton("Quit");
    
            add(p11);
            add(p21);
            add(p12);
            add(p22);
            add(reset);
            add(quit);
            p11.setBackground(Color.BLUE);
            p11.repaint();
            repaint();
        }
    }
    Java Code:
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import javax.swing.JApplet;
    import javax.swing.JComponent;
    import javax.swing.JPanel;
    
    public class PaintPanel extends JPanel {
        protected PaintPanel() {
            super();
            setBackground(Color.GRAY);
        }
    
        protected void paintComponent(Graphics g) {
            Graphics2D g2d = (Graphics2D) g;
            g2d.setColor(Color.GRAY);
            g2d.fillRect(0, 0, 250, 250);
            g2d.setColor(Color.RED);
            g2d.fillRect(50, 50, 50, 50);
    
        }
    }

  2. #2
    Michael Dunn is offline Member
    Join Date
    Jul 2008
    Posts
    62
    Rep Power
    0

    Default

    first thing to try is to add the usual suspects:

    Java Code:
    protected void paintComponent(Graphics g) {
            super.paintComponent(g);//<----------------
            Graphics2D g2d = (Graphics2D) g;

  3. #3
    mevets is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Default

    I added that and still the panel is still not changing its background

  4. #4
    Michael Dunn is offline Member
    Join Date
    Jul 2008
    Posts
    62
    Rep Power
    0

    Default

    did you close your browser (totally) between changes?

  5. #5
    mevets is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Default

    Thanks I was using chrome and I guess restarting that browser fixed the problem. I also did not need those repaint() methods.

Similar Threads

  1. background color with jpanel
    By hannerz06 in forum New To Java
    Replies: 6
    Last Post: 03-31-2010, 04:25 AM
  2. unable to change the TextArea.background !
    By Y. Progammer in forum New To Java
    Replies: 2
    Last Post: 02-28-2010, 05:55 PM
  3. how to put a background image in a JPanel
    By yanipao in forum New To Java
    Replies: 5
    Last Post: 10-21-2009, 03:27 PM
  4. my code doesn't change background color
    By javanoobita in forum New To Java
    Replies: 5
    Last Post: 02-22-2009, 05:30 AM
  5. when muse pressed the background change
    By pcman in forum Java Applets
    Replies: 1
    Last Post: 03-18-2008, 12:51 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
  •