import java.awt.*; import javax.swing.*; import javax.swing.event.ChangeListener; /* * This class creates dialog which contains button. Pressing * button adds JTextArea. After addin text area the dialog is * resized accoring to its minimal size. */ public class TestMain { public TestMain() { } private JDialog jd; public void test() { jd = new JDialog(); jd.setModal(true); jd.setSize(300,300); jd.getContentPane().add(new ButtonPanel()); jd.show(); } public static void main(String[] args) { TestMain tm = new TestMain(); tm.test(); } public class ButtonPanel extends javax.swing.JPanel { private javax.swing.JButton jButton1; public ButtonPanel() { initComponents(); } private void initComponents() { jButton1 = new javax.swing.JButton(); setLayout(new BorderLayout()); jButton1.setText("clickMe"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); add(jButton1, BorderLayout.SOUTH); } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { add(new TextPanel(), BorderLayout.CENTER); // Force dialog to resize. // The getMinimumSize() & setSize() calls might not be necessary, // but should work I think. After fix of 4410243 it does not. Dimension sz = jd.getSize(); System.err.println("JDialog.getSize()="+sz); Dimension minsz = jd.getMinimumSize(); System.err.println("JDialog.getMinimumSize()="+minsz); System.err.println("!!! height of the JDialog.getMinimumSize() is too high on j2sdk1.4.2_b16!!!"); if (minsz.width > sz.width || minsz.height > sz.height) { jd.setSize(Math.max(minsz.width, sz.width), Math.max(minsz.height, sz.height)); } jd.validate(); jd.repaint(); } } public class TextPanel extends javax.swing.JPanel { private javax.swing.JTextArea jTextArea1; public TextPanel() { initComponents(); } private void initComponents() { java.awt.GridBagConstraints gridBagConstraints; jTextArea1 = new javax.swing.JTextArea(); setLayout(new java.awt.GridBagLayout()); jTextArea1.setLineWrap(true); jTextArea1.setText("random text random text random text random text random text random text random text random text random text random text random text random text random text random text random text random text random text random text random text random text random text random text random text random text random text random text random text random text random text random text random text random text"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 0; gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 0); add(jTextArea1, gridBagConstraints); } } }