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.

Page 4 of 4 FirstFirst ... 234
Results 76 to 93 of 93

Thread: issue sending value to new pane

  1. #76
    Member
    Join Date
    Feb 2018
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: issue sending value to new pane

    Im trying to send the value of lblamnt or tvar that is in index.java and pass that variable to index2 so i can perform calculations and such.

  2. #77
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: issue sending value to new pane

    Im trying to send the value of lblamnt or tvar that is in index.java and pass that variable to index2
    I assume you mean Index not index and Index2 not index2. Case is important

    For code in the Index class to pass/send the value in a variable to another class, the other class needs to have a setter method that the sending class can call and also the sending class needs a reference to the target class to be able to call the setter method.
    Does the code in Index have a reference to an instance of Index2?
    Does Index2 have a setter method?

    Then it could do this in Index:
      refToIndex2.setTheVar(theVar);   //  send value in theVar to Index2 set method
    If you don't understand my answer, don't ignore it, ask a question.

  3. #78
    Member
    Join Date
    Feb 2018
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: issue sending value to new pane

    i get what you are saying but im having a hard time visualizing it.

    the index has a reference to Index.

    Index2 index2;
    public void setIndex2(Index2 index2){
            this.index2 = index2;
        }

    and in Index2 i got this...setter is unclear what i should have.

    Index index;

    I dont know how i would set setter up. it feels like this is inception.

  4. #79
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: issue sending value to new pane

    how i would set setter up
    You just posted an example of a setter:
    public void setIndex2(Index2 index2){
            this.index2 = index2;
        }
    That code would be in a class that you wanted to have a reference to an instance of Index2. The setter saved the passed value in a local class level variable so other methods in the class the setter is in can use it to access methods in Index2.
       index2.someMethodInIndex2();  // call a method in Index2
    If you don't understand my answer, don't ignore it, ask a question.

  5. #80
    Member
    Join Date
    Feb 2018
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: issue sending value to new pane

    the setindex2 is placed in index. was this suppose to be placed in index2?

    and the index2.setter() is placed in index?

    Im really confused, because it feels like those 2 classes is just slinging shit back and forth.

  6. #81
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: issue sending value to new pane

    You said:
    trying to send the value of lblamnt or tvar that is in index.java and pass that variable to index2
    That says the code in Index needs a way to send/pass a value to the Index2 class.
    I described a way to do that in post#77.

    For code in the Index class to send/pass data to the Index2 class,
    1) the Index class needs a reference to an instance of Index2
    2)the Index2 class needs to have a setter method that can be called to receive the value that is being sent/passed

    the setindex2 is placed in Index
    That would solve 1) above - make sure Index has a reference to Index2

    index2.setter() is placed in Index?
    Yes that would be how the code in Index would send data to Index2 - by calling the Index2's setter method
    If you don't understand my answer, don't ignore it, ask a question.

  7. #82
    Member
    Join Date
    Feb 2018
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: issue sending value to new pane

    im just not getting this at all. Im staring at your response with confusion. Can you clearly outline what is suppose to be in Index and Index2 and post the corresponding code? Im very confused what goes where.

  8. #83
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: issue sending value to new pane

    Before writing any code you need to specify the steps that the user will take and what the code will do in response.
    Right now you say:
    send the value of lblamnt or tvar that is in index.java and pass that variable to index2
    I've alread discussed what to do if there is a value in Class1 and you want to pass it to Class2.
      public class Class1 {
        int theVarToPass = someValue;
       ...
        Class2 refToClass2;
     
        public void saveRef(Class2 refClass2) {  // This could be called by Class3
          refToClass2 = refClass2;
        }
        ...
        // pass theVarToPass to Class2
       refToClass2.setTheVar(theVarToPass);
       ...
     } // end Class1
     
     
    public class Class2 {
       int someVar;
       ...
      public setTheVar(int theVar) {
         someVar = theVar;  // save passed value
      }
    } // end Class2
     
    public class Class3 {
       Class2 cls2 = new Class2();
       Class1 cls1 = new Class1();
       ...
       cls1.saveRef(cls2); //  pass reference to Class2 to Class1
    } // end Class3
    If you don't understand my answer, don't ignore it, ask a question.

  9. #84
    Member
    Join Date
    Feb 2018
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: issue sending value to new pane

    Index.java

    import javax.swing.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    public class Index {
        private JPanel panel;
        private JLabel lblamnt;
        private JButton button1;
        private JButton btn1;
        private JButton btn2;
        private JButton btn3;
        private JButton btn4;
        private JButton btn5;
        private JButton btn0;
        private JButton btnD;
        private JLabel lblDisplay;
        private MainPage parentForm;
        String tval;
        Index2 index2;
     
        public Index() {
            button1.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    parentForm.showPanel(MainPage.INDEX2);
                }
            });
            btn0.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    tval = tval + 0;
                    lblamnt.setText(tval);
                }
            });
            btn1.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    tval = tval + 1;
                    lblamnt.setText(tval);
                }
            });
            btn2.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    tval = tval + 2;
                    lblamnt.setText(tval);
                }
            });
            btn3.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    tval = tval + 3;
                    lblamnt.setText(tval);
                }
            });
            btn4.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    tval = tval + 4;
                    lblamnt.setText(tval);
                }
            });
            btn5.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    tval = tval + 5;
                    lblamnt.setText(tval);
     
                }
            });
            btnD.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    lblDisplay.setText(tval);
                }
            });
        }
        public void saveRef(Index2 ind2){
            index2 = ind2;
        }
        index2.setter(tval);
        public void setParentForm(final MainPage parentForm) {
            this.parentForm = parentForm;
        }
        public JPanel getPanel() {
            return panel;
        }
     
    }

    Index2.java


    import javax.swing.*;
     
    public class Index2 {
        private JPanel panel;
        private JLabel lblgamnt;
        private MainPage parentForm;
        Index index;
     
        public Index2(Index index) {
            this.index = index;
            //lblgamnt.setText(setter());
        }
        String thevar;
        public void setter(String tgval){
            thevar = tgval;
        }
     
        public void setParentForm(final MainPage parentForm) {
            this.parentForm = parentForm;
        }
     
        public JPanel getPanel() {
            return panel;
        }
     
    }

    Still having trouble.

    Index.java
    index2.setter(tval);

    Unknown class 'tval'

  10. #85
    Junior Member
    Join Date
    Feb 2018
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: issue sending value to new pane

    Index2(Index) in Index2 cannot be applied
    This means that it expects you to send an Index instance to the constructor

    Try changing that line to send an Index.

    one = new Index2(zero);

    Index2.java

    Index Index;

    This is wrong

    It should be.

    Index index;

    And you missed step 5 im my previous post

    Add:

    Index2 index2;

    to index.java



    Your general concept of defining and passing variables to methods isn't solid at all. Maybe read up on some tutorials about that.

    https://docs.oracle.com/javase/tutor...arguments.html
    https://www.youtube.com/watch?v=XEgpAnTmWNU

  11. #86
    Member
    Join Date
    Feb 2018
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: issue sending value to new pane

    so i started from scratch and passed variables. I got it down pat.

    public class MainForm {
        String varpass = "This is a string that has to be passed.";
        public String t1(){
            String text = "This is a non-static method being called.";
            return text;
        }
        public static String t2(){
            String text = "This is a static method being called.";
            return text;
        }
        public void t3(){
            System.out.println("This is a non-static void method and cannot return.");
        }
        public static void t4(){
            System.out.println("This is a static void method and cannot return.");
        }
        public void place1 (){
            //=======================================Method calls from another class========================================
            //Calls from another class. It is non-static and thus requires it to be instantiated. EG. class var = new class();
            Methods call = new Methods();
            System.out.println(call.t1());
     
            //Calls from another class. It is non-static void and thus requires it to be instantiated and be called straight.
            call.t3();
     
            //Calls from another class. It is static and thus does not require it to be instantiated. EG. class var = new class();
            System.out.println(Methods.t2());
     
            //Calls from another class. It is static void and thus does not require it to be instantiated.
            Methods.t4();
     
            //Trying to call a variable that was sent.
            call.display();
            call.getvar(varpass);
            call.display();
            System.out.println("Sending..."+call.getVar);
     
            //=======================================Method calls from current class========================================
            MainForm mcall = new MainForm();
            //Calls from within the same class. It is static and thus does not require it to be instantiated. EG. class var = new class();
            System.out.println(mcall.t1());
            mcall.t3();
            System.out.println(t2());
            t4();
        }
        public static void main(String[] args) {
            MainForm place = new MainForm();
            place.place1();
        }
    }
    public class Methods {
        String getVar = "Initial";
        public String t1(){
            String text = "This is a non-static method being called.";
            return text;
        }
        public static String t2(){
            String text = "This is a static method being called.";
            return text;
        }
        public void t3(){
            System.out.println("This is a non-static void method and cannot return.");
        }
        public static void t4(){
            System.out.println("This is a static void method and cannot return.");
        }
        public void display(){
            System.out.println("New Var is "+getVar);
        }
        public void getvar(String varsent){
            String msg = "getver() Variables are varsent("+varsent+"), getVar("+getVar+"), getVar(";
            getVar = varsent;
            msg = msg + getVar+")";
            System.out.println(msg);
        }
    }

    But when i put the same methods in my index/index2 classes it still doesnt spit it out.

    import javax.swing.*;
    import java.awt.*;
     
    public class MainPage extends JDialog {
        private JPanel contentPane;
        private JPanel cardPanel;
        private MainPage parentForm;
     
        //GUI Forms
        final static String INDEX = "Index";
        final static String INDEX2 = "Index 2";
     
        //Panels
        private Index zero;
        private Index2 one;
     
        //Card Layout Filler
        public void showPanel(String id) {
            final CardLayout cl = (CardLayout) cardPanel.getLayout();
            cl.show(cardPanel, id);
        }
     
        public MainPage() {
            setContentPane(contentPane);
            setModal(true);
            setDefaultCloseOperation(DISPOSE_ON_CLOSE);
     
            zero = new Index();
            zero.setParentForm(this);
            cardPanel.add(zero.getPanel(), INDEX);
     
            one = new Index2();
            one.setParentForm(this);
            cardPanel.add(one.getPanel(), INDEX2);
     
            //Initial form loading for default
            final CardLayout cl = (CardLayout) cardPanel.getLayout();
            cl.show(cardPanel, INDEX);
        }
        public void setParentForm(final MainPage parentForm) {
            this.parentForm = parentForm;
        }
        public static void main(String[] args) {
            MainPage dialog = new MainPage();
            dialog.pack();
            dialog.setVisible(true);
            System.exit(0);
        }
     
    }

    import javax.swing.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    public class Index {
        private JPanel panel;
        private JLabel lblamnt;
        private JButton button1;
        private JButton btn1;
        private JButton btn2;
        private JButton btn3;
        private JButton btn4;
        private JButton btn5;
        private JButton btn0;
        private JButton btnD;
        private JLabel lblDisplay;
        private MainPage parentForm;
        String tval = "";
        Index2 index2 = new Index2();
     
        public Index() {
            button1.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    index2.getvar(tval);
                    parentForm.showPanel(MainPage.INDEX2);
                }
            });
            btn0.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    tval = tval + 0;
                    lblamnt.setText(tval);
                }
            });
            btn1.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    tval = tval + 1;
                    lblamnt.setText(tval);
                }
            });
            btn2.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    tval = tval + 2;
                    lblamnt.setText(tval);
                }
            });
            btn3.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    tval = tval + 3;
                    lblamnt.setText(tval);
                }
            });
            btn4.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    tval = tval + 4;
                    lblamnt.setText(tval);
                }
            });
            btn5.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    tval = tval + 5;
                    lblamnt.setText(tval);
     
                }
            });
            btnD.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    lblDisplay.setText(tval);
                }
            });
        }
        public void setParentForm(final MainPage parentForm) {
            this.parentForm = parentForm;
        }
        public JPanel getPanel() {
            return panel;
        }
     
    }

    import javax.swing.*;
     
    public class Index2 {
        private JPanel panel;
        private JLabel lblgamnt;
        private MainPage parentForm;
        String someVar = "Initial";
     
        public void getvar(String varsent){
            someVar = varsent;
            System.out.println("getvar is "+someVar);
        }
     
        public Index2() {
            lblgamnt.setText("Test " +someVar);
            System.out.println("index2 is "+someVar);
        }
     
        public void setParentForm(final MainPage parentForm) {
            this.parentForm = parentForm;
        }
     
        public JPanel getPanel() {
            return panel;
        }
     
    }

    normal course of the program will output this...

    index2 is Initial
    index2 is Initial
    getvar is 12<--- random input number.

    when index2 loads it still doesnt update the variable or text even though it can run getvar method no problem when clicking on button1.

  12. #87
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: issue sending value to new pane

    Do you get any errors when executing the posted code? I do, because some code is missing.
    If you don't get errors, why not? Is the posted code what you are using?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #88
    Member
    Join Date
    Feb 2018
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: issue sending value to new pane

    i have in the past in this forum. page 2.

    It would require an external library from intellij. I have once again included all source files included the hidden gui portion to this link. This includes the .java files with all source code.

    https://mega.nz/#!EYpFmAhK!3oeJ_siRz...dQxRXZu9aLDdGw

  14. #89
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: issue sending value to new pane

    require an external library from intellij.
    Can you rewrite the code to use standard java se classes? At least while you are trying to get help. Later you could return to the intellij world.
    Otherwise you will have to wait until someone comes along that will work with the Intellij code.

    --- Update ---

    index2 is Initial
    index2 is Initial
    Why is that line shown 2 times? That is from the Index2 class's constructor. Is there supposed to be 2 instances of Index2?
    If you don't understand my answer, don't ignore it, ask a question.

  15. #90
    Member
    Join Date
    Feb 2018
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: issue sending value to new pane

    "one = new Index2();" MainPage.java
    "Index2 index2 = new Index2();" Index.java

    while you wait while i convert all the buttons...

    --- Update ---

    "one = new Index2();" MainPage.java
    "Index2 index2 = new Index2();" Index.java

    while you wait while i convert all the buttons...

  16. #91
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: issue sending value to new pane

    I don't think you want two instances of Index2. If the Index class needs a reference to the instance of Index2 created in the MainPage class, then that reference to Index2 should be passed to the Index class via its constructor when MainPage creates the instance of Index.

    I think I've said that at least two times before on this long thread.

    Also the name of the method: getvar is wrong. That method sets a value. It does not get a value.
    It should be called setVar
    If you don't understand my answer, don't ignore it, ask a question.

  17. #92
    Member
    Join Date
    Feb 2018
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: issue sending value to new pane

    what is the purpose of the sending if the variable isnt saved?

    So whjat you are saying is i should remove index2 instance from index

    go to mainpage then change one = new Index2() to.......what?

  18. #93
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: issue sending value to new pane

    remove index2 instance from index
    Yes. The only instance of Index2 should be the one created in MainPage.

    change one = new Index2()
    No.
    I said:
    reference to Index2 should be passed to the Index class via its constructor when MainPage creates the instance of Index.
    Pass the reference to Index2 to Index's constructor:
     ... new Index(referenceToIndex2);   // pass reference to Index2 to Index
    The name of the method: getvar is wrong. That method sets a value. It does not get a value.
    It should be called setVar
    If you don't understand my answer, don't ignore it, ask a question.

Page 4 of 4 FirstFirst ... 234

Similar Threads

  1. Replies: 0
    Last Post: December 7th, 2017, 12:55 PM
  2. Help incorporating a Joption Pane
    By Dysun in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 15th, 2012, 05:25 PM
  3. What exactly is a content pane?
    By TP-Oreilly in forum Java Theory & Questions
    Replies: 2
    Last Post: December 7th, 2011, 09:07 AM
  4. Sending large Strings ?! only sending a line
    By camel in forum Java Networking
    Replies: 2
    Last Post: April 19th, 2011, 12:41 PM
  5. JOption Pane help
    By dallas1125 in forum AWT / Java Swing
    Replies: 5
    Last Post: November 18th, 2009, 05:08 PM