setSize Bug or Feature in Frame class since 1.4?

I have a application/applet which is creating Frame’s of differnt size, some are 900 x 700 ( client size ), so they are a little bit bigger with the insets.

Problem is when the screen size is set to 800 x 600.

In my program as a application running under java 1.6.0_03 ( latest stable version available ) its running fine and creating a Frame of size 812 x 612 ( this is ok windows XP restricts frame to this size ).

When i run my program as a applet under MSIE 6.0 using java 1.6.0_03 as a plugin, the size of the Frame is the ScreenSize minus Screen Insets. setSize is not able to make my Frame bigger.

My Frame is resizable, when the user is making it bigger by dragging at the frame edges, the frame is resizing to 812 x 612

This is a strange and unwanted behaviour, is this a bug or a feature, any workaround known.

This strange behaviour does not happen with java 1.1 or with M$ java 1.1.4

I notice this strange frame behaviour in applets since version 1.4

Yes, this issue is well known, because the previous buffer sometimes overlaps with the new sized buffer. I recommend you to make resizing disabled but to change the resolution within a new instance of your Frame, it works 99% chances. The same issue can occur when switching to fullscreen on Mac OS X.
But what is a bit strange is that this issue shouldn’t occur on Windows 32 bits. Have you checked the values that specify the dimension of your graphics-buffer ? For instance, getSize() should be replaced with calls to getWidth() and getHeight() for each component (c.f. javadoc).

Here is a little Test Programm you may run it under 1.6.0_03 as application or as applet

TestApplet.java Class


/**************************************************************************************************
 *
 *************************************************************************************************/

import java.applet.*;

/**************************************************************************************************
 *
 *************************************************************************************************/
public class TestApplet extends Applet
{
    private final static long serialVersionUID = 654321; // keep the compiler happy

    /**********************************************************************************************
     *
     *********************************************************************************************/
    public void init()
    {
    }

    /**********************************************************************************************
     *
     *********************************************************************************************/
    public void start()
    {
        TestFrame.main(null);
    }

    /**********************************************************************************************
     *
     *********************************************************************************************/
    public void stop()
    {
    }

    /**********************************************************************************************
     *
     *********************************************************************************************/
    public void destroy()
    {
    }
}

/*************************************************************************************************/


/**************************************************************************************************
 *
 *************************************************************************************************/

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;

import javax.swing.*;

/**************************************************************************************************
 *
 *************************************************************************************************/
public class TestFrame extends Frame
{
    private final static long serialVersionUID = 123456; // keep the compiler happy

    private final static int WIDTH = 900;
    private final static int HEIGHT = 700;

    /**********************************************************************************************
     *
     *********************************************************************************************/
    static public void main(String[] args)
    {
        try
        {
            TestFrame aFrame = new TestFrame();

            aFrame.enableEvents(AWTEvent.COMPONENT_EVENT_MASK);
            aFrame.setBounds(0, 0, TestFrame.WIDTH, TestFrame.HEIGHT);
            aFrame.setLayout(null);
            aFrame.setResizable(false);
            aFrame.setTitle("TestFrame");
            aFrame.setBackground(Color.red);
            aFrame.setVisible(true);
        }
        catch ( Exception ex )
        {
            ex.printStackTrace();
        }
    }

    /**********************************************************************************************
     * is called when parts of the peer are damaged and needs redraw
     *********************************************************************************************/
    protected void processComponentEvent(ComponentEvent e)
    {
        if ( e.getID() == ComponentEvent.COMPONENT_SHOWN )
        {
            setMinimumSize(new Dimension(812, 612));
            setLocation(10, 10);
            setMinimumSize(null);
            System.out.println("COMPONENT_SHOWN");
        }
    }

    /**********************************************************************************************
     * is called when parts of the peer are damaged and needs redraw
     *********************************************************************************************/
    public void paint(Graphics gg)
    {
        gg.setColor(Color.blue);
        gg.fillRect(0, 0, TestFrame.WIDTH - 20, TestFrame.HEIGHT - 20);

        gg.setColor(Color.black);
        gg.drawString("Hello World :-)", 20, 50);

        gg.drawString(getWidth() + "x" + getHeight(), 20, 100);
    }

    /**********************************************************************************************
     * triggered by repaint
     *********************************************************************************************/
    public void update(Graphics gg)
    {
        paint(gg);
    }
}

/*************************************************************************************************/

First run it with the processComponentEvent method commented out and you will see under a 800x600 desktop the window size is screen size minus screen insets, now uncomment the processComponentEvent and the window size is bigger.

Is this a bug ? or a feature ?

I don’t see any bugs. Your code is correct but what you want to do with the processComponentEvent must be validated by re-packing again the Frame.

protected void processComponentEvent(ComponentEvent e)
    {
        if ( e.getID() == ComponentEvent.COMPONENT_SHOWN )
        {
            setMinimumSize(new Dimension(812, 612));
            setLocation(10, 10);
            setMinimumSize(null);
            pack();
            repaint();
            System.out.println("COMPONENT_SHOWN");
        }
    }

:slight_smile:

pack is obsolete, i have no LayoutManager, i use setSize instead of pack, i tried it with pack and preferedSize ( which muste be set when using pack, otherwise window is very very small :slight_smile:

same result problem is not pack or setSize, Problem is java is limiting setSize, when i make the Frame resizable and the user ( me ) is making the frame bigger, its working …

Workaround for Java 1.6 is using setMinimumSize which is forcing the window bigger :slight_smile:

I file this as bug and will post here the bug id when it is public available

yes, indeed setMininumSize() has been called twice. let the second call off and it should work. By the way, size cannot get under he preferredSize limit, but above. pack() is not obfuscating your layout, in fact it is null, but pack() has much more improvements to lay out the frame actually. resizing is much reflected if you want to display images. There’s no bug about that. Do you have a link for tthe bug you submitted so that I can have something to refer to?