Amit
Amit

Reputation: 34725

How to update JTextArea in Java Swing?

I have a JComboBox named "jComboBox18" and a JTextArea "jTextArea11". Now I want that whenever a item is selected from the "jComboBox18" combo box its corresponding description is shown in the "jTextArea11" textarea.

I have added the appropriate listener to the JComboBox But the JTextArea is not showing any text. The code that I have written is as follows:

private void jComboBox18ItemStateChanged(java.awt.event.ItemEvent evt) {

    Object item = jComboBox18.getSelectedItem();

    if(item != null) {
        ems.logic.Process selectedProcess = (ems.logic.Process)item;

        jTextArea11.setText(selectedProcess.getProcessDescription());
        jTextArea11.updateUI();
        jTextArea11.revalidate();
        jTextArea11.validate();
    } 
}

=====================EDITED===========================================

The method is being called for sure. I am changing the state of one more combobox which is also being written in this method and its state changes successfully whenever item is selected from the "jComboBox18"

Upvotes: 2

Views: 8846

Answers (2)

sateesh
sateesh

Reputation: 28653

In the code shown your method is named as jComboBox18ItemStateChanged. Are you sure this method is being called. The ItemListener for a JComboBox should implement the interface ItemListener which declares that the subclasses should implement the below method.

void itemStateChanged(ItemEvent e);

How are you adding an instance of ItemListener to your JComboBox ?

EDIT:
After reading your edit and comments one another possiblity that I can think of is that: you have a listener that is triggered when the textarea is updated and probably itis undoing the changes done in JComboBox listener.

Upvotes: 0

Chad Okere
Chad Okere

Reputation: 4578

I think That should work. In fact, you should only need the setText() call. My guess is that you're function isn't getting called for some reason. Put a break point in your code and make sure it's getting called.

Upvotes: 2

Related Questions