Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Why Component is not visible

Posted on 2004-04-30
10
Medium Priority
?
258 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

885 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