Solved

Why Component is not visible

Posted on 2004-04-30
10
252 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Python Assistance 7 80
why cannot we forward request once the response is committed 2 42
difference of if loops 23 49
Java exception bubble up 2 18
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

773 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