Results 1 to 5 of 5
- 04-10-2010, 06:05 AM #1
Member
- Join Date
- Apr 2010
- Posts
- 3
- Rep Power
- 0
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); } }
- 04-10-2010, 06:59 AM #2
Member
- Join Date
- Jul 2008
- Posts
- 62
- Rep Power
- 0
first thing to try is to add the usual suspects:
Java Code:protected void paintComponent(Graphics g) { super.paintComponent(g);//<---------------- Graphics2D g2d = (Graphics2D) g;
- 04-10-2010, 07:51 AM #3
Member
- Join Date
- Apr 2010
- Posts
- 3
- Rep Power
- 0
I added that and still the panel is still not changing its background
- 04-10-2010, 08:14 AM #4
Member
- Join Date
- Jul 2008
- Posts
- 62
- Rep Power
- 0
did you close your browser (totally) between changes?
- 04-14-2010, 01:07 AM #5
Member
- Join Date
- Apr 2010
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
background color with jpanel
By hannerz06 in forum New To JavaReplies: 6Last Post: 03-31-2010, 03:25 AM -
unable to change the TextArea.background !
By Y. Progammer in forum New To JavaReplies: 2Last Post: 02-28-2010, 04:55 PM -
how to put a background image in a JPanel
By yanipao in forum New To JavaReplies: 5Last Post: 10-21-2009, 02:27 PM -
my code doesn't change background color
By javanoobita in forum New To JavaReplies: 5Last Post: 02-22-2009, 04:30 AM -
when muse pressed the background change
By pcman in forum Java AppletsReplies: 1Last Post: 03-17-2008, 11:51 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks