Solved

Why Component is not visible

Posted on 2004-04-30
10
250 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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:mayankeagle
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

708 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

12 Experts available now in Live!

Get 1:1 Help Now