-1

I am not a java programmer. But i have to solve this problem in java. I have a textbox and a button. after entering some text in the text box , by clicking the button i have to generate a new button whose text is same as that in the textbox. I have to create the buttons and add then to a Jpanel. Can anyone help me with this. This is the actionPerformed() i wrote, which was not working

 public void actionPerformed(ActionEvent e) {
    String str=textFeild1.getText();
    panel1.add(new JButton(str));
   }
  • For better help sooner, post an [MCVE](http://www.stackoverflow/mcve) (Minimal, Complete, Verifiable Example). See also the first example in [this answer](http://stackoverflow.com/a/9554657/418556) which adds new labels on button click. – Andrew Thompson Jul 17 '14 at 06:48
  • Try adding panel1.validate(); after you add Button to the panel. – user2640782 Jul 17 '14 at 06:51

1 Answers1

1

Try adding panel1.validate();

public void actionPerformed(ActionEvent e) {
    String str=textFeild1.getText();
    panel1.add(new JButton(str));
    panel1.validate();
}

The validate method is used to cause a container to lay out its subcomponents again. It should be invoked when this container's subcomponents are modified (added to or removed from the container, or layout-related information changed) after the container has been displayed. validate()

In your case you add a component and you have to validate it.

user2640782
  • 1,074
  • 8
  • 15