You want a JDialog to dynamically adjust its size based on the content of a contained JPanel, but even after setting the setMaximumSize property, the dialog still resizes beyond the specified maximum.
While setting the maximum size may seem sufficient, unconventional interactions between JDialog, BorderLayout, and MaximumSize can hinder its functionality. Instead, consider using the setVisibleRowCount method of JList or getViewport().setPreferredSize(...) to provide relevant information for setting the preferred size.
In the example below, a dialog adjusts its size to display a JList up to a maximum of N rows. When the Add button is clicked, rows are added to the list, causing the dialog to expand until it reaches the maximum size. At that point, scrollbars appear.
<code class="java">public class ListDialog { // ... private void append() { model.addElement("String " + String.valueOf(++count)); list.ensureIndexIsVisible(count - 1); if (count <= N) { list.setVisibleRowCount(count); dlg.pack(); } } // ... }</code>
The above is the detailed content of Resizing a JDialog: Why SetMaximumSize May Not Work?. For more information, please follow other related articles on the PHP Chinese website!