View Single Post
  #1 (permalink)  
Old 02-09-2008, 04:11 AM
Djangolo Djangolo is offline
Member
 
Join Date: Feb 2008
Posts: 1
Djangolo is on a distinguished road
Drawing on aJPanel
I've built a GUI using Netbeans. The GUI includes a JPanel and I want to draw graphics on this panel. The instance of JPanel is called jPanel1 and is set to preferred size 604x310.

If I create a bit of code like this -

Code:
public JPanel createContentPane () { JPanel redPanel = new JPanel(); redPanel.setBackground(Color.red); redPanel.setLocation(10, 10); redPanel.setSize(50,50); return redPanel; }
and then add this to jPanel1 like this -

Code:
jPanel1.add(createContentPane());
the red square appears on the panel. Simple stuff.
But if I do this -

Code:
public JPanel createContentPane (){ JPanel mainPanel = new JPanel(new GridLayout(2, 4, 10, 30)); mainPanel.setPreferredSize(new Dimension(604, 310)); JPanel tempPanel = new JPanel(); tempPanel.setBackground(Color.blue); tempPanel.setMinimumSize(new Dimension(50, 50)); tempPanel.setMaximumSize(new Dimension(50, 50)); tempPanel.setPreferredSize(new Dimension(50, 50)); mainPanel.add(tempPanel); return mainPanel;
and then try and add it to jPanel1, nothing is displayed.

Can anyone tell me why this happens, 'cos I don't understand what's wrong.

Thanks for any help.

DJ
Reply With Quote
Sponsored Links