Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Components I add to JApplet won't appear!

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Components I add to JApplet won't appear!

    For the life of me, I can't figure out what's wrong.

    I want my Applet to have two Panels. One in BorderLayout.Center, and another in South.

    Right now, all I'm trying to do is get one of the two panels to appear. It has a paint method which should draw a border, but nothing happens. In fact, the main content pane doesn't even show it's background color!

    Perhaps there's something wrong with the DisplayPanel class I define at the end? It's just supposed to be a JPanel, basically, but I needed to be able to paint on it. So, I defined a the DisplayPanel class.

     
    import java.awt.*;
    import javax.swing.*;
     
    public class ShooterApplet extends JApplet {
     
    	private int height, width;
    	Container mainContentPane;
    		DisplayPanel displayPanel; 
    		JPanel controlPanel;
     
    	public void init() {
     
    		mainContentPane = getContentPane();
    		mainContentPane.setSize(new Dimension(500,500));
    		mainContentPane.setLayout(new BorderLayout());
    		mainContentPane.setBackground(Color.GRAY);
     
    		displayPanel = new DisplayPanel();
    		mainContentPane.add(displayPanel, BorderLayout.CENTER);
     
    		validate();
     
    	}
     
    	public void start() {
     
    	}
     
    	public void paint(Graphics g) {
     
    	}
    }
     
    class DisplayPanel extends JPanel {
     
    	private int height, width;
     
    	public DisplayPanel() {
    		super();
    		height = getHeight();
    		width = getWidth();
    	}
     
    	public void paint(Graphics g) {
    		g.drawRect(0,0,width-1,height-1);
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,414
    Thanks
    49
    Thanked 2,214 Times in 2,187 Posts

    Default Re: Components I add to JApplet won't appear!

    What is the empty paint method for
    What is the width and height of the DisplayPanel?

  3. #3
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Components I add to JApplet won't appear!

                    height = getHeight();
    		width = getWidth();
    height and width are = 0

  4. #4
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Components I add to JApplet won't appear!

    when you call getHeight() and getWidth() when extending a Swing Component like a JPanel one(you can see this typical coding elsewhere), you should always be aware of values that these methods return, and by looking at your code, it seems like your attempting to create a custom component via JPanel as a container for other components.., well, be careful on customizing one, because you might end with messing up painting things, if that is your goal, you might consider looking on Google samples.

Similar Threads

  1. Sockets in JApplet
    By shrey.haria in forum Java Networking
    Replies: 4
    Last Post: September 5th, 2011, 09:45 AM
  2. JApplet Not Running In Firefox
    By aussiemcgr in forum Java Theory & Questions
    Replies: 13
    Last Post: August 13th, 2011, 08:40 AM
  3. initializing a java JApplet
    By j_a_lyons in forum Java Theory & Questions
    Replies: 0
    Last Post: January 8th, 2011, 03:49 PM
  4. Cannot update Jlabel in JApplet
    By rin in forum Java Applets
    Replies: 2
    Last Post: April 17th, 2010, 08:21 AM
  5. JApplet containing JButton and a JLabel
    By JuneM in forum AWT / Java Swing
    Replies: 3
    Last Post: March 26th, 2010, 08:32 AM