【发布时间】:2017-11-15 11:22:31
【问题描述】:
我有一个 JTable,其中最后一列是一个 JButton,其 ActionListener 是:
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);
}
});
}
}
JDialog 的类是:
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);
}
}
此 JDialog 用于显示长 XML 文本,该文本由 TableModel 保存,但在 JTable 加载时不会立即对用户可见。 JTextArea 未启用用户编辑,但我不明白为什么 JDialog 出现后它总是为空。 里面没有文字显示。相反,如果我调用
this.txtAreaDetail.setText(text);
在构造函数中,文本出现。 为什么会这样?
【问题讨论】:
-
考虑到在模态对话框上调用 setVisible 会阻塞 UI,直到对话框被释放,所以使用构造函数是最好的解决方案。但是目前你的对话框不是模态的,所以这不应该发生
-
我刚刚发现了!是的,我的 JDialog 是模态的,我忘了提它。在 setDetailText() 中移动 setVisible() 方法现在似乎可以工作了。
标签: java swing jtextarea jdialog invokelater