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 1 of 4 123 ... LastLast
Results 1 to 25 of 93

Thread: issue sending value to new pane

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

    Default issue sending value to new pane

    I am having trouble trying to send a label value to another form. Below is the code copied to pastebin.

    MainPage.java

    https://pastebin.com/BJuy0yHn

    Index.java

    https://pastebin.com/BYf06qdG

    Index2.java

    https://pastebin.com/3LkwTz3z

    What i am trying to achieve is have index let the user modify the lblamnt field to a purchase amount(eg 109.99) grabbed all the user input and send it to index2 and have it displayed. Problem is it doesnt send anything the user input it in. EG. default is 1337 user put in 45 with final string as 133745. The amount gets sent and display only 1337.
    Last edited by CGBubbles; February 19th, 2018 at 11:23 PM. Reason: pastebin links

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

    Default Re: issue sending value to new pane

    It appears at a glance that the this would be your basic call stack up until you grab the value of lblamnt.

    Basic call stack:
    main(String[] args)
        MainPage dialog = new MainPage();
            Index2 one = new Index2();
                lblgamnt.setText(getter());
                    return gg.gval();
                        lblamnt.getText()

    gg being a seperate instance of type Index created and initialized in your Index2:one instance. You probably meant to use the instance you create in your MainPage class. That being said: youre changing lblamnt in the Index:zero instance and trying to read the lblamnt in the Index:gg instance.
    I only looked for a second but this might be part of the problem.

    You also might run into trouble because 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.

  3. #3
    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

    Please post any code you have questions about here on the forum, not as links.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: issue sending value to new pane

    Norm...thye code is in pastebin already...but oh well..

    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";
     
        //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);
     
            Index zero = new Index();
            zero.setParentForm(this);
            cardPanel.add(zero.getPanel(), INDEX);
     
            Index2 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 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 MainPage parentForm;
     
        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) {
                    lblamnt.setText(lblamnt.getText() + "0");
                }
            });
            btn1.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    lblamnt.setText(lblamnt.getText() + "1");
                }
            });
            btn2.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    lblamnt.setText(lblamnt.getText() + "2");
                }
            });
            btn3.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    lblamnt.setText(lblamnt.getText() + "3");
                }
            });
            btn4.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    lblamnt.setText(lblamnt.getText() + "4");
                }
            });
            btn5.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    lblamnt.setText(lblamnt.getText() + "5");
                }
            });
        }
        public String gval (){
            return lblamnt.getText();
        }
        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 gg = new Index();
     
        public Index2() {
            lblgamnt.setText(getter());
        }
     
        public String getter(){
            return gg.gval();
        }
     
        public void setParentForm(final MainPage parentForm) {
            this.parentForm = parentForm;
        }
        public JPanel getPanel() {
            return panel;
        }
    }
    .
    again. im trying to get the value of lblamnt(index) and have that value carried over to lblgamnt(index2)

  5. #5
    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

    get the value of lblamnt(index)
    I see that lblamnt is defined as a JLabel in the Index class not a method. So that syntax is wrong. You don't use the name of a JLabel object as a method and pass it the value of index.
    Can you explain what you mean by : lblamnt(index)? It doesn't make any sense.

    The same for lblgamnt.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: issue sending value to new pane

    sorry a misunderstanding. i meant lblamnt(index) as in the variable located in index as in the code snippet. not in the literal coding sense.

    Could you give an example? i have to see the code to understand how it works for this.

  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: issue sending value to new pane

    Does the code compile without errors?
    If you get a clean compile, are there any errors when you try to execute the code?

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

    variable located in index
    Note: Class names should start with an uppercase letter. Index is the class name, not index. Using the correct class name prevents some confusion.

    Could you give an example?
    An example of what?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: issue sending value to new pane

    No compiling errors. When i am on Index.java code and i press button1 to go to Index2.java. The variable im trying to pass is lblamnt or tvar(which is just tvar=tvar+0 or whatever number is pushed.)

    --- Update ---

    Anything? I have been stuck on this for almost a month.

  9. #9
    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

    I get execution errors when I compile and execute the posted code.
    If you don't get errors when executing the code, then you must have a different version of the code.
    Please post the version of code that you are using for testing so we all can work with the same code.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    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 no idea what compile errors you would be getting.

    I have rared the whole folder.

    https://mega.nz/#!pAgy3LwK!VeprxFczc...M4wQcuMra1DE7A

  11. #11
    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

    I am not getting any compiler errors. I am getting errors when I try to execute the code.
    For example:
    Exception in thread "main" java.lang.NullPointerException
    at MainPage$Index.<init>(MainPage.java:72)
    at MainPage.<init>(MainPage.java:36)
    at MainPage.main(MainPage.java:50)
    Please post the code here on the forum so we can see it to save having to download and extract it before we can see it.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    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 uploaded the files and what you have in the mega.nz is the most current code. I just compiled it fine.

  13. #13
    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

    There is no problem with the compiling.

    The problem is with the execution of the code. It gets the exception I posted in post#11

    If the code does not get any exceptions when you execute it, then there must be a difference in the code so that yours works and the posted code does not.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: issue sending value to new pane

    i just rared it 30 minutes ago. I havent touched it. And it also executes just fine.

    https://youtu.be/V_5O8107xHo

  15. #15
    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

    Post the code so I can see it. Not as a link that requires download and unpacking.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    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

    import javax.swing.*;
    import java.awt.*;
     
    public class MainPage extends JDialog{
        private JPanel contentPane;
        private JPanel cardPanel;
        Index Index = new Index();
     
        //GUI Forms
        final static String INDEX = "Index";
        final static String INDEX2 = "Index 2";
     
        //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);
     
            Index zero = new Index();
            zero.setParentForm(this);
            cardPanel.add(zero.getPanel(), INDEX);
     
            Index2 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 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 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 gg = new Index();
     
        public Index2() {
            lblgamnt.setText(getter());
            System.out.println(getter());
        }
     
        public static String getter(){
            return Index.tval;
        }
     
        public void setParentForm(final MainPage parentForm) {
            this.parentForm = parentForm;
        }
        public JPanel getPanel() {
            return panel;
        }
    }

  17. #17
    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

    Same problem with the new code:
    Exception in thread "main" java.lang.NullPointerException
    at MainPage1$Index.<init>(MainPage1.java:71)
    at MainPage1.<init>(MainPage1.java:16)
    at MainPage1.main(MainPage1.java:46)
    Where is button1 given a value before it is used?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: issue sending value to new pane

    button1 is solely to bring the user to index2. There was never a value given to the button1 itself. Could you elaborate?

    Also i am confused as to what you have or where you are executing...my code works flawlessly. I copy+pasted it straight from a working code.

  19. #19
    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

    There was never a value given to the button1 itself.
    There must be a value in the button1 variable, otherwise it is null. When the statement:
       button1.addActionListener(new ActionListener() {
    is executed in the Index constructor, a NullPointerException will be thrown.

    Can there be another version of the code that you are executing?
    What is the command that you use to execute the code?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: issue sending value to new pane

    https://youtu.be/V_5O8107xHo

    this is the video that shows me running the program no errors. I am using 1.8 Java on IntelliJ. I press the Green Play button on top.

  21. #21
    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

    Where is the variable: button1 given a value? If it is null then the program will throw a NPE.

    What is the command that you use to execute the code?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: issue sending value to new pane

    Im not sure what you mean. button1 was never meant to have any value assigned to it other than the text "Button". Its sole purpose is to bring the user to Index2.java.

    button1.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    parentForm.showPanel(MainPage.INDEX2);
                }
            });

    my IntelliJ IDE handles the commands to run the program. I have demonstrated to you in this video https://youtu.be/V_5O8107xHo and my java version is 1.8 for compile and run. I tried showing you this but you replied with...

    Quote Originally Posted by Norm View Post
    Post the code so I can see it. Not as a link that requires download and unpacking.
    So i apologize but im not quite sure what you expect as i have already provided the answers.

  23. #23
    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

    button1 was never meant to have any value assigned to it other than the text "Button".
    Where is that done? The posted code does not assign any value to button1.

    If button1 is null then this statement will throw a NPE:
       button1.addActionListener(new ActionListener() {

    I don't understand how your version of the code can execute if button1 has a null value.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: issue sending value to new pane

    Please review this video of what i recorded moments ago. This explains what version im on and how i run it.

    https://youtu.be/51H5iuuwIKc

  25. #25
    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 do not go to third party links.

    A video does not prove that that posted code is what you are executing.

    Answer the question in post#23. Where is button1 assigned any value?
    Where is there a statement with = that assigns a value?
    ... button1 = ...
    If you don't understand my answer, don't ignore it, ask a question.

Page 1 of 4 123 ... 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