Results 1 to 6 of 6
Like Tree2Likes
  • 1 Post By DarrylBurke
  • 1 Post By JosAH

Thread: Need new String value to appear on button

  1. #1
    Maarten is offline Banned
    Join Date
    Dec 2011
    Location
    Utrecht
    Posts
    26
    Rep Power
    0

    Default Need new String value to appear on button

    Hello,

    I dont know why this does not work. A string called naam is empty in the beginning. Thats why its NULL on the button. But if I give it the value "karel", it still states null on the button, how can I update this?

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    
    
    public class Probeer
    {
        
        private JFrame frame;
        private JMenuBar hetMenu;
        private JButton knop;
        private String naam;
    
        /**
         * Constructor for objects of class Probeer
         */
        public Probeer()
        {
            // initialise instance variables
            makeFrame();
            
        }
    
       
        public void makeFrame()
        {
            frame = new JFrame("probeersel");
            
            makeMenu();
            
            Container frameVeld = frame.getContentPane();
            
            knop = new JButton("En de naam is: " + naam);
            knop.setBackground(Color.WHITE);
            frameVeld.add(knop);
           
            
            frame.pack();
            frame.setVisible(true);
            
        }
       
         
        public void makeMenu()
        {
            hetMenu = new JMenuBar();
            frame.setJMenuBar(hetMenu);
            
            JMenu zien = new JMenu("naamgeving");
            hetMenu.add(zien);
            
            JMenuItem zieKnop = new JMenuItem("Geef de string een naam");
            zien.add(zieKnop);
            
            zieKnop.addActionListener(new ActionListener(){
                        public void actionPerformed(ActionEvent e) 
                        {
                            naam = "karel";
                            knop.revalidate();
                            knop.repaint();
                            
                        }
                    });
            
            
            
        }
        
        
        
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Need new String value to appear on button

    The variable 'naam' is in no way related to the text on the button; try knop.setText(naam); in your listener.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Need new String value to appear on button

    You need to consider the order that the statements are executed. When the button is created, the value in naam was used. If the value of naam is changed, that will not change the contents of the button.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    Maarten is offline Banned
    Join Date
    Dec 2011
    Location
    Utrecht
    Posts
    26
    Rep Power
    0

    Default Re: Need new String value to appear on button

    Quote Originally Posted by Norm View Post
    You need to consider the order that the statements are executed. When the button is created, the value in naam was used. If the value of naam is changed, that will not change the contents of the button.
    Thanks, I didn't think of the fact the button is what it is when made. I thought it updated when I called repaint(), but wrong thougt-process on my part. I'll use settext, what Jos suggested, should work, thanks again! Maarten.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: Need new String value to appear on button

    The button *is* re-drawn when you call repaint(). But that doesn't magically set its text property to the new value of the variable you used when assigning the text earlier.

    db
    Maarten likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Need new String value to appear on button

    b.t.w. if you change the text with the setText( ... ) method, there's no need to repaint() the Component; i.e. it is clever enough to update its visual appearance.

    kind regards,

    Jos
    Maarten likes this.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 2
    Last Post: 06-20-2014, 09:24 AM
  2. returning a string when button clicked
    By droidus in forum AWT / Swing
    Replies: 13
    Last Post: 02-02-2012, 04:50 AM
  3. Replies: 7
    Last Post: 08-06-2011, 04:39 AM
  4. Make a button class that uses your button image.
    By eLancaster in forum New To Java
    Replies: 1
    Last Post: 04-26-2011, 12:32 PM
  5. Adding a sqrt button and percentage button to a calculator
    By Josie_Taylor in forum New To Java
    Replies: 4
    Last Post: 03-14-2011, 02:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •