Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

adding label dynamically to frame  RSS feed

 
Rajesh Chandra
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();


}

}
 
Julien Grenier
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.
 
Norm Radder
Rancher
Posts: 1733
22
  • 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.
 
Sripathi Krishnamurthy
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
 
Ilja Preuss
author
Sheriff
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!