Jul 6, 2008

Custom Jpanel Class Does Not Show When Instantiated

Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > Java, Java Servlets, Java Script, & JSP

free web hosting

Custom Jpanel Class Does Not Show When Instantiated

Jeune
Here's a class I made,

CODE


import javax.swing.JPanel;
import java.awt.Graphics;

public class customPanel extends JPanel
{
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.fillOval(50,10,60,60);
    }
}




when I instantiate that here....

CODE

import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.BorderLayout;
import javax.swing.JButton;
import java.awt.FlowLayout;

public class draw extends JFrame
{
    public draw()
    {
        super("Draw Demo");
        setLayout(new BorderLayout());    
        
        customPanel drawPanel= new customPanel();
        FlowLayout southFlowLayout = new FlowLayout();
        add(drawPanel,BorderLayout.SOUTH);
        
    }
    public static void main(String args[])
    {
        draw app = new draw();
        app.setSize(300,300);
        app.setVisible(true);
    }
}




...I don't see the circle. How come?

However when I do this...

CODE

import javax.swing.JPanel;
import java.awt.Graphics;
import javax.swing.JFrame;
import java.awt.GridLayout;

public class customPanel extends JPanel
{
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.fillOval(50,10,60,60);
    }
    public static void main(String args[])
    {
        JFrame app = new JFrame();
        app.setLayout(new GridLayout(3,1));
        customPanel drawPanel = new customPanel();
        app.add(drawPanel);
        app.setSize(300,300);
        app.setVisible(true);
        
    }
}


The circle shows up!

 

 

 


Reply

BhajunSingh
The reason it shows up in the second one is because it has a give height: 300. In your first example, the JPanel doesn't, and for some reason the SOUTH area in BorderLayout does not tend to show components without a specified height.

So, you could keep the first example by either adding something like drawPanel.setPreferredSize(new Dimension(300, 100)); or using BorderLayout.CENTER instead.

Reply

Jeune
QUOTE(BhajunSingh @ Sep 1 2006, 05:35 AM) *

The reason it shows up in the second one is because it has a give height: 300. In your first example, the JPanel doesn't, and for some reason the SOUTH area in BorderLayout does not tend to show components without a specified height.

So, you could keep the first example by either adding something like drawPanel.setPreferredSize(new Dimension(300, 100)); or using BorderLayout.CENTER instead.


Ok, so it does work. But I don't get it. Why? How? sad.gif

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Recent Queries:-
  1. creating a jpanel class - 19.73 hr back.
  2. add custom jpanel to jframe - 51.58 hr back.
  3. show jpanel when keypressed - 67.83 hr back.
  4. java jpanel class - 75.77 hr back.
  5. jpanel does not show up on jframe - 98.26 hr back.
  6. customize jpanel in java - 116.27 hr back.
  7. show jpanel in jframe - 122.94 hr back.
Similar Topics

Keywords : custom jpanel instantiated


    Looking for custom, jpanel, class, show, instantiated

Searching Video's for custom, jpanel, class, show, instantiated
advertisement



Custom Jpanel Class Does Not Show When Instantiated



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE