• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

adding label dynamically to frame

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have written program which creates a frame with 2 textfields
I want to enter some text into the textfield and retrieve that text

and present it on the frame as label.the prog is running.It even

retrieves text and print it on to console but Im not able to add the

label with the text retrieved dynamically.Is there a way out?? Thanks in advance.
regs- rajesh

import java.awt.*;
import java.awt.event.*;
public class PhoneBook extends Frame implements ActionListener {

String name;
Button save;TextField tn;
PhoneBook(){


Label ln=new Label("name");
tn=new TextField("name");
Label lp=new Label("PhoneNo");
TextField tp=new TextField(15);
save=new Button("SAVE");
add(ln);
add(tn);
add(lp);
add(tp);
add(save);
setLayout(new FlowLayout());
setSize(400,400);
setVisible(true);

save.addActionListener(this);

addWindowListener
(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
// System.exit(0);
dispose();
}
}
);




}

public void actionPerformed(ActionEvent a)
{
// Object ob=a.getSource();
if(a.getActionCommand()=="SAVE"){
String myname=tn.getText();
System.out.println(myname);
Label la=new Label(myname);
add(la);
}
}


public static void main(String st[]){

PhoneBook p=new PhoneBook();


}

}
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After that you add your label to the Frame, you need to refresh(repaint) it(the frame).


now you need to find how.
 
Rancher
Posts: 3373
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also look at the validate() and invalidate() methods. They have something to do with displaying/layouting components in a Container.
 
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


add validate() method as shown above.
 
Rajesh Chandra
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks to everyone for helping me out.
i got what i wanted and I learned new things.Thanks to all ranchers
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another solution is to add the label when the frame gets first constructed, and just change its text later.

Moving to Swing/AWT...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!