Solved

Why Component is not visible

Posted on 2004-04-30
10
253 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

856 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