This discussion is archived
1 Reply Latest reply: Feb 25, 2013 2:22 AM by Tolls RSS

Initialise a Swing JPanel with Another

993100 Newbie
Currently Being Moderated
I have a problem initialising a JPanel .Here is my code :`

JPanel canvas = new JPanel();
JTextField txt = new JTextField();
txt.setBounds(50, 50, 200, 30);
canvas.add(txt);
JPanel can = canvas;

System.out.println("COMPONENTS 1 :" + canvas.getComponentCount());
System.out.println("COMPONENTS 1 :" + can.getComponentCount());
JTextField txt1 = new JTextField();
txt1.setBounds(100, 100, 200, 30);
can.add(txt1);
System.out.println("COMPONENTS 2 :" + canvas.getComponentCount());
System.out.println("COMPONENTS 2 :" + can.getComponentCount());

The output is :

COMPONENTS 1 :1
COMPONENTS 1 :1
COMPONENTS 2 :2
COMPONENTS 2 :2


What i want :

COMPONENTS 1 :1
COMPONENTS 1 :1
COMPONENTS 2 :1
COMPONENTS 2 :2

Any help will be highly appreciated

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points