Unable to Dynamically Change panel

I have 2 panels. The first panel has a combo box. depending on the value of the item in the combobox selected, a panel below it must change. in the action listener of the combo box, when I try to change the panel, it does not change. Why is this?

cb1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String s = (String) cb1.getSelectedItem(); if (s.equals("Invoice")) { panel3Customizera(); g.gridy = 2; remove(panel3); add(panel3, g); } else { panel3Customizerb(); g.gridy = 2; add(panel3, g); } } });

panel3customizer's add elements into panel3.panel 3 is added to a jframe. The link to the entire code can be found here


Sounds like the bottom JPanel ought to be using CardLayout. Here's a tutorial on How to Use CardLayout.

And @Hovercraft Full Of Eels is right in his recommendation to invoke revalidate() and repaint() on the container object, since this will cause it to layout its components again and remove any visual "artifacts".


You need to call revalidate and repaint on the container that holds your panel3 object and that here uses GridBagLayout after adding or removing components. Note that revalidate is only for objects derived from JComponent such as JPanel.

<strong>Edit 1</strong> If you are adding directly to the JFrame, then you are adding in fact to its contentPane which is usually a JPanel. So an example of doing what I suggested would look something like this:

cb1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JPanel contentPane = (JPanel) getContentPane(); String s = (String) cb1.getSelectedItem(); if (s.equals("Invoice")) { panel3Customizera(); g.gridy = 2; remove(panel3); contentPane.add(panel3, g); } else { panel3Customizerb(); g.gridy = 2; contentPane.add(panel3, g); } contentPane.revalidate(); contentPane.repaint(); } });

but having said this, I have to put a big plug into mre's suggestion about using a CardLayout instead to swap views.


  • Each user can edit only one page-wordpress
  • Java - GUI: Passing values from frame to another
  • DataGrid returning null selectedItem
  • GridBagLayout fix space set by weight x/y
  • Select an element in jquery with two attribiutes
  • “append” text for JTextField
  • How to items from jcombobox to mysql table
  • L5 How to use trait that hashes id but keep pivot functionality
  • Java Swing dynamic JComboBox
  • How to find control on page by id
  • How to add custom fields to the order_invoice.tpl, OpenCart 2.0
  • Entity Framework - Saving child entities on update
  • Mutate value by using a value from a different row in a tibble
  • Original method still getting called in Moq even after CallBase = true/false
  • EntityDataSource query inner join
  • Unable to retrieve number before incoming call in marshmallow
  • Compare variables PHP
  • ValidationResult Returned From IValidatableObject.Validate Is Not Localized
  • Set cookie from Web Api 2 IAuthenticationFilter AuthenticateAsync method
  • Regex: Match everything except backreference
  • Get Users in Group from Azure AD via Microsoft Graph
  • Why == is different for Integer and String?
  • Where should I store a file in Android?
  • Dynamically generated lookup key for IQueryable
  • How to fail Phing without triggering backtrace
  • Linq Full Outer Join on Two Objects
  • How to get the index of element in the List in c#
  • JSF NumberFormatException with f:setPropertyActionListener
  • With Hadoop, can I create a tasktracker on a machine that isn't running a datanode?
  • Time complexity of a program which involves multiple variables
  • Regex thinks I'm nesting, but I'm not
  • What is the “return” in scheme?
  • How to disable jQuery.jplayer autoplay?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Angular 2 constructor injection vs direct access
  • Java static initializers and reflection
  • Android Google Maps API OnLocationChanged only called once
  • apache spark aggregate function using min value
  • Sorting a 2D array using the second column C++
  • UserPrincipal.Current returns apppool on IIS