Jump to content
xisto Community
Sign in to follow this  
Jeune

Custom Jpanel Class Does Not Show When Instantiated

Recommended Posts

Here's a class I made,

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....

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...

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!

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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? :D

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.