Solved

Why Component is not visible

Posted on 2004-04-30
10
251 Views
Last Modified: 2008-03-04
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();
      }
}
0
Comment
Question by:maheshexp
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 4

Expert Comment

by:91mustang
ID: 10959515
use :
this.setBounds(10,10,50,50);
0
 
LVL 4

Expert Comment

by:91mustang
ID: 10959545
rather
c.setBounds(10,10,50,50);

with your start point and sizes
0
 
LVL 7

Author Comment

by:maheshexp
ID: 10960843
still i can't see the component with out any border or color...
why that color is not visible???
0
 
LVL 92

Expert Comment

by:objects
ID: 10964691
because its not opaque
call setOpaque(true);
0
 
LVL 7

Author Comment

by:maheshexp
ID: 10965249
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 92

Expert Comment

by:objects
ID: 10965306
try using a panel subclass instead of a JComponent

class Comp extends JPanel{
0
 
LVL 7

Author Comment

by:maheshexp
ID: 10965664
in JPanel it is working, no need of all those fittings....but why not in JComponent???
0
 
LVL 92

Accepted Solution

by:
objects earned 30 total points
ID: 10965692
cause it's a base class that doesn't do any painting.
0
 
LVL 7

Author Comment

by:maheshexp
ID: 10966356
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 10971308
>> 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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Which is the latest version of Apache Tomcatl 3 50
Image decoding from Camera 3 72
JUnit 4 @Before and @BeforeClass differences 3 48
servlet example 17 10
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now