-2

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?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
mino
  • 6,978
  • 21
  • 62
  • 75
  • 6
    Please post minimal code that reproduces your issue. – Mat Jan 29 '12 at 14:17
  • Maybe this post can help you http://stackoverflow.com/questions/9046175/updating-jlabel/9046558#9046558 – alain.janinm Jan 29 '12 at 14:25
  • Yours is likely a problem of references -- not using a correct reference to the viewed JLabel in your JButton's ActionListener -- but again without code, who knows. Please give enough information so that your question is actually answerable and so we don't have to waste our time guessing. – Hovercraft Full Of Eels Jan 29 '12 at 14:25
  • Did someone edited the code by mistake ? – nIcE cOw Jan 29 '12 at 16:18
  • @GagandeepBali: what code? And that's the entire problem -- the original poster won't post any despite many requests. – Hovercraft Full Of Eels Jan 29 '12 at 16:23
  • @HovercraftFullOfEels : I thought the code was so bad, that someone edited it that much, that it's not visible now :-) – nIcE cOw Jan 29 '12 at 16:36

1 Answers1

3

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();
            }
        });
    }
}
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045