Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-09-2008, 04:11 AM
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 02-17-2008, 02:01 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,015
hardwired is on a distinguished road
Code:
import java.awt.*; import javax.swing.*; public class Test { public static void main(String[] args) { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setContentPane(getContentPane()); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); } private static JPanel createContentPane() { JPanel redPanel = new JPanel(); redPanel.setBackground(Color.red); redPanel.setLocation(10, 10); redPanel.setSize(50,50); return redPanel; } private static JPanel getContentPane(){ // GridLayout tends to ignore the size hints, // creates a grid of the available space and // tries to size the components to fill the // space of the grid cells. 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); mainPanel.add(createContentPane()); mainPanel.add(createContentPane()); mainPanel.add(createContentPane()); return mainPanel; } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
New: Want to understand Drawing... diRisig New To Java 1 02-05-2008 09:13 AM
drawing window BlitzA Advanced Java 0 12-30-2007 06:39 PM
drawing window BlitzA New To Java 0 12-30-2007 05:45 PM
Drawing outside paintComponent() DarkSide1 Java 2D 2 11-08-2007 11:36 PM
Help with Drawing a line Rgfirefly24 New To Java 1 08-06-2007 09:40 AM


All times are GMT +3. The time now is 06:30 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org