SagittariusA
SagittariusA

Reputation: 5427

Java Swing: cannot set JTextArea text in JDialog after it opens

I have a JTable where the last column is a JButton whose ActionListener is:

private class EventDetailActionListener implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                final long seqEventSel = tblModelEvents.getEventSeq(tblEvents.getSelectedRow());
                final String eventDetail = tblModelEvents.getEventDetail(tblEvents.getSelectedRow());
                new DialogEventDetail(seqEventSel).setDetailText(eventDetail);
            }
        });
    }
}

and the class for JDialog is:

public class DialogEventDetail extends JDialog {
    private JTextArea txtAreaDetail;

    public DialogEventDetail(JFrame parent) {
      /* Building JDialog with its size and a BorderLayout
         with a JScrollPanel at CENTER containing a
         txtAreaDetail */
      this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
      this.setVisible(true);
    }

    public void setDetailText(String text) {
      this.txtAreaDetail.setText(text);
    }
}

This JDialog is used to show a long XML text which is held by TableModel but not immediately visible to suer when JTable loads. JTextArea is not enabled to be edited by user but I cannot understand why it is always empty after JDialog appears. There's no text showed inside. Instead, if I call

this.txtAreaDetail.setText(text);

inside the constructor, the text appears. Why this?

Upvotes: 1

Views: 236

Answers (1)

Saran
Saran

Reputation: 146

Try using

this.txtAreaDetail.revalidate();
this.txtAreaDetail.repaint();

Any specific reason using EventQueue over SwingWorker.

As I can not add comments posting this as answer.

Upvotes: 1

Related Questions