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 4078286
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:39:19+00:00 2026-05-20T17:39:19+00:00

I’m having problems getting my JPanel to display properly. I want to use different

  • 0

I’m having problems getting my JPanel to display properly. I want to use different extended JPanels to display what I want the user to do with this program (which is ultimately to display photographs). Below is the code for the only two classes that exist at this point. Unfortunately, I’m having problems just getting this to work right out of the gate with the first panel which was to present the user with the ability to select different graphic images.

What’s happening is, I can’t get my JPanel to display until I click the “Open” menu item in the File menu. Once that JOptionPane shows, so does my JPanel (NewAlbum).

class PhotoGallery {
    static JPanel transientPanel = null;
    static final JFrame mainFrame = new JFrame("Photo Gallery");

    public static void main(String[] args) {
        JMenu fileMenu = new JMenu("File");
        fileMenu.setMnemonic(KeyEvent.VK_F);

        JMenuItem open = new JMenuItem("Open");
        open.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(mainFrame, "Hello World");
            }
        });
        fileMenu.add(open);

        JMenuItem newAlbum = new JMenuItem("New Album");
        open.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                AssignToTransientPanel((JPanel) new NewAlbum());
                Container content = mainFrame.getContentPane();
                content.removeAll();
                content.add(transientPanel);
                content.validate();
                content.repaint();
            }
       });
       fileMenu.add(newAlbum);

       JMenuItem exit = new JMenuItem("Exit");
       exit.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               System.exit(0);
           }
       });
       fileMenu.add(exit);

       JMenuBar pgMenu = new JMenuBar();
       pgMenu.add(fileMenu);
       mainFrame.setJMenuBar(pgMenu);
       mainFrame.setSize(640, 480);
       mainFrame.setLocation(20, 45);

       mainFrame.validate();
       mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       mainFrame.setVisible(true);
   }

    public static void AssignToTransientPanel(JPanel jp) {
        if(transientPanel != null)
            mainFrame.remove(transientPanel);
            transientPanel = jp;
        }
    }
}

class NewAlbum extends JPanel {
    JButton selectImages = new JButton("Select Images");
    JFileChooser jfc;
    File[] selectedFiles;

    public NewAlbum() {
        selectImages.setLocation(25, 25);
        add(selectImages);

        selectImages.addActionListener(new ActionListener() {
             public void actionPerformed(java.awt.event.ActionEvent ae) {
             jfc = new JFileChooser();
             jfc.setMultiSelectionEnabled(true);
             jfc.showOpenDialog(getParent());
             selectedFiles = jfc.getSelectedFiles();
         }
      });

      this.validate();
   }

   public int getHeight() {
       return getParent().getSize().height - 20;
   }

   public int getWidth() {
       return getParent().getSize().width - 20;
   }

   public Dimension getPreferredSize() {
       return new Dimension(this.getWidth(), this.getHeight());
   }

}

  • 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-20T17:39:20+00:00Added an answer on May 20, 2026 at 5:39 pm

    You have not added any components to the mainFrame’s content pane in the main method. The only time a panel gets added is in this ActionListener:

        open.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                AssignToTransientPanel((JPanel) new NewAlbum());
                Container content = mainFrame.getContentPane();
                content.removeAll();
                content.add(transientPanel);
                content.validate();
                content.repaint();
            }
       });
    

    This is only getting called when “Open” is clicked as you have, I assume accidentally, added the ActionListener to the open JMenuItem rather than the newAlbum JMenuItem. To add content on startup you need to add something like this before the mainFrame.setVisible(true) line:

    mainFrame.add(new NewAlbum());
    

    BTW, the convention is for all methods in Java source code to start with a lower case letter. assignToTransientPanel would be a better name for your method.

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

Sidebar

Related Questions

No related questions found

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.