Kaj
Kaj

Reputation: 2503

Java KeyListener change JFrame label value

I've the following code:

package testOpdracht1;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.io.InputStream;

public class MainMenu extends JFrame implements KeyListener {

    public MainMenu() {
        initUI();
    }

    public final void initUI() {

        JLabel label1 = new JLabel("text1");
        add(label1);

        addKeyListener(this);

        setTitle("Bla");
        setPreferredSize(new Dimension(400,250));
        setMinimumSize(getPreferredSize());
        setResizable(true);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void keyPressed(KeyEvent e) {

    }
    public void keyReleased(KeyEvent e) {

    }
    public void keyTyped(KeyEvent e) {

    }


    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                MainMenu ex = new MainMenu();
                ex.setVisible(true);
            }
        });
    }    
}

I would like to change the text in the label when any button is pressed. How can I do this? I know I can call methods from the JFrame class, since my MainMenu class extends it, but I can't find the way to refer to the label element in order to change the value.

Sincerely,

Luxo

Upvotes: 4

Views: 1868

Answers (3)

hd1
hd1

Reputation: 34657

Modify your code to look like:

package testOpdracht1;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.io.InputStream;

public class MainMenu extends JFrame implements KeyListener {
    final JLabel label1 = new JLabel("text1");
    public MainMenu() {
        initUI();
    }

    public final void initUI() {
        add(label1);

        addKeyListener(this);

        setTitle("Bla");
        setPreferredSize(new Dimension(400,250));
        setMinimumSize(getPreferredSize());
        setResizable(true);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void keyPressed(KeyEvent e) {
          label1.setText("foo");
    }

    public void keyReleased(KeyEvent e) {

    }
    public void keyTyped(KeyEvent e) {

    }


    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                MainMenu ex = new MainMenu();
                ex.setVisible(true);
            }
        });
    }    
}

Upvotes: 1

Mickäel A.
Mickäel A.

Reputation: 9357

You have to declare your JLabel as a global variable, then in any of the keyXXX() method you can change its text using the setText() method of the JLabel class.

Upvotes: 0

Sujay
Sujay

Reputation: 6783

Declare the JLabel as a global variable and instantiate it as you've, in the initUI(). Now in your methods of ActionListener when you handle the event, you can change the text of your label over there.

Upvotes: 1

Related Questions