Home > Java > javaTutorial > body text

Resizing a JDialog: Why SetMaximumSize May Not Work?

DDD
Release: 2024-10-23 22:40:02
Original
251 people have browsed it

Resizing a JDialog: Why SetMaximumSize May Not Work?

Setting a JDialog's Maximum Size

Problem

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.

Solution

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.

Example

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>
Copy after login

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!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!