The short version: do I need to do something tricky to get JDialog's setMaximumSize() to work?
The full version: I've got a JDialog (layout manager: BorderLayout) which contains a scroll pane and a JPanel on the bottom with the commit buttons.
The scroll pane contains a JPanel which is built dynamically elsewhere in the system.
What I want is for the dialog to dynamically size itself around the JPanel up to a certain size, and then start growing scrollbars. This is, more or less, what happens by default, except the maximum size seems to be the size of my monitor.
I thought this is what the .setMaximumSize() method inherited from java.awt.Component did, but setting it doesn't seem to have any effect.
Setting the preferred size does has an effect - but then the dialog is always that size no matter what, which really isn't what I want.
(And the effects are the same if I set the maximum/preferred size properties on the scroll pane.)
Have I missed something tremendously obvious? Is there some wacky JDialog / BorderLayout / MaximumSize interaction I don't know about?
As discussed in Sizing a Scroll Pane, some components can provide useful information about setting the viewport's preferred size. The setVisibleRowCount()
method of JList
is particularly convenient, but even getViewport().setPreferredSize(…)
may suffice. Naturally, an sscce would help.
Addendum: As a concrete example, the dialog below is initially sized to have N-2 rows. As more are added, the dialog grows until the number reaches N. At that point the scrollbars start "growing". The example uses a JList
, but any Scrollable
component should be adaptable.
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
/** @see http://stackoverflow.com/questions/5759131 */
public class ListDialog {
private static final int N = 12;
private JDialog dlg = new JDialog();
private DefaultListModel model = new DefaultListModel();
private JList list = new JList(model);
private JScrollPane sp = new JScrollPane(list);
private int count;
public ListDialog() {
JPanel panel = new JPanel();
panel.add(new JButton(new AbstractAction("Add") {
@Override
public void actionPerformed(ActionEvent e) {
append();
if (count <= N) {
list.setVisibleRowCount(count);
dlg.pack();
}
}
}));
for (int i = 0; i < N - 2; i++) {
this.append();
}
list.setVisibleRowCount(N - 2);
dlg.add(sp, BorderLayout.CENTER);
dlg.add(panel, BorderLayout.SOUTH);
dlg.pack();
dlg.setLocationRelativeTo(null);
dlg.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dlg.setVisible(true);
}
private void append() {
model.addElement("String " + String.valueOf(++count));
list.ensureIndexIsVisible(count - 1);
}
public static void main(String[] a_args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
ListDialog pd = new ListDialog();
}
});
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With