Results 1 to 2 of 2
  1. #1
    kdub is offline Member
    Join Date
    Nov 2010
    Posts
    1
    Rep Power
    0

    Default Is there a better way to embed external (sub) JPanels in existing (parent) Jpanel?

    Hello,

    I am having a problem that may be due to my being new to Java. I am using Netbeans gui builder to create an interface and and I am using JComboBox filled with external JPanels (also created in NB gui builder) in the same package.

    It works. I can select a JPanel from the comboBox and press "select" to get the JPanel to show up in the subPanel placeholder. (see attached image)

    The problem is that any button I place inside the subPanel that call methods in the parent Jpanel are not working properly (i.e. only static methods are working). Is there a better way to embed external (sub) JPanels in an existing (parent) Jpanel?

    thanks!

    Is there a better way to embed external (sub) JPanels in existing (parent) Jpanel?-subpanel.jpg



    Java Code:
    
     // The goal of this part of the project is to be able to access several different jPanels
     // from a comboBox in a GUI created in Netbeans GUI builder in order to be dynamically 
     // add|remove JPanels as needed.
    
    
    
      private void createSubPanels() {
        
            // This method fills up a comboBox with an ArrayList of JPanels
            // using the JPanel's name. This method is called during project init.
    
    
            // First, add subPanels to an ArrayList of panels (code is added here for clarity)
                ArrayList<JPanel> panelList = new ArrayList<JPanel>();
                panelList.add(new subPanel1());
                panelList.add(new subPanel2());
                panelList.add(new subPanel3());
    
            // Populate ComboBox with subPanel names for later selection
                for (int i = 0; i < panelList.size(); i++) {
                    subPanelComboBox.addItem(panelList.get(i).getName());
                }
                subPanelComboBox.setSelectedIndex(0);
        }
    
    
        private void subJpanelSelectionButtonActionPerformed(java.awt.event.ActionEvent evt) {        
                                                    
            // This is the method called when the "select" button pressed after a subPanel
            // is selected in the subPanelComboBox. The previous subPanel is removed and the
            // selected JPanel is added to subPanel place holder.
    
    
                if (subPanelComboBox.getSelectedIndex() < panelList.size()) {
    
                    subPanel.removeAll();
                    subPanel.setLayout(new BoxLayout(objectivesTypePanel, BoxLayout.Y_AXIS));
                    subPanel.add(panelList.get(ObjectiveTypeComboBox.getSelectedIndex()));
                    subPanel.setVisible(true);
                    subPanel.revalidate();
                    subPanel.repaint();
                }
        }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by kdub View Post
    I am having a problem that may be due to my being new to Java. I am using Netbeans gui builder to create an interface and and I am using JComboBox filled with external JPanels (also created in NB gui builder) in the same package.

    It works. I can select a JPanel from the comboBox and press "select" to get the JPanel to show up in the subPanel placeholder. (see attached image)

    The problem is that any button I place inside the subPanel that call methods in the parent Jpanel are not working properly (i.e. only static methods are working). Is there a better way to embed external (sub) JPanels in an existing (parent) Jpanel?
    You may have a problem with references, of not having a proper reference of one object in another object. I suspect this since you state that your problem is with calling methods of one class form inside another class. Are you sure that the reference you have to the parent JPanel that is held in the child JPanel is in fact the parent JPanel that is being displayed? Did you pass this reference into the sub JPanel via a method or constructor parameter?

    But really I'm just guessing since it's impossible to tell what the actual problem is based on the small code snippets you've given us.

    Consider creating and posting an SSCCE -- a non-NetBeans produced small program that replicates your problem, one we can compile and run and test ourselves. Also, you should probably use a CardLayout for swapping JPanels as you wish to do.

    Luck!

    Best of luck!

Similar Threads

  1. Problems regarding JPanels in JPanels
    By ColtonPhillips in forum AWT / Swing
    Replies: 2
    Last Post: 07-19-2010, 08:33 PM
  2. JList and JPanels
    By JetsYanks in forum New To Java
    Replies: 8
    Last Post: 12-25-2009, 02:11 PM
  3. Populating CardLayout JPanels with External classes
    By fred2482 in forum New To Java
    Replies: 5
    Last Post: 12-21-2009, 06:08 AM
  4. HELP with jPanels
    By maverik_vz in forum AWT / Swing
    Replies: 1
    Last Post: 03-12-2009, 11:46 AM
  5. Help with overlapping JPanels
    By xcallmejudasx in forum New To Java
    Replies: 2
    Last Post: 03-09-2009, 10:19 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •