Page 4 of 4 FirstFirst ... 234
Results 61 to 77 of 77
  1. #61
    sharmapiyush is offline Member
    Join Date
    Apr 2014
    Posts
    74
    Rep Power
    0

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

    can you tell me,
    if i will click on the label1 button then the jframe displaying the button will be closed.

  2. #62
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

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

    Get a reference to the jframe and call its method.
    If you don't understand my response, don't ignore it, ask a question.

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

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

    you have told me to give reference to the main using refToMain

    Define a variable of type Main as a class instance variable:
    Main refToMain;
    In the constructor, assign that variable the value passed to the constructor:
    this.refToMain = refToMain; // save
    later in the code when you want to call a method in Main:
    refToMain.aMethodInMain(); // call a method in Main
    i have done the same thing but i am getting a null pointer exception when i am clicking on the label1 button.

  4. #64
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

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

    i am getting a null pointer exception
    What variable has the null value? Why doesn't it have a valid value?

    Without seeing the full text of the error message and the full code showing what happened, I can't say what is wrong in the code.
    If you don't understand my response, don't ignore it, ask a question.

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

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

    here is the error.

    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at New2.actionPerformed(New2.java:18)
    	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    	at java.awt.Component.processMouseEvent(Component.java:6505)
    	at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    	at java.awt.Component.processEvent(Component.java:6270)
    	at java.awt.Container.processEvent(Container.java:2229)
    	at java.awt.Component.dispatchEventImpl(Component.java:4861)
    	at java.awt.Container.dispatchEventImpl(Container.java:2287)
    	at java.awt.Component.dispatchEvent(Component.java:4687)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    	at java.awt.Container.dispatchEventImpl(Container.java:2273)
    	at java.awt.Window.dispatchEventImpl(Window.java:2719)
    	at java.awt.Component.dispatchEvent(Component.java:4687)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729)
    	at java.awt.EventQueue.access$200(EventQueue.java:103)
    	at java.awt.EventQueue$3.run(EventQueue.java:688)
    	at java.awt.EventQueue$3.run(EventQueue.java:686)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    	at java.awt.EventQueue$4.run(EventQueue.java:702)
    	at java.awt.EventQueue$4.run(EventQueue.java:700)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:699)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

  6. #66
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

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

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at New2.actionPerformed(New2.java:18)
    Look at line 18 and find the variable with the null value. Then backtrack in the code to see why that variable does NOT have a valid value.
    If you don't understand my response, don't ignore it, ask a question.

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

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

    that is the new coding for New2 form as you have told to me.
    line no 18 is calling a function from Main Class..

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class New2 extends JPanel implements ActionListener{
     
         private Main refToMain;
        public New2(Main refToMain)
        {
            this.refToMain=refToMain;
            JButton b1=new JButton("Label1");
            add(b1);
            b1.addActionListener(this);
        }
          public void actionPerformed(ActionEvent e) {
                 JPanel p1=new Label();
                 refToMain.display(p1);
         }
    }

  8. #68
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

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

    What is the contents of the refToMain argument passed to the New2 constructor?
    If you don't understand my response, don't ignore it, ask a question.

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

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

    see my post #24
    there is a slight difference in Main From coding on line no 34-36


    Java Code:
                public void actionPerformed(ActionEvent e) {
           
                    JPanel p1=new New2(null);
                    display(p1);

  10. #70
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

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

    The null value passed to the New2 constructor will cause a NullPointerException when used the the New2 class.
    The code should pass a reference to the Main class instance.
    If you don't understand my response, don't ignore it, ask a question.

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

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

    how to do that?

  12. #72
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

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

    Use the "this" keyword. From inside an inner class you need to prefix this with the classname: Main.this
    If you don't understand my response, don't ignore it, ask a question.

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

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

    thanks norm its perfectly working..............................
    thanks a lot...

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

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

    can you give me a good,working example of cardlayout.

  15. #75
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

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

    Glad you got it to work.

    See the tutorial: http://docs.oracle.com/javase/tutori...yout/card.html
    If you don't understand my response, don't ignore it, ask a question.

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

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

    can you tell me one more thing
    there is a jframe on which there is a panel1 and on that panel there is a panel2 that contains a button ,i want that when i will click on that button the frame will get hide. setvisible() method is not working.

  17. #77
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

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

    Please start a new thread for this question. Be sure to post a small program that shows the problem and wrap the code in code tags.
    If you don't understand my response, don't ignore it, ask a question.

Page 4 of 4 FirstFirst ... 234

Similar Threads

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