0

I am not very good with Swings please help me out. senario: I am trying to add a new Jbutton with in the same frame, while clicking an existing Jbutton, but it was not working.

Thanks in Advance.

I am using below code

JButton addAnotherButton = new JButton("Add New Button");
addAnotherButton.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent event) {  
    if (event.getSource() == addAnotherButton) {
      JButton newButton = new JButton("New born Button");
      newButton.addActionListener(this);
    }
  }
});
addAnotherButton.setFont(new Font("Times New Roman", Font.BOLD, 12));
addAnotherButton.setBounds(276, 222, 137, 29);          
contentPane.add(addAnotherButton);
Mihriban Minaz
  • 3,043
  • 2
  • 32
  • 52
  • You aren't adding the new button to any container. – Arnaud May 30 '16 at 12:39
  • `addAnotherButton.setBounds(276, 222, 137, 29); ` 1) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson May 31 '16 at 00:02

1 Answers1

0

In your code you are adding the button to the contentPane:

contentPane.add(addAnotherButton);

The listeners does not add the button to any ui component, so you will not see it.

Maybe, what you want is to add the button to the same pane:

contentPane.add(newButton);
  • Thanks a lot @Matthias it's working fine but it is appearing after I minimize and maximize the window how to resolve that. Now my another problem is how to fix it permanently I mean it should appear always once we close and open Frame also. – Chandrashekhar Ch May 31 '16 at 05:18