Chris
Chris

Reputation: 85

How can I change the visibility of a JTextArea/JTextLabel?

I tried using .setVisibility(true) but got a NullPointerException. How can I do this more effectively? I want the labels and fields to start invisible then become visible when the user presses a button. I also want to change the size of an object. Ideas? Heres one of the line of error:

public JLabel lblName;




    JLabel lblName = new JLabel("Name:");
    lblName.setFont(new Font("Tahoma", Font.PLAIN, 15));
    lblName.setBounds(10, 91, 79, 19);
    panel.add(lblName);
    lblName.setVisible(false);


    public void actionPerformed(ActionEvent e) {
        lblName.setVisible(true);
    }

All of the above are seperate code snippets that worked flawlessly until I added the 3rd snippet.

Upvotes: 0

Views: 578

Answers (2)

Bob
Bob

Reputation: 83

In other words go to this line:

JLabel lblName = new JLabel("Name:");

and make it say

lblName = new JLabel("Name:");

What was happening was you were initiating a new local variable and never setting the public variable.

Upvotes: 0

Lews Therin
Lews Therin

Reputation: 10995

Judging from the code in your question:

You have a field and local JLabel lblName You are not initializing the field, only the local variable. Hence your exception.

Upvotes: 1

Related Questions