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 4 of 4

Thread: How do I pass an object from a button in another gui back to the main gui?

  1. #1
    Junior Member
    Join Date
    Jul 2021
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How do I pass an object from a button in another gui back to the main gui?

    Hi all, I have just started learning JavaFX and I have some questions.

    Hi everyone, I have 2 GUIs, main and edit gui, in which I am trying to pass this Person object from the edit gui to the main gui.

    In my main gui, it has this Edit Person button, which calls for the edit gui as follows:
    public class EditGui 
    {
        private static Stage window;
        private static Person currentPers;
        private static Button saveBtn;
     
        public static void display(String msg, Person persObj)
        {
            currentPers = persObj;
     
            // Creation of the GUI elements
            saveBtn = new Button("Save");
            ...
            ...
     
            setEventHandler();
            Scene scene = new Scene(gridPane);
            window.setScene(scene);
            window.setResizable(false);
            window.showAndWait();
        }
     
        private static void setEventHandler()
        {
            saveBtn.setOnAction(evt -> saveMemberInfo());
        }
     
        private static Person saveMemberInfo()
        {
            Person tempNew = new Person(firstnameTxt.getText());
     
            if (tempNew.isEqual(currentPers))
            {
                System.out.println("No changes found. Nothing to save.");
                tempNew = currentPers;
            }
     
            return tempNew;
        }
    }

    In my main gui/ code, the code used is:
    Person pObj = EditGui.display(getSelectedTreeItemName(), getSelectedTreeItemPersonObj());

    I am attempting to return the Person object from Edit gui back to main code and that results an error since display() return type is void but I had thought that since the action stems from the save button, should the end product not comes from there?

    Even so, based on the above code,

    1. Is it a good idea to split up the event-handling? (See setEventHandler() in my edit gui) I split them up, thinking it will help facilitate my understanding and troubleshooting?
    2. How can I go about resolving the issue such that when Save button is clicked upon, the Person object will be passed back to the main gui?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,209
    Thanks
    53
    Thanked 2,627 Times in 2,577 Posts

    Default Re: How do I pass an object from a button in another gui back to the main gui?

    Too many static variables and methods. The first changes to the code needs to be to remove all uses of static except for the main method.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2021
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I pass an object from a button in another gui back to the main gui?

    Quote Originally Posted by Norm View Post
    Too many static variables and methods. The first changes to the code needs to be to remove all uses of static except for the main method.
    Hi, noted on that. I used static variables as I am following from some code that I saw online. While I can remove the use of 'static' , any insights towards my question 1 and 2?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,209
    Thanks
    53
    Thanked 2,627 Times in 2,577 Posts

    Default Re: How do I pass an object from a button in another gui back to the main gui?

    Sorry, I am not much good at design issues.

    when Save button is clicked upon, the Person object will be passed back to the main gui?
    The way to pass a reference to an instance of a class to an instance of another class is to have a reference to the receiving class and use that to call a method to pass the instance.
      refToOtherClass.methodInOtherClass(refToPass);  // pass refToPass to OtherClass
    refToPass would point to the Person object
    refToOtherClass would point to the main gui class
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. how to Pass the value of selected radio button from one jsp to another
    By Anonymous6 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 5th, 2018, 08:54 AM
  2. [SOLVED] GUI button not linking back to code
    By StaticHS in forum What's Wrong With My Code?
    Replies: 13
    Last Post: February 1st, 2014, 04:16 AM
  3. How to Pass Hex number as Argument to main
    By divix in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: April 2nd, 2012, 06:37 AM
  4. Control Browser Back Button
    By Ganezan in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: January 1st, 2010, 08:21 AM
  5. control Browser Back Button
    By Ganezan in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: December 30th, 2009, 07:49 AM

Tags for this Thread