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

Thread: My birthday was Yesterday, can someone look at my code. Trouble with updating JTEXT

  1. #1
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default My birthday was Yesterday, can someone look at my code. Trouble with updating JTEXT

    class Gamehandler extends JFrame implements ActionListener {
        //questions
     
        JPanel qpanel = new JPanel();
        //answers
        JPanel aPanel = new JPanel();
        JRadioButton[] responses;
        ButtonGroup group = new ButtonGroup();
        //bottom
        JPanel botPanel = new JPanel();
        JButton next = new JButton("next");
        JButton finish = new JButton("Finish");
        static String guess = " ";
        readExcel re = new readExcel();
        JLabel label = new JLabel();
        JFrame frame = new JFrame("Test");
     
        public void actionPerformed(ActionEvent event) {
            re.readExcel2();
            Random rm = new Random();
            int radiobuttonsize = readExcel.ColumnTwo2.size();
            System.out.print("this is the size ofthe radiobuttonsize"+radiobuttonsize);
            radioButtons rbs = new radioButtons();
            scoreTest sr = new scoreTest();
            responses = new JRadioButton[readExcel.ColumnTwo2.size()];
            //adds my radio buttons 
            //adds action listeneners 
            //adds get actions
            for (int i = 0; i < readExcel.ColumnTwo2.size(); i++) {
                responses[i] = new JRadioButton(readExcel.ColumnTwo2.get(i));
                group.add(responses[i]);
                aPanel.add(responses[i]);
                responses[i].addActionListener(rbs);
                responses[i].setActionCommand(String.valueOf(i));
                Object selection = responses[0].getAction();
            }
     
            int randomguessArray = rm.nextInt(radiobuttonsize);
            guess = readExcel.ColumnOne1.get(randomguessArray);
            label = new JLabel();
            label.setText("What does this mean " + " " + guess);
            qpanel.add(label);
            frame = new JFrame("Test");
            frame.setSize(400, 300);
            frame.setResizable(true);
            frame.setVisible(true);
            frame.add(aPanel);
            botPanel.add(next);
            botPanel.add(finish);
            frame.getContentPane().add(qpanel, BorderLayout.NORTH);
            frame.getContentPane().add(aPanel, BorderLayout.CENTER);
            frame.getContentPane().add(botPanel, BorderLayout.SOUTH);
            next.addActionListener(sr);
         String event1 =   event.getActionCommand();
         System.out.println("This is the event1"+event1);
        }
     
        public void changeText() {
            label.setText("Work");
            label.revalidate();
        }
    }
    //next button changes the label
    class scoreTest extends JFrame implements ActionListener {
     
        public void actionPerformed(ActionEvent event) {
            Gamehandler gr = new Gamehandler();
           gr.changeText();
        }
    }
    I hope I didn't add to much of my code. So I added an actionlistener to the JButton and when it's pressed I want it to update the JLabel in the JFrame. I am unsure why this is not working correctly. I looked through a lot of notes and could not find anything that would help me. There is no errors, it just doesn't update. I am new to programming and thought I this part would be easy.

    I have tried to update the panel with the new label and still nothing. I hope you can point me in the right direction.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: My birthday was Yesterday, can someone look at my code. Trouble with updating JTEXT

    It should be easy, but you made it difficult.

    It's okay to have another class like scoreTest (should be named ScoreTest) be the ActionListener, that's even a good thing, but there's no reason for it to extend JFrame. But the real problem is that scoreTest creates a new instance of Gamehandler and calls that instance's changeText() method. The new instance may never be visible (it's not clear when Gamehandler is setVisible()), so you don't see the change in the new instance, but it's happening somewhere in memory.

    You might rewrite ScoreTest as:
    //next button changes the label
    class ScoreTest implements ActionListener
    {
    	Gamehandler gh;
     
            // default constructor
    	public ScoreTest( Gamehandler gh )
    	{
    		this.gh = gh;
    	}
     
        public void actionPerformed(ActionEvent event)
        {
           gh.changeText();
        }
    }
    Then when you create the action listener instance, pass the current instance of Gamehandler to its constructor:

    ScoreTest sr = new ScoreTest( this );

    I couldn't test this approach because of the missing parts of your code, but the concept is sound. Minor adjustments may be necessary.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    loui345 (August 18th, 2013)

  4. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: My birthday was Yesterday, can someone look at my code. Trouble with updating JTEXT

    revalidate() is used to update changes made to the layout.
    After changing the text you should only need to call repaint()

  5. #4
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: My birthday was Yesterday, can someone look at my code. Trouble with updating JTEXT

    Hello.
    Instead of revalidate(), can you invoke validate() on the layout object and let me know if it worked.

    Syed.

  6. #5
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Re: My birthday was Yesterday, can someone look at my code. Trouble with updating JTEXT

    HI,

    Thank you the fix worked, but I have to make sure I understand exactly from this situation,so I can grow from this situation and correct my thinking pattern. So in this analogy, if Objects represent remote controls my controller was pointed at an entirely different TV,so what you did to fix this problem, was to keep my remote pointed at the same TV so it speak. You achieved this my passing the instance of the class through the constructor.

    I would have never came to that conclusion on my own. Hopefully, my explanation is correct on this, because it seems to make sense in my head at 2:30am in the morning. Keep in mind, I have to wake up early tomorrow to go hang with the family early in the AM. Nothing in the world is more important than overcoming a programming problem. Thank you my kind sir.

  7. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: My birthday was Yesterday, can someone look at my code. Trouble with updating JTEXT

    I'm not a huge fan of the TV analogy, but as one who constantly has one on, I can see the attraction, so I'll do my best:

    The object is the TV, the action listener the remote control. You had 2 TVs (two Gamehandler objects), one visible and one not - maybe in another room entirely - and the action listener (remote control) was set for or pointed at the wrong TV.

    Have a great family day!

    Edit: Oh! And a belated Happy Birthday! (belated is a weird word)

Similar Threads

  1. resubmitting question from yesterday
    By deeprdeepr in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 11th, 2013, 07:59 AM
  2. Replies: 1
    Last Post: February 8th, 2013, 07:37 AM
  3. Trouble Updating an Array
    By Gravity Games in forum Java Theory & Questions
    Replies: 36
    Last Post: July 25th, 2012, 12:52 AM
  4. Problem with JText Fields and using action listener
    By toble in forum AWT / Java Swing
    Replies: 2
    Last Post: October 27th, 2010, 04:44 PM

Tags for this Thread