Why Component is not visible

why does the compoent doesnt seems to visible
package test2;

import java.awt.Color;
import java.awt.Container;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * @author maheshexp
 */

class Comp extends JComponent{
      public Comp(){
            this.setSize(30,30);
            this.setBackground(Color.GREEN);
      }
}

class Window3 extends JFrame{
      public Window3(){
            super("Window");
            Comp c = new Comp();
            this.setSize(400, 400);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            JPanel p = new JPanel();
            p.setLayout(null);
            p.add(c);
            
            Container pane= this.getContentPane();
            pane.add(p);
            this.validate();
            this.show();
      }
}

public class Test5 {

      public static void main(String[] args) {
            new Window3();
      }
}
LVL 7
maheshexpAsked:
Who is Participating?
 
objectsConnect With a Mentor Commented:
cause it's a base class that doesn't do any painting.
0
 
91mustangCommented:
use :
this.setBounds(10,10,50,50);
0
 
91mustangCommented:
rather
c.setBounds(10,10,50,50);

with your start point and sizes
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
maheshexpAuthor Commented:
still i can't see the component with out any border or color...
why that color is not visible???
0
 
objectsCommented:
because its not opaque
call setOpaque(true);
0
 
maheshexpAuthor Commented:
included these two lines....in the Comp Constructor
>this.setBounds(10,10,50,50);
>setOpaque(true);

 but sillt not working i.e i can't see anything...did any one run that code????
0
 
objectsCommented:
try using a panel subclass instead of a JComponent

class Comp extends JPanel{
0
 
maheshexpAuthor Commented:
in JPanel it is working, no need of all those fittings....but why not in JComponent???
0
 
maheshexpAuthor Commented:
thanks for your answers objects &  91mustang
i tried drawing a line between two JPanels, how to do that.....two jpanles are in size 30x30, i need a connecting line between them...when they moves the line should extend / shrink...
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> in JPanel it is working, no need of all those fittings....but why not in JComponent???

Try adding a new JLabel ( "" ) at the end. p.add ( new JLabel ( "" ) ) ;
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.