Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6593051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:37:59+00:00 2026-05-25T17:37:59+00:00

Here’s my code. I have no problem when showing up the previously designed frames

  • 0

Here’s my code. I have no problem when showing up the previously designed frames (with panels added). I get this issue when adding panels to an empty JFrame dynamically.

package com.mytunes.controllers;

import com.mytunes.views.*;
import com.mytunes.views.panels.*;
import java.awt.BorderLayout;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.WindowConstants;


public class GUIController {
    ArrayList<JFrame> displayFrames = new ArrayList<JFrame>();

    JPanel displayPanel;
    HeaderPanel headerPanel = new HeaderPanel();
    FooterPanel footerPanel = new FooterPanel();


    public int showFrame(String frameName, Object controller){
        Class c;
        Constructor ctr;
        int lastFrame = -1;

        try {

           // call a class dynamically
           c = Class.forName("com.mytunes.views.frames." + frameName + "Frame");
           // calll a constructor of a class
           ctr = c.getConstructor(SessionController.class);
           // instantiate dynamically created class
           displayFrames.add((JFrame) ctr.newInstance(controller));

           // get the index of frame just added
           lastFrame = displayFrames.size()-1;

           displayFrames.get(lastFrame).setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
           displayFrames.get(lastFrame).pack();
           displayFrames.get(lastFrame).setLocationRelativeTo(null);

           // hide it by default. 
           displayFrames.get(displayFrames.size()-1).setVisible(false);

        } catch (InstantiationException ex) {
            Logger.getLogger(GUIController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(GUIController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(GUIController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(GUIController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchMethodException ex) {
            Logger.getLogger(GUIController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SecurityException ex) {
            Logger.getLogger(GUIController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            JOptionPane.showMessageDialog(null, "Error while loading class 'com.mytunes.view.frames." + frameName + "'" );
        }

        System.out.println(lastFrame);
        return lastFrame;

    }

    //-------------show panels --------------//
     public void showOperator(String frame, Object controller){

        // this works fine! this frame is already having Panels
        int f = showFrame(frame, controller);
        displayFrames.get(f).setVisible(true);
     }
    public void showEnterPIN(String frame, Object controller){


        int f = showFrame(frame, controller);
        displayFrames.get(f).getContentPane().add(new EnterPINPanel(), BorderLayout.CENTER);
        displayFrames.get(f).setVisible(true);

        //        Try every following ways to show the "Dynamically added" panels
        //        displayFrames.get(f).getContentPane().removeAll();
        //        displayFrames.get(f).getContentPane().invalidate();
        //        displayFrames.get(f).getContentPane().add(new EnterPINPanel(), BorderLayout.PAGE_START);
        //        displayFrames.get(f).getContentPane().validate();
        //        displayFrames.get(f).pack();
        //        displayFrames.get(f).setVisible(true);

    }

}

Appreciate if someone can spot this issue out for me. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T17:37:59+00:00Added an answer on May 25, 2026 at 5:37 pm

    If you add a component to a container you need to tell the container’s layout managers to layout all the components they contain by calling revalidate() and then by sometimes calling repaint(); on the container. So, something like…

    JPanel contentPane = (JPanel)displayFrames.get(f).getContentPane();
    contentPane.add(new EnterPINPanel(), BorderLayout.CENTER);
    contentPane.revalidate();
    contentPane.repaint();
    

    Having said this, your application design looks a bit different. Do you really use an ArrayList of JFrames? Most real-world applications don’t use a bunch of separate windows but rather swap displays in one main window, and you can do a similar thing with Swing by using just one JFrame and swap views via a CardLayout.

    Edit 2
    What if you try:

      int f = showFrame(frame, controller);
      JPanel contentPane = (JPanel)displayFrames.get(f).getContentPane();
      contentPane.removeAll();
      contentPane.setLayout(new BorderLayout()); // just to be sure
      contentPane.add(headerPanel, BorderLayout.PAGE_START);
      contentPane.add(new EnterPINPanel(), BorderLayout.CENTER);
      contentPane.add(footerPanel, BorderLayout.PAGE_END);
      contentPane.revalidate(); // *** note that it's **re**validate
      contentPane.repaint();
      displayFrames.get(f).setVisible(true);
    

    Again, if this doesn’t help, then you should create and post a “Short, Self Contained, Correct (Compilable), Example” or SSCCE

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's a problem I ran into recently. I have attributes strings of the form
Here is the issue I am having: I have a large query that needs
Here's a coding problem for those that like this kind of thing. Let's see
Here, i have coded to get data from DB. I want to store the
Here's the issue... if I use, say a directory catalog in MEF and have
Here I had a problem that I am adding contact from the address book
Here is what I am trying to achieve in PHP: I have this string:
here is my problem: I have a jList and a popup menu. When I
Here is my code: dialog = $(this).closest('.ui-dialog') dialog.animate({ left: document.width - dialog.width(), // or
Here's my scenario - I have an SSIS job that depends on another prior

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.