Page 1 of 4 123 ... LastLast
Results 1 to 20 of 77
  1. #1
    sharmapiyush is offline Member
    Join Date
    Apr 2014
    Posts
    74
    Rep Power
    0

    Default can't navigate from child panel to main panel

    i have created two(displaypanel & buttonpanel) main panels in a JFrame and many child panels,one of the panel is for holding buttons and displaypanel mainly swap child panel as directed from buttonpanel, but the problem arises i cannot navigate from child panel to another child panel,
    as i have made a button on one of a child panel and from button panel i add the childpanel to displaypanel.it is working
    but when i tried to navigate from the button which is on child panel nothing happened.
    "i have made a function in main form which swap the content of mainpanel (displaypanel) and in childpanel i have acces the function through object of mainform"

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,021
    Rep Power
    6

    Default Re: can't navigate from child panel to main panel

    What do you mean by "can't navigate?" What are you trying to do exactly? Best to submit an Short, Self Contained, Correct Example to demonstrate the problem.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: can't navigate from child panel to main panel

    i cannot navigate from child panel to another child panel,
    What does it mean to "navigate" between panels?
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    sharmapiyush is offline Member
    Join Date
    Apr 2014
    Posts
    74
    Rep Power
    0

    Default Re: can't navigate from child panel to main panel

    i mean to say i have many child jpanels in a package and an another package in which contains a jframe or mainform in which display and button jpanel is present.
    and i have a button on one of my child Jpanel and that child panel is added to displayjpanel.
    now i want that if i will click on that button which is on childjpanel then a new child jpanel will be added to display panel.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: can't navigate from child panel to main panel

    a new child jpanel will be added to display panel.
    To call the add() method you need a reference to the class that is the container.
    Use the new statement to create a new panel.

    Can you submit an Short, Self Contained, Correct Example to demonstrate the problem?
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    sharmapiyush is offline Member
    Join Date
    Apr 2014
    Posts
    74
    Rep Power
    0

    Default Re: can't navigate from child panel to main panel

    i have tried that

    MainForm m= new MainForm();
    JPanel p1=new New();
    m.display(p1);

    i have made an instance of mainform and there is a display function in mainform,i have called the function here but nothing happens.

  7. #7
    sharmapiyush is offline Member
    Join Date
    Apr 2014
    Posts
    74
    Rep Power
    0

    Default Re: can't navigate from child panel to main panel

    Display function

    public void display(JPanel panel)
    {
    displaypanel.removeAll();
    displaypanel.add(panel);
    getContentPane().add(displaypanel);
    displaypanel.updateUI();
    }

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: can't navigate from child panel to main panel

    You need to post a small, COMPLETE program that compiles, executes and shows the problem.
    Be sure to wrap the code in code tags.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    sharmapiyush is offline Member
    Join Date
    Apr 2014
    Posts
    74
    Rep Power
    0

    Default Re: can't navigate from child panel to main panel

    Main Form

    Java Code:
    package mainaction;
    import javax.swing.JPanel;
    import tool.*;
    public class Main extends javax.swing.JFrame {
        public Main() {
            setBounds(100, 100, 600, 600);
            initComponents();
        }
    //Component Declaration
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
    
            buttonpanel = new javax.swing.JPanel();
            jButton1 = new javax.swing.JButton();
            jButton2 = new javax.swing.JButton();
            displaypanel = new javax.swing.JPanel();
            jMenuBar1 = new javax.swing.JMenuBar();
            jMenu1 = new javax.swing.JMenu();
            jMenu2 = new javax.swing.JMenu();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            getContentPane().setLayout(null);
    
            buttonpanel.setLayout(null);
    
            jButton1.setText("Label");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
            buttonpanel.add(jButton1);
            jButton1.setBounds(18, 98, 80, 23);
    
            jButton2.setText("New");
            jButton2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton2ActionPerformed(evt);
                }
            });
            buttonpanel.add(jButton2);
            jButton2.setBounds(14, 244, 80, 23);
    
            getContentPane().add(buttonpanel);
            buttonpanel.setBounds(0, 0, 130, 450);
            getContentPane().add(displaypanel);
            displaypanel.setBounds(130, 0, 610, 450);
    
            jMenu1.setText("File");
            jMenuBar1.add(jMenu1);
    
            jMenu2.setText("Edit");
            jMenuBar1.add(jMenu2);
    
            setJMenuBar(jMenuBar1);
    
            pack();
        }// </editor-fold>                        
    
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            JPanel p1=new Label();
            display(p1);
        }                                        
    
        private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            JPanel p1=new New();
            display(p1);
        }                                        
    
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            /* Set the Nimbus look and feel */
            //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
            /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
             * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
             */
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>
    
            /* Create and display the form */
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new Main().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify                     
        private javax.swing.JPanel buttonpanel;
        private javax.swing.JPanel displaypanel;
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JMenu jMenu1;
        private javax.swing.JMenu jMenu2;
        private javax.swing.JMenuBar jMenuBar1;
        // End of variables declaration   
    // display function to add child panels to main                
    public void display(JPanel panel)
    {
         displaypanel.removeAll();
         displaypanel.add(panel);
         getContentPane().add(displaypanel);
         displaypanel.updateUI();
    }
    }

  10. #10
    sharmapiyush is offline Member
    Join Date
    Apr 2014
    Posts
    74
    Rep Power
    0

    Default Re: can't navigate from child panel to main panel

    Label Panel

    Java Code:
    package tool;
    public class Label extends javax.swing.JPanel {
        public Label() {
        initComponents();
        }
    
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
    
            jTextField1 = new javax.swing.JTextField();
            jScrollPane1 = new javax.swing.JScrollPane();
            jTextArea1 = new javax.swing.JTextArea();
    
            jTextField1.setText("jTextField1");
    
            jTextArea1.setColumns(20);
            jTextArea1.setRows(5);
            jScrollPane1.setViewportView(jTextArea1);
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
            this.setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 410, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(244, 244, 244))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addGap(106, 106, 106)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap(37, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(97, 97, 97)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(49, 49, 49)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 232, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(53, Short.MAX_VALUE))
            );
        }// </editor-fold>                        
        // Variables declaration - do not modify                     
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JTextArea jTextArea1;
        private javax.swing.JTextField jTextField1;
        // End of variables declaration                   
    }

  11. #11
    sharmapiyush is offline Member
    Join Date
    Apr 2014
    Posts
    74
    Rep Power
    0

    Default Re: can't navigate from child panel to main panel

    New Panel

    Java Code:
    package tool;
    import javax.swing.JPanel;
    import mainaction.*;
    public class New extends javax.swing.JPanel {
    
        public New() {
            initComponents();
        }
    
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
    
            jButton1 = new javax.swing.JButton();
            jLabel1 = new javax.swing.JLabel();
            jScrollPane1 = new javax.swing.JScrollPane();
            jList1 = new javax.swing.JList();
    
            jButton1.setText("Label");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
    
            jLabel1.setText("jLabel1");
    
            jList1.setModel(new javax.swing.AbstractListModel() {
                String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
                public int getSize() { return strings.length; }
                public Object getElementAt(int i) { return strings[i]; }
            });
            jScrollPane1.setViewportView(jList1);
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
            this.setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(107, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(99, 99, 99)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(jLabel1))
                    .addGap(261, 261, 261))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(97, 97, 97)
                    .addComponent(jLabel1)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(36, 36, 36)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(69, 69, 69)
                            .addComponent(jButton1)))
                    .addContainerGap(141, Short.MAX_VALUE))
            );
        }// </editor-fold>                        
    // code to display label panel on main JFrame when clicked on Button Label
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            JPanel p1=new Label();
            Main o=new Main();
            o.display(p1);
        }                                        
    
        // Variables declaration - do not modify                     
        private javax.swing.JButton jButton1;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JList jList1;
        private javax.swing.JScrollPane jScrollPane1;
        // End of variables declaration                   
    }

  12. #12
    sharmapiyush is offline Member
    Join Date
    Apr 2014
    Posts
    74
    Rep Power
    0

    Default Re: can't navigate from child panel to main panel

    i want that when i will click on Label button which is on Label Panel then New Panel will e added to displaypanel in Main Jframe.

  13. #13
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,021
    Rep Power
    6

    Default Re: can't navigate from child panel to main panel

    A small example. Not all of that code. Keep in under 150 lines total and make certain it can compile. And I recommend you stay away visual design aids. They overly complicate your code. Use layout managers instead.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: can't navigate from child panel to main panel

    You need to get rid of the generated GUI if you want others to look at the code and make suggestions on how to change it. Often it is not possible to change generated GUI.
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    sharmapiyush is offline Member
    Join Date
    Apr 2014
    Posts
    74
    Rep Power
    0

    Default Re: can't navigate from child panel to main panel

    Main code

    Java Code:
    package mainaction;
    
    
    import javax.swing.JPanel;
    import tool.*;
    public class Main extends javax.swing.JFrame {
       private javax.swing.JPanel buttonpanel;
        private javax.swing.JPanel displaypanel;
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JMenu jMenu1;
        private javax.swing.JMenu jMenu2;
        private javax.swing.JMenuBar jMenuBar1;
    
        public Main() {
            setBounds(100, 100, 600, 600);
            initComponents();
        }
                   
        private void initComponents() {
    
            buttonpanel = new javax.swing.JPanel();
            jButton1 = new javax.swing.JButton();
            jButton2 = new javax.swing.JButton();
            displaypanel = new javax.swing.JPanel();
            jMenuBar1 = new javax.swing.JMenuBar();
            jMenu1 = new javax.swing.JMenu();
            jMenu2 = new javax.swing.JMenu();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
           
            jButton1.setText("Label");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
            buttonpanel.add(jButton1);
    
            jButton2.setText("New");
            jButton2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton2ActionPerformed(evt);
                }
            });
            buttonpanel.add(jButton2);
            
            getContentPane().add(buttonpanel);
            buttonpanel.setBounds(0, 0, 130, 450);
            getContentPane().add(displaypanel);
            displaypanel.setBounds(130, 0, 610, 450);
    
            jMenu1.setText("File");
            jMenuBar1.add(jMenu1);
    
            jMenu2.setText("Edit");
            jMenuBar1.add(jMenu2);
    
            setJMenuBar(jMenuBar1);
    
            pack();
        }                       
    
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            JPanel p1=new Label();
            display(p1);
        }                                        
    
        private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            JPanel p1=new New();
            display(p1);
        }                                        
    
    
        public static void main(String args[]) {
            new Main().setVisible(true);
                
        }
                       
                       
    public void display(JPanel panel)
    {
         displaypanel.removeAll();
         displaypanel.add(panel);
         getContentPane().add(displaypanel);
         displaypanel.updateUI();
    }
    }

  16. #16
    sharmapiyush is offline Member
    Join Date
    Apr 2014
    Posts
    74
    Rep Power
    0

    Default Re: can't navigate from child panel to main panel

    Label Form .
    Java Code:
    package tool;
    public class Label extends javax.swing.JPanel {
        public Label() {
            initComponents();
        }
                          
        private void initComponents() {
    
            jTextField1 = new javax.swing.JTextField();
            jTextArea1 = new javax.swing.JTextArea();
          
           add(JTextarea1};
           add(JTextField1);
    
        }                   
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JTextArea jTextArea1;
        private javax.swing.JTextField jTextField1;
                     
    }

  17. #17
    sharmapiyush is offline Member
    Join Date
    Apr 2014
    Posts
    74
    Rep Power
    0

    Default Re: can't navigate from child panel to main panel

    New From
    Java Code:
    package tool;
    
    import javax.swing.JPanel;
    import mainaction.*;
    public class New extends javax.swing.JPanel {
        public New() {
            initComponents();
        }
                            
        private void initComponents() {
    
            jButton1 = new javax.swing.JButton();
            jLabel1 = new javax.swing.JLabel();
            add(JButton1)
            add(JLabel1);
            jButton1.setText("Label");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
    
            jLabel1.setText("jLabel1");
    
              }                      
    
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            JPanel p1=new Label();
            Main o=new Main();
            o.display(p1);
        }                                        
    
                       
        private javax.swing.JButton jButton1;
        private javax.swing.JLabel jLabel1;
                    
    }

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: can't navigate from child panel to main panel

    Can you explain what problem you are having with the posted code?
    Be specific about what method in what class is causing the problem.

    BTW Label is the name of a Java SE class. Its less confusing if the code uses a unique name, not an existing name for a class.
    If you don't understand my response, don't ignore it, ask a question.

  19. #19
    sharmapiyush is offline Member
    Join Date
    Apr 2014
    Posts
    74
    Rep Power
    0

    Default Re: can't navigate from child panel to main panel

    Label And New Are the two different Jpanel class and both are in package named tool package.
    the main class is JFrame in which two panels that are displaypanel and button panel and the Jframe is in a different package named Mainaction package.
    have you understood what i am saying.

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: can't navigate from child panel to main panel

    Ok, what is the problem?

    BTW The posted code has syntax errors and does not compile. How did you test this code if it doesn't compile?
    If you don't understand my response, don't ignore it, ask a question.

Page 1 of 4 123 ... LastLast

Similar Threads

  1. Replies: 7
    Last Post: 04-03-2011, 01:35 PM
  2. panel on a panel not visible
    By warchieflll in forum Advanced Java
    Replies: 2
    Last Post: 01-29-2011, 09:29 PM
  3. Displaying panels inside a main panel
    By Sneaky Fox in forum AWT / Swing
    Replies: 4
    Last Post: 01-21-2011, 05:12 PM
  4. Add panel to parent panel
    By LovJava in forum AWT / Swing
    Replies: 10
    Last Post: 09-02-2010, 09:43 PM
  5. How to add more components to main panel
    By aneesahamedaa in forum AWT / Swing
    Replies: 4
    Last Post: 08-21-2008, 07:31 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
  •