The Artima Developer Community
Sponsored Link

Java Answers Forum
PLS HELP abt making/adding a componet in runtime

4 replies on 1 page. Most recent reply: Nov 20, 2006 12:26 AM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Tanvir Hossain

Posts: 53
Nickname: tanvirtonu
Registered: Feb, 2006

PLS HELP abt making/adding a componet in runtime Posted: Nov 12, 2006 2:52 PM
Reply to this message Reply
Advertisement
I want to add a componet to a container(suppose Jframe)in runtime. I tried with the following code but it didnt work.
I wanted to add a JLabel to a JFrame in run time but it didnt work.Actually I want to add several JLabels in runtime Pls anybody tell me when ,how and where(in a class)to call repaint method and differece between repaint/revalidate/paint method. Pls help me. My code is give below- import java.awt.*;
import javax.swing.*;

public class GantChart extends JFrame {
JLabel jLabel1 = new JLabel();
JPanel jPanel1 = new JPanel();

public static void main(String[] args) {
GantChart gantChart = new GantChart();
gantChart.getContentPane().setLayout(null);
gantChart.setSize(new Dimension(422, 331));
gantChart.setBackground(Color.cyan);

Toolkit tk=Toolkit.getDefaultToolkit();
Dimension d=tk.getScreenSize();
gantChart.setLocation((d.width-300)/2,(d.height-300)/2);


new JLabel().setBackground(new Color(224, 112, 59));
new JLabel().setOpaque(true);
new JLabel().setBounds(new Rectangle(77, 35, 208, 21));
gantChart.getContentPane().add(new JLabel(), null);



gantChart.setVisible(true);
}


}


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: PLS HELP abt making/adding a componet in runtime Posted: Nov 13, 2006 10:51 PM
Reply to this message Reply
You DO know that you created 4 new labels?
You have to add jLabel1 to the frame, and should not create a new label for every step.


At the end you added one new label without any formatting or displaying text. It's just not visible, that's all.
You should also define where you want to put it (depending on the layout manager).


btw: EVERY component is added during runtime, there's just no other way to do it.


One more thing: You used the CODE tag to display your program code below. That's more than many of the other users here do, but for Java code it's better if you use the JAVA tag. This way you get syntax highlighting and all the leading space characters in your code.

Tanvir Hossain

Posts: 53
Nickname: tanvirtonu
Registered: Feb, 2006

Re: PLS HELP abt making/adding a componet in runtime Posted: Nov 15, 2006 12:25 PM
Reply to this message Reply
U r so helpful ,very very very THNX

Tanvir Hossain

Posts: 53
Nickname: tanvirtonu
Registered: Feb, 2006

Re: PLS HELP abt making/adding a componet in runtime Posted: Nov 17, 2006 2:08 PM
Reply to this message Reply
What actually I want to say is dat- How can I add a number of components in a loop(for/while)Like the following-
for(int a =1;a<10;a++)
{
add(new JLabel();)
}
It will only create some objets .But what if I want to set the background color,text,size,location(setBounds) and anything else for each instance of dat component and add it to the container during runtime.I hope u get me now.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: PLS HELP abt making/adding a componet in runtime Posted: Nov 20, 2006 12:26 AM
Reply to this message Reply
Well, you have to access the EXISTING label, in your example you created a new label for every step.

There are two ways:
for(int a =1;a<10;a++)
{
    JLabel tempLabel= new JLabel();
    tempLabel.set ...
    add(tempLabel);
}


The other way would be just adding all the labels to the frame, then accessing them using the getComponent() method of the frame.
Way 1 is clearly better.

Flat View: This topic has 4 replies on 1 page
Topic: PLS HELP abt making/adding a componet in runtime Previous Topic   Next Topic Topic: how to place other Widgets (ie.Label,Button) on Sash? urgent

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use