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 3 of 4 FirstFirst 1234 LastLast
Results 51 to 75 of 93

Thread: issue sending value to new pane

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

    Default Re: issue sending value to new pane

    the package was included in the rar i put in the youtube video.

    https://mega.nz/#!YcJgzbRb!evT7Or8qH...2zShjXzPp2zdEg

    all you need to do is compile in that directory with this command ==> "javac MainPage.java -cp forms_rt.jar;."

    My problem is not resolved. I still cant pass the value of a label to the index2 cardlayout.

  2. #52
    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: issue sending value to new pane

    Sorry, I don't need or want to clutter up my PC with special jar files for testing a program.

    If you want to stick with the IntelliJ packages, I'll leave you for someone that is willing to download and install whatever jar files are needed.

    If you will write vanilla java code, post it and I'll try to help.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: issue sending value to new pane

    Norm for 21,037 posts i fail to grasp how you think downloading this will be "clutter"

    It is one folder which i can presume you already made one. You simply put the contents of the rar in that folder and compile. No installing. Everything you need is in that folder. The only thing you need to do is compile it with the above -cp. This is no different than normally compiling it.

    Have you reviewed the youtube video? It clearly shows you everything you need to do. Which is 2 lines of command...Again NO DIFFERENT to what you normally would do. I use intellij for mainly the ease of compiling and the ease of creating GUIs quickly and easily. But for the sake of trying to resolve an issue what do you suggest i replace the code with?

  4. #54
    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: issue sending value to new pane

    If you have programming questions in the future you will have better luck finding some one to help if you do not use 3rd party packages that require downloads, creating folders, tailoring the IDE for that one problem and then doing compiles and executions. I'm sure there are people willing to do all that.

    I prefer to keep it simple with: copy and paste the source, compile and execute.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: issue sending value to new pane

    by viewing the code i posted previously how can i resolve this problem.

    Recap. I need the value of lblamnt or tval to be carried to index2 so i can pull. I tried doing a getter function but it pulls blanks.

  6. #56
    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: issue sending value to new pane

    need the value of lblamnt or tval to be carried to index2
    When does the value need to be available in Index2?
    When it is changed in an instance of the Index class?
    Or when it is needed by some code in the Index2 class?

    If the latter, if a reference to the instance of Index is passed to the constructor of Index2 when Index2 is created, the code in Index2 could use tht reference to call a getter method in Index.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: issue sending value to new pane

    "If the latter, if a reference to the instance of Index is passed to the constructor of Index2 when Index2 is created, the code in Index2 could use tht reference to call a getter method in Index."

    Can you provide a basic example for this? Clearly i cant get it right ive been on this damn thing for almost a month now trying to get it right.

  8. #58
    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: issue sending value to new pane

    Passing a reference to an object as an arg in a constructor:
       AnotherClass aReferenceToAnObject = ....
     
       // In class that creates instance of SomeClass
       ... = new SomClass(aReferenceToAnObject);  // pass object reference to a constructor
     
      // The constructor for SomeClass
      public SomeClass(AnotherClass refToObject) {
         this.refToObject = refToObject;  //  save reference 
         ...
     
        // In SomeClass
        // Call aMethod in AnotherClass using passed reference
       refToObject.aMethod();
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: issue sending value to new pane

    #1: Get rid of the initialization of gg in Index2

    Before:
    Index gg = new Index();

    After:
    Index index;

    This was a useless instance if Index that never gets used the way you have it now.

    (change the reference to gg in getter() to match index)


    #2: Make zero and one a global private variable in mainPage.

    //GUI Forms
        final static String INDEX = "Index";
        final static String INDEX2 = "Index 2";
     
     
    //Panels
        private Index zero;
        private Index2 one;



    #3:Get rid of the declare of zero and one in the constructor.

    Before:
    public MainPage(){
            setContentPane(contentPane);
            setModal(true);
            setDefaultCloseOperation(DISPOSE_ON_CLOSE);
     
            Index zero = new Index();
    ...
            Index2 one = new Index2();

    After :
    public MainPage(){
            setContentPane(contentPane);
            setModal(true);
            setDefaultCloseOperation(DISPOSE_ON_CLOSE);
     
            zero = new Index();
    ...
            one = new Index2();

    #4: Change the arguments the constructor of Index2 to accept an instance of Index
        public Index2(Index index) {
            this.index = index;
        }

    #5: Add instance of Index2 to Index

    Index2 index2;


    #6: Create a setter in Index

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


    #7: Change the call to the constructor of Index2 and call setter for zero to set the references to each other in MainPage

            Index zero = new Index();
            zero.setParentForm(this);
            cardPanel.add(zero.getPanel(), INDEX);
     
            Index2 one = new Index2(zero);
            one.setParentForm(this);
            cardPanel.add(one.getPanel(), INDEX2);
     
            zero.setIndex2(one);




    And now.... as Norm said...
    Quote Originally Posted by Norm View Post
    When does the value need to be available in Index2?
    As I said in my very first post...
    Quote Originally Posted by brynpttrsn View Post
    you only call the gval() function in the constructor at the time of app initialization. Im not sure if any changes that are made post initialization will be reflected in the value generated by gval(). Might have to be called later in the game after its been changed depending on when you want that value to be sent over to Index2:one.
    You probably want it somewhere in here:

            button1.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println(index2.getter() + " " + lblamnt.getText());
                    parentForm.showPanel(MainPage.INDEX2);
                }
            });

    I have added a print statement since they always help in figuring out what the values of all the variables are without IDEs or breakpoints.


    WORDS OF SLIGHT BUT NOT ENTIRELY KIND OF CONFUSION AND OR GENERAL BEWILDERMENT:

    Usually variable names are slightly more descriptive of exactly what they are kind of like how I changed "gg" to "index". You may have worked with the code for months and know exactly what types and uses of all of the variables are, but when you get stuck and need to ask for help... whomever is going to look at the code, this time Norm and I, is really going to have trouble discerning what variables "gg" "zero" or "one" are. It also might help the future you if you need to come back and edit, revise, or reuse your own code. "zero" could be changed to something like "index" and "one" could probably be something like "index2".


    DISCLAIMER: I, like Norm, have not downloaded, compiled, run, tested, or anything like that in order to get this to compile on my own system. I have not tested or debugged any code and in fact don't even know if my code will compile as is. It was too much trouble to even write a compileable source since so many variable declarations in your provided source are missing.

    Essentially, I'm just eyeballing it.

    DISCLAIMER#2: This solution is just how I would do it in addition to reading the suggestion from Norm on which direction to go. In no way am I saying that this is the only way or the best way. I am open for suggestions for improvements.

    If you need additional troubleshooting guidance I'll be available after my brief hospital visit... I broke all of my fingers typing this extensively in depth novel of a post.

    Good day sir.
    Last edited by brynpttrsn; February 24th, 2018 at 04:18 AM. Reason: Spelling and grammar is for squares

  10. #60
    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 updated the code according to brynpttrsn.

    MainPage.java

    import javax.swing.*;
    import java.awt.*;
     
    public class MainPage extends JDialog {
        private JPanel contentPane;
        private JPanel cardPanel;
     
        //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);
     
            zero.setIndex2(one);
        }
     
        public static void main(String[] args) {
            MainPage dialog = new MainPage();
            dialog.pack();
            dialog.setVisible(true);
            System.exit(0);
        }
     
    }

    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;
        public static String tval = "";
     
        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 setIndex2(Index2 index2){
            this.index2 = index2;
        }
        public static String tgval() {
            return 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(getter());
            System.out.println(getter());
        }
     
        public static String getter() {
        }
     
        public void setParentForm(final MainPage parentForm) {
            this.parentForm = parentForm;
        }
     
        public JPanel getPanel() {
            return panel;
        }
     
    }

    Few errors.

    MainPage.java

    one = new Index2();

    "Index2(Index) in Index2 cannot be applied"

    index.java

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

    "Cannot resolve symbol 'index2'"

    Index2.java

    this.index = index;

    "Cannot resolve symbol index"

    Sorry for delay i have been extremely busy with personal affairs.

  11. #61
    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: issue sending value to new pane

    "Index2(Index) in Index2 cannot be applied"
    The compiler only found one constructor that takes an Index object. The code needs to pass a reference to an Index object to the constructor.

    "Cannot resolve symbol 'index2'"
    The compiler can not find a definition for the variable: index2. Make sure a variable is defined that is in scope where it is referenced.

    "Cannot resolve symbol index"
    Same as above.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #62
    Member
    Join Date
    Feb 2018
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: issue sending value to new pane

    can you provide an example on how this would be?

    i learn best when an example of code is displaced with a brief explanation.

  13. #63
    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: issue sending value to new pane

    From your code:
      private MainPage parentForm;    //  define variable here
       .... 
        public void setParentForm(final MainPage parentForm) {
            this.parentForm = parentForm;    // save value in above variable
        }
    If you don't understand my answer, don't ignore it, ask a question.

  14. #64
    Member
    Join Date
    Feb 2018
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: issue sending value to new pane

    those are already in index and index2, i have to put this again in mainpage?

  15. #65
    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: issue sending value to new pane

    The variables need to be defined in scope where they are being used. The compiler could not find definitions for the variables that are shown in the error messages:
    "Cannot resolve symbol 'index2'"

    "Cannot resolve symbol 'index'"
    Go to the classes where those errors are located and add the missing definition for the variable the compiler could not find.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #66
    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
    public void setIndex2(Index2 index2){
            Index2 Index2;
            this.index2 = index2;
        }

    index2.java

    public Index2(Index index) {
            Index Index;
            this.index = index;
            lblgamnt.setText(getter());
            System.out.println(getter());
        }

    Issue persists.

  17. #67
    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: issue sending value to new pane

    Look at the example in post#63. It shows that the variable is defined OUTSIDE of the method at the class level.

    Also java is case sensitive. I is not the same as i. Index is different from index.

    Here is another example:
    /* Display compiler error message for missing definition
    */
     
    public class MissingDefinition {
    //   String theVariable;       //  define variable here - it is missing as a comment
     
       public void saveTheVariable(String theVariable) {
    	   this.theVariable = theVariable;
    	}
    }/*  The error message for that above:
     
    MissingDefinition.java:7: error: cannot find symbol
    	   this.theVariable = theVariable;
    	       ^
      symbol: variable theVariable
    1 error
    */
    Removing the // at the start of line 5 will allow the compiler to see the definition of theVariable and there won't be a compiler error.
    If you don't understand my answer, don't ignore it, ask a question.

  18. #68
    Member
    Join Date
    Feb 2018
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: issue sending value to new pane

    thank you norm, that was my own careless mistake. I fixed those and put it in the class. It resolved the errors for index and index2.

    MainPage still persists.

  19. #69
    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: issue sending value to new pane

    MainPage still persists.
    Please explain.
    If you don't understand my answer, don't ignore it, ask a question.

  20. #70
    Member
    Join Date
    Feb 2018
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: issue sending value to new pane

    MainPage.java

    one = new Index2();

    "Index2(Index) in Index2 cannot be applied"

  21. #71
    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: issue sending value to new pane

    Do you understand what I said in post#61?

    Look at the constructor for Index2 and see what its requirements are.
    If you don't understand my answer, don't ignore it, ask a question.

  22. #72
    Member
    Join Date
    Feb 2018
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: issue sending value to new pane

    its looking for a variable to be sent in the Index2().

    So is this the part where i send the value of lblamnt/tvar in index?

    How would i call that value in here?

  23. #73
    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: issue sending value to new pane

    its looking for a variable to be sent in the Index2().
    Yes, what is the data type of that variable?
    Hint: The data type is given in the error message and in the source line that defines the Index2 constructor.
    If you don't understand my answer, don't ignore it, ask a question.

  24. #74
    Member
    Join Date
    Feb 2018
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: issue sending value to new pane

    the lblamnt is a JLabel and tvar is a String.

    error.jpg

    The data type isnt in the error.

  25. #75
    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: issue sending value to new pane

    Did you fix the compiler errors? Does the code compile ok now?

    the lblamnt is a JLabel and tvar is a String.
    Are you moving to a new problem now? Please explain.

    I work on various forums with multiple programming problems and do not always remember what was posted on a particular problem days ago. It'd be helpful if you'd explain what the current problem is.
    If you don't understand my answer, don't ignore it, ask a question.

Page 3 of 4 FirstFirst 1234 LastLast

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