1

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?

SagittariusA
  • 5,289
  • 15
  • 73
  • 127
  • 2
    Consider that calling setVisible on a modal dialog blocks the UI until the dialog is disposed, so using the constructor is the best solution. However currently your dialog is not modal, so this should not happen – Ansharja Nov 15 '17 at 11:39
  • 1
    I just discovered that! Yes, my JDialog is a modal, I forgot to mention it. Moving setVisible() method inside setDetailText() seems to work now. – SagittariusA Nov 15 '17 at 11:41

1 Answers1

1

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.

Saran
  • 146
  • 7
  • Thank you for your answer. There's no any specific reason for EventQueue instead of SwingWorker. I just use it because previous developer who worked in my product did the same. Anyway, i discovered that the problem might be linked to the fact I called that method after JDialog is made visibile. Moving setVisible() method inside setDetailText() seems to work now. – SagittariusA Nov 15 '17 at 11:40
  • This is never required for showing text in a JTextArea, only after adding or removing components in a container (i.e., JPanel), and is called on the container, not the component. – Hovercraft Full Of Eels Nov 15 '17 at 12:06
  • Put another way -- since a JTextArea is never used as a container, there is **never** a reason to make these method calls on it. Ever. – Hovercraft Full Of Eels Nov 15 '17 at 12:09
  • Thank you for the update, In this case as the visible dialog is blocking the EDT to call setDetailText(eventDetail), the text area is not populated. Is there any case where we will be using repaint on JTextArea, I have seen examples, so was curious. – Saran Nov 15 '17 at 12:36
  • Show me a link to one decent example please. – Hovercraft Full Of Eels Nov 15 '17 at 12:43
  • https://findusages.com/search/javax.swing.JTextArea/repaint$0?offset=35 – Saran Nov 15 '17 at 12:45
  • A *decent* example please, one espoused in a legitimate tutorial or by a Swing expert on a q/a site. – Hovercraft Full Of Eels Nov 15 '17 at 12:53