🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #1772

Who uses Component.getMinimumSize() ?

#1772
From: Adam Gawne-Cain
Date: Sat, 19 Jul 2003 01:39
60 lines
2594 bytes
I want to enforce a minimum size on my dialogs.  But when I override
Component.getMinimumSize() the user can still resize my dialogs to just the
title bar.

I have programmed a SizeEnforcer (see code sample below), but it is not
elegant.  I am worried that it could cause infinite loops in a fight with
other listeners.

It there a way to get Swing to use getMinimumSize() earlier, eg while the
user is dragging resizable Dialog borders?

Thanks for any advice,
Adam.

**********************

package test;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class SizeEnforcer extends ComponentAdapter {
  public void componentResized(ComponentEvent e) {
    Component c=e.getComponent();
    Dimension d1=c.getMinimumSize();
    Dimension d2=c.getSize();
    Dimension d3=c.getMaximumSize();
    int w=Math.max(d1.width,Math.min(d2.width,d3.width));
    int h=Math.max(d1.height,Math.min(d2.height,d3.height));
    if (w!=d2.width || h!=d2.height)
      c.setSize(w,h);
  }
}

public class Test {
  public static void main(String[] args) {
    final JDialog dlg=new JDialog((JFrame)null,"Test",true) {
      public Dimension getPreferredSize() {
        return new Dimension(256,192);
      }
      public Dimension getMinimumSize() {
        return new Dimension(128,128);
      }
      public Dimension getMaximumSize() {
        return new Dimension(512,512);
      }
    };
    dlg.pack();
    dlg.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });

    dlg.addComponentListener(new SizeEnforcer());
    dlg.show();
  }
}
Technical Details
Message-ID: <3f18a131@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-09!supernews.com!newshosting.com!news-xfer1.atl.newshosting.com!newsfeed.wirehub.nl!news.kweb.on.ca!KWeb!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 111d45,9345c95a2a30a614,start
X-Google-Attributes: gid111d45,public
X-Google-ArrivalTime: 2003-07-18 18:41:26 PST
From: "Adam Gawne-Cain" <adam@gawnecainresearch.co.uk>
Newsgroups: borland.public.jbuilder.java.swing
Subject: Who uses Component.getMinimumSize() ?
Date: Sat, 19 Jul 2003 10:39:28 +0900
Lines: 62
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
NNTP-Posting-Host: 220.97.1.44
Message-ID: <3f18a131@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1058578737 220.97.1.44 (18 Jul 2003 18:38:57 -0700)
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-09!supernews.com!newshosting.com!news-xfer1.atl.newshosting.com!newsfeed.wirehub.nl!news.kweb.on.ca!KWeb!newsgroups.borland.com!not-for-mail