Showing components of JFrame

I got a class which extends JFrame, this class contains only a JTextField that diplays the string argument in the field. I am trying to create two instances of this class, but only the second frame appears with the name while the first frame does not show anything.

Here is the code:

public class SimpleFrame extends JFrame {

static JTextField nameField = new JTextField();

public SimpleFrame (String Name){

nameField.setText(Name);

add(nameField);

setVisible(true);

pack();

}

public static void main(String[] args) {

SimpleFrame sf1 = new SimpleFrame("Tony");

SimpleFrame sf2 = new SimpleFrame("Nick");

}

}

when runing this Two JFrame appear but only the second one shows Nick while the first frame does not show Tony.?

Any help is appreciated.

[848 byte] By [ALIZa] at [2007-11-26 20:44:31]
# 1
static JTextField nameField = new JTextField();Why static? I think the problem lies here.
OleVVa at 2007-7-10 2:05:21 > top of Java-index,Desktop,Core GUI APIs...
# 2
Yes the problem with static text filed. Remove the static keyword from the the declaration of JtextField then it works fine
zshaneefa at 2007-7-10 2:05:21 > top of Java-index,Desktop,Core GUI APIs...
# 3
Yes replacing static with private sort it out, but i am using static as I need to use the text of the JTextField from another class by using:SimpleFrame.nameFieldIs there a way to keep it static and still show both JFrames correctly.Thaks.
ALIZa at 2007-7-10 2:05:21 > top of Java-index,Desktop,Core GUI APIs...
# 4

u need to write getters and setters for the text field to get the text of text field and for setting the text.

like

public String getTextFromNameField(){

return nameFeild.getText();

}

In another class u need to create the instance of SimplaeFrame and call the above method

like

SimplaeFrame frame = new SimplaeFrame ();

frame.getTextFromNameFeild()

zshaneefa at 2007-7-10 2:05:21 > top of Java-index,Desktop,Core GUI APIs...
# 5

> Is there a way to keep it static and still show both

> JFrames correctly.

No way,

static means there is only one variable holding the reference to the text field no matter how many instances of the class you create. How could one and the same text field be inside two different windows?

OleVVa at 2007-7-10 2:05:21 > top of Java-index,Desktop,Core GUI APIs...
# 6
Thanks all.
ALIZa at 2007-7-10 2:05:21 > top of Java-index,Desktop,Core GUI APIs...