Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7180863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:26:16+00:00 2026-05-28T17:26:16+00:00

I have a class of buttons called Keys.java which returns a panel of buttons

  • 0

I have a class of buttons called Keys.java which returns a panel of buttons to the class called Control.java. I have a JLabel in Control.java, but what I want to do is change a JLabel when a button is pressed. How would you go about doing this?

I have tried setting a string in Keys.java which changes based on the button and then setting the JLabel’s text equal to the string but it doesn’t seem to work.

Any thoughts on how to achieve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T17:26:16+00:00Added an answer on May 28, 2026 at 5:26 pm

    It may be that you are updating the wrong string or setting the corresponding label’s text incorrectly. Both are required. In the example below (using your names), the two updates are tightly coupled in the button’s actionPerformed(). A more loosely coupled approach is shown here.

    import java.awt.EventQueue;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import javax.swing.AbstractAction;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    /** @see https://stackoverflow.com/questions/9053824 */
    public class JavaGUI extends JPanel {
    
        private Control control = new Control();
        private Keys keys = new Keys("Original starting value.");
    
        public JavaGUI() {
            this.setLayout(new GridLayout(0, 1));
            this.add(keys);
            this.add(control);
        }
    
        private class Control extends JPanel {
    
            public Control() {
                this.add(new JButton(new AbstractAction("Update") {
    
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        System.out.println("Command: " + e.getActionCommand());
                        keys.string = String.valueOf(System.nanoTime());
                        keys.label.setText(keys.string);
                    }
                }));
            }
        }
    
        private class Keys extends JPanel {
    
            private String string;
            private JLabel label = new JLabel();
    
            public Keys(String s) {
                this.string = s;
                label.setText(s);
                this.add(label);
            }
        }
    
        private void display() {
            JFrame f = new JFrame("JavaGUI");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(this);
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new JavaGUI().display();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have inherited a c# class 'Button' (which I can't change) which clashes with
I have a set of class instances which have a BOOL instance variable called
I have a class called Menu and inside that class I want to place
I have three buttons Image , Videos , Audios. I want to change the
Hi I have a class called ColorChooser (in the net.java.dev.colorchooser.ColorChooser package) This is a
I have an array of buttons which shall all call the same method but
I have a css class that is called button.blue: button.blue { background-color: blue; ...
I have the following problem / bug: I made a custom button-class (called CustomBlitButton)
I have a formpanel that is extended to make a class which is called
I have a window with several buttons. I want when a button is clicked

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.