user2644819
user2644819

Reputation: 1827

JTextArea - setText() and append() not working in Listener method()

As the title says i have a JList on the left side and a JTextArea on the right.

I set up my listener for the JList in the makeTextPanel() method like this:

listSelectionModel = list.getSelectionModel();
listSelectionModel.addListSelectionListener(this);

where the method being called on the action event is:

//This method is required by ListSelectionListener.
public void valueChanged(ListSelectionEvent e) {
    System.out.println("HEY");
    textArea.setText(null);
    textArea.setText("1: \n");
    textArea.append("2: \n");
    textArea.append("3: \n");
    textArea.append("4: \n");
    }

The event happens because in the terminal i can see "HEY" but nothing gets printed to the textarea. If i try to print to the textarea in the makeTextPanel() method after creating the textarea then it will print. What is wrong? Why wont it print in the event method valueChanged? I would really appreciate it if someone could take a look at see what i am missing here.

Full code:

Upvotes: 3

Views: 3151

Answers (1)

Sanjeev
Sanjeev

Reputation: 9946

It is working as per your code. You have declared textArea and JList globally so these two will only hold latest instances that is the instances you created while making edit tab. so if you see the edit tab your textArea will be having that text you set in the method.

you have to keep different instances of textArea and List for all your tabs and then only it will produce desired behavior.

Hope this helps.

Upvotes: 5

Related Questions