Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Reg:extending a jpanel

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Location
    trichy
    Posts
    28
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Reg:extending a jpanel

    Hi all,

    I m working with small java application called personal expenses database.
    in my application i have a one jframe and two panels called login_panel and reg_panel.
    login_panel is in inside the jframe.
    reg_panel is a separate class that extends jpanel.

    if click a create new account button,login_panel will be removed and reg_panel will be added.
    what my question is, how can i get back to the original jframe after click the back button on reg_panel.

    i have tried lot of things (ie. sending the contentpane reference to reg_panel object)
    but i seem not working.

    anyone pls help me.thanks in advance.


  2. #2
    Member
    Join Date
    Feb 2013
    Posts
    45
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: Reg:extending a jpanel

    Can you post your code what you have tried so far?
    that will be easy to help you...
    Regards
    Android developer
    Trinay Technology Solutions
    http://www.trinaytech.com
    5705750475

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Location
    trichy
    Posts
    28
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Reg:extending a jpanel

    this is my main frame
    public class login extends JFrame {

    //some code

    public void run(){

    GUI related code goes here
    }
    public void actionPerformed(ActionEvent e){

    if(e.getSource()==reg){
    Registration r=new Registration()
    r.setVisible(true);
    SwingUtilities.invokeLater(r)
    }
    }

    another panel
    public class Registration extends JPanel

    {


    some code

    }
    after click a register button the Registration panel will be added.
    then how i go back to login panel?

    Thank you

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Reg:extending a jpanel

    Can you make a small, complete program that compiles, executes and shows the problem?
    Be sure to wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Reg:extending a jpanel

    If you look at the JavaDocs for the remove function you will see that when a component is removed it must be validated. After the validation you should do a repaint. Another way of doing it is just adding both panels and controlling which one you see using the setVisible() function.

    	/**
    	 * Changes JFrame from regularPanel to extendedPanel
    	 */
    	private void loadExtendedPanel(){
    		this.remove(regularPanel);
    		this.add(extendedPanel);
    		this.validate();
    		this.repaint();
    	}
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  6. #6
    Junior Member
    Join Date
    Mar 2013
    Location
    trichy
    Posts
    28
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Reg:extending a jpanel

    public class login extends JFrame {

    //some code

    public void run(){

    GUI related code goes here

    login_panel=new JPanel();
    getContentPane().add(loign_panel);
    }
    public void actionPerformed(ActionEvent e){

    if(e.getSource()==reg){
    Registration r=new Registration()
    r.setVisible(true);
    SwingUtilities.invokeLater(r)
    }
    }

    another panel
    public class Registration extends JPanel

    {


    login_panel=new JPanel();//this is the place compliler says null pointer exception
    login_panel.setVisible(false);
    login_panel.setVisible(true);

    }

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Reg:extending a jpanel

    login_panel=new JPanel();//this is the place compliler says null pointer exception
    I don't see how that assignment statement can cause the error. Check the error's location again.

    Please copy the full text of the error message and paste it here.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Mar 2013
    Location
    trichy
    Posts
    28
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Reg:extending a jpanel

    because Registration is a separate class that extending JPanel. not inside in main frame.
    main frame only has login panel.login panel have register button to navigate Registration panel.

    And Registration panel has a back button.after i click the back button i want to go back to login panel.
    how to do that?

  9. #9
    Junior Member
    Join Date
    Mar 2013
    Location
    trichy
    Posts
    28
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Reg:extending a jpanel

    Quote Originally Posted by mohamed_mashood View Post
    because Registration is a separate class that extending JPanel. not inside in main frame.
    main frame only has login panel.login panel have register button to navigate Registration panel.

    And Registration panel has a back button.after i click the back button i want to go back to login panel.
    how to do that?
    pls help me.i need a solution immediately.
    thank you

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Reg:extending a jpanel

    Please copy the full text of the error message and paste it here.

    Can you make a small, complete program that compiles, executes and shows the problem?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Extending class
    By TP-Oreilly in forum Java Theory & Questions
    Replies: 8
    Last Post: January 10th, 2012, 05:06 PM
  2. [SOLVED] Pesky <JPanel>.getWidth() and <JPanel>.getHeight() Methods...
    By snowguy13 in forum AWT / Java Swing
    Replies: 1
    Last Post: December 31st, 2011, 03:35 PM
  3. Reg: Collections
    By vddmanikanta in forum Collections and Generics
    Replies: 1
    Last Post: July 17th, 2011, 09:33 AM
  4. class extending
    By Reem in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 18th, 2010, 01:49 AM
  5. reg how to use log in jsp page
    By javaking in forum JavaServer Pages: JSP & JSTL
    Replies: 7
    Last Post: April 9th, 2010, 12:36 AM