0
public class Create_JFrame extends JFrame{

public Create_JFrame(){

     //Create a Frame
     JFrame Frame = new JFrame("Bla-Bla");
     JPanel Panel_1 = new JPanel();
     JPanel Panel_2 = new JPanel();
     JButton Option_1 = new JButton("Option-1");

     //Layout management for Panels
     Frame.getContentPane().add(BorderLayout.WEST, Panel_1);

     //Add button to Panel
     Panel_1.add(Option_1);

     //Registering Listeners for all my buttons
     Option_1.addActionListener(new ListenerForRadioButton(Panel_2));

     //Make the frame visible
     Frame.setSize(500, 300);
     Frame.setVisible(true);
}//end of Main
}//end of Class

public class ListenerForRadioButton implements ActionListener{

    JPanel Panel_2;
    JButton browse = new JButton("Browse");

    //Constructor, will be used to get parameters from Parent methods
    public ListenerForRadioButton(JPanel Panel){
        Panel_2 = Panel;
    }

    //Overridden function, will be used for calling my 'core code' when user clicks on button.
    public void actionPerformed(ActionEvent event){
        Panel_2.add(browse);
        System.out.println("My listener is called");
    }//end of method
}//end of class

Problem Statement:

I have 2 JPanel components in a a given JFrame. Panel_1 is having a Option_1 JButton. When user clicks on that I am expecting my code to add a JButton 'browse' in Panel_2 at runtime.

Runtime Output:

System is not adding any JButton in Panel_2. However, I see my debug message in output, indicating that system was successful in identifying user's click action on 'option-1'.

Question:

Why is JPanel not adding any component at Runtime?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Krishan
  • 61
  • 1
  • 1
  • 4
  • *" add a Jbutton 'browse' in JPanel-2 on runtime."* 1) Use a [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html) as shown in [this answer](http://stackoverflow.com/a/5786005/418556). The button should be added at start-up to one of the cards, but a 2nd card with no components is seen by the user. An whatever condition the app. would add the button, change the cards to reveal the already added button. 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Jul 16 '16 at 04:09
  • 1
    .. 3) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. – Andrew Thompson Jul 16 '16 at 04:16
  • Thanks Andrew ! can you please let me know why by existing code wont work. I am passing reference of the Jpanel to my actionPerfored method and i should be allowed to add a Jbutton from there as well. – Krishan Jul 16 '16 at 04:24

2 Answers2

1
Panel_2.add(browse);
Panel_2.revalidate();

adding a 'revalidate' will solve the problem.

Krishan
  • 61
  • 1
  • 1
  • 4
0

There are some reasons. but:

  • usually it's because of using unsuitable LayoutManager.
  • sometimes it's because of adding the JPanel to it's root component in worng way. which any operation (add, remove,...) works but is not visible.
  • you must refresh the view when you make some changes on it, like adding or removing components to/from it.

Community
  • 1
  • 1
Rahmat Waisi
  • 1,293
  • 1
  • 15
  • 36