2

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 1

6

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.

2
  • Russ, thank you for the second set of eyes. I was truly laughing out loud when I read your response. I feel quite dumb, but that's the new mistakes one makes when programming in a new environment. Mar 19, 2011 at 19:25
  • 1
    Andrew, you'll want to "accept" Russ's answer if his post answers your original question. Mar 19, 2011 at 21:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.