Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / May 2008

Tip: Looking for answers? Try searching our database.

JFrame resizing

Thread view: 
Stephan Lukits - 28 May 2008 12:34 GMT
Hello Group,
why doesn't the following code work?

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

class Test extends JFrame {

    public void paint( Graphics g ) {
    if ( getSize().width != 200 || getSize().height != 100 ) {
       System.out.println("Frame Size is different");
       setSize( 200, 100 );
    }
    g.drawString( "A test string", 50, 75);
    }

    public static void main( String args[] ) {
    JFrame f = new Test();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
    f.setLayout( null );
    f.setVisible( true );
    }
}

I'd expect it to change the frames' size after the user has
resized the frame.  But that happen only occasionally.
(I actually want to make sure, that the frams' size dosen't
 go below a specific size).

cat /proc/version:
Linux version 2.6.21.4-eeepc (root@LinuxPC) (gcc version 4.1.2 20061115
(prerelease) (Debian 4.1.1-21)) #5 Thu Nov 8 17:17:35 UTC 2007

java -version:
java version "1.6.0_05"
Java(TM) SE Runtime Environment (build 1.6.0_05-b13)
Java HotSpot(TM) Client VM (build 10.0-b19, mixed mode)

regards
Stephan
Kenneth P. Turvey - 28 May 2008 13:00 GMT
>      public void paint( Graphics g ) {
>     if ( getSize().width != 200 || getSize().height != 100 ) {
[quoted text clipped - 3 lines]
>     g.drawString( "A test string", 50, 75);
>      }

Ok, I can't really tell you why this isn't working, but the paint method
is the wrong place for this kind of code.  You should put it in a
ComponentListener.

Signature

Kenneth P. Turvey <kt-usenet@squeakydolphin.com>
http://www.electricsenator.net

 In the next place, the soul does violence to itself when it turns away
 from any man, or even moves towards him with the intention of injuring,
 such as are the souls of those who are angry.
       -- Marcus Aurelius Antoninus, Meditations 2-16

Stephan Lukits - 28 May 2008 13:38 GMT
Kenneth P. Turvey schrieb:
> [...]
>> [...]
> Ok, I can't really tell you why this isn't working, but the paint method
> is the wrong place for this kind of code.  You should put it in a
> ComponentListener.

Thanks for your answer, but that dosen't work either:

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

class Test extends JFrame {
    Test() {
    addComponentListener(new ComponentAdapter() {
        public void componentResized(ComponentEvent e) {
           if ( getSize().width != 200 || getSize().height != 100 ) {
            System.out.println("Resize to 200x100");
            setSize( 200, 100 );
           }
        }
       });
    }
    public void paint( Graphics g ) {
    g.drawString( "A test string", 50, 75);
    }

    public static void main( String args[] ) {
    JFrame f = new Test();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setLayout( null );
    f.setSize( 200, 100 );
    f.setVisible( true );
    }
}

regards
Stephan
Kenneth P. Turvey - 28 May 2008 13:47 GMT
> Kenneth P. Turvey schrieb:
>> [...]
[quoted text clipped - 4 lines]
>
> Thanks for your answer, but that dosen't work either:

The code you posted works great here.  What is it doing or not doing that
you don't like?

Signature

Kenneth P. Turvey <kt-usenet@squeakydolphin.com>
http://www.electricsenator.net

 A distributed system is one in which the failure of a computer you
 didn't even know existed can render your own computer unusable.
    -- Les Lamport

Stephan Lukits - 28 May 2008 14:16 GMT
Kenneth P. Turvey schrieb:

>> Kenneth P. Turvey schrieb:
>>> [...]
[quoted text clipped - 6 lines]
> The code you posted works great here.  What is it doing or not doing that
> you don't like?

Thanks again for your effort.  It just doesn't snap back!
Precise, it does it only occasionally. It does:
"System.out.println("Resize to 200x100");" as I can
see in my console window, (thus it is in the if branch)
but it dosen't do the resizing.  Debugging tells my that
the frames' size attribute is as desired but the actual
window isn't resized???

regards
Stephan
Kenneth P. Turvey - 28 May 2008 13:52 GMT
> Kenneth P. Turvey schrieb:
>> [...]
[quoted text clipped - 4 lines]
>
> Thanks for your answer, but that dosen't work either:

Actually, I think I might know what's bothering you.   I'm guessing you
don't want the frame to resize and then snap back to the size you
specify.  You just want it to stay the size you specify without any of
this snapping back stuff.  Basically you want to allow the frame to grow,
but not beyond a certain point.. or shrink, but not beyond a certain
point.

Unfortunately swing won't let you do these things.  The closest you're
going to get is having the frame snap back to an acceptable size.  I
don't know why Swing doesn't have a way to do what you want.  It is a
common request, but it doesn't.

Hopefully I'm just incorrect about what you really want and it is
something we can do something about.

Signature

Kenneth P. Turvey <kt-usenet@squeakydolphin.com>
http://www.electricsenator.net

 Unix gives you just enough rope to hang yourself -- and then a couple
 of more feet, just to be sure.
       -- Eric Allman

Andrew Thompson - 28 May 2008 14:03 GMT
On May 28, 9:34 pm, Stephan Lukits <stephan.luk...@fernuni-hagen.de>
wrote:
...
> (I actually want to make sure, that the frams' size dosen't
>   go below a specific size).

<sscce>
import javax.swing.*;
import java.awt.*;

class MinimumSizeFrame extends JFrame {

 public void paint(Graphics g) {
   super.paint(g);
   g.setColor( Color.BLACK );
   g.drawString(
     "WxH: " + getWidth() + "x" + getHeight(),
     20,
     40 );
 }

 public static void main(String[] args) {
   Runnable r = new Runnable() {
     public void run() {
       JFrame f = new MinimumSizeFrame();
       f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
       f.setMinimumSize( new Dimension(400,300) );
       f.pack();
       f.setLocationRelativeTo(null);
       f.setVisible(true);
     }
   };
   EventQueue.invokeLater(r);
 }
}
</sscce>

--
Andrew T.
PhySci.org
Kenneth P. Turvey - 28 May 2008 15:52 GMT
> On May 28, 9:34 pm, Stephan Lukits <stephan.luk...@fernuni-hagen.de>
> wrote:
> ...
>> (I actually want to make sure, that the frams' size dosen't
>>   go below a specific size).

I was absolutely certain that I'd run across this before and not been
able to make it work correctly.  Hmm.  Looks like Andrew solved the
problem.  

Signature

Kenneth P. Turvey <kt-usenet@squeakydolphin.com>
http://www.electricsenator.net

 To a rational being it is the same thing to act according to nature
 and according to reason.
       -- Marcus Aurelius Antoninus, Meditations

Andrew Thompson - 28 May 2008 16:15 GMT
On May 29, 12:52 am, "Kenneth P. Turvey" <kt-
use...@squeakydolphin.com> wrote:

Re. setMinimumSize()

> I was absolutely certain that I'd run across this before and not been
> able to make it work correctly.  

AFAI(V)R - there are a number of layouts that
either ignore it, or behave strangely, especially
when the GUI is resized to a size that is less than
that which allows the full component to be drawn.

That would not affect a root level component such
as a frame, dialog ..is window even relevant here?

Perhaps you were seeing that behaviour within a
layout?

--
Andrew T.
PhySci.org
Kenneth P. Turvey - 28 May 2008 17:52 GMT
> Perhaps you were seeing that behaviour within a layout?

Could be.  It was discussed here.

Signature

Kenneth P. Turvey <kt-usenet@squeakydolphin.com>
http://www.electricsenator.net

 I find it rather easy to portray a businessman.  Being bland, rather
 cruel and incompetent comes naturally to me.  
       -- John Cleese

Stephan Lukits - 29 May 2008 09:17 GMT
Andrew Thompson schrieb:
> On May 28, 9:34 pm, Stephan Lukits <stephan.luk...@fernuni-hagen.de>
> wrote:
[quoted text clipped - 5 lines]
> [...]
> </sscce>

Thanks for your answer to the above question.  This question
unfortunately is a simplification of what I actually want.
Resizing was an important part of the question.  Now I try
to be precise and short (excuse my english):

- I need a Frame where I can pixelwise draw to, thus no
  automatic layout manger/manging.

- There is a minimum frame size, a maximum Frame size
  and if the user resizes the frame there is a "best
  fitting" frame size due to the user resizing.  That
  means that the programm can't necessary full fill the
  users desired size but it tries to be as close as
  possible.
  The minimum and maximum frame size can change while
  using the software caused by changing the user settings
  (or changing the screen resolution).

Thus my paint method checks whether the resolution
has changed, whether the frame size has changed,
or whether the user settings have changed, since
the last call of paint.  If so the gui is newly
calculated which leads to a frame size that needs
to be set.  How can I set it?  I belief that I can't
calculate the needed size  outside the paint method,
since I need a graphic context to do so?

Thanks a lot for any further help.
regards
Stephan
Stephan Lukits - 29 May 2008 09:42 GMT
Stephan Lukits schrieb:
> Andrew Thompson schrieb:
>> On May 28, 9:34 pm, Stephan Lukits <stephan.luk...@fernuni-hagen.de>
[quoted text clipped - 11 lines]
> Resizing was an important part of the question.  Now I try
> to be precise and short

This would be a simplified code example from what I have in mind:

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

class Test extends JFrame {
    public void paint( Graphics g ) {
        if ( isRecalculationNecessary() ) {
            setSize( calculateSize() );
            repaint();
            return;
        }
        g.drawString( "A test string", 50, 75);
    }

    boolean isRecalculationNecessary() { return true; }
    Dimension calculateSize() {
        return new Dimension( 200, 100 );
    }
    public static void main( String args[] ) {
        JFrame f = new Test();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLayout( null );
        f.setSize( 200, 100 );
        f.setVisible( true );
    }
}

But this code works at my side only the first time
and when I maximize the window, then it snaps back.

regards
Stephan
Stephan Lukits - 29 May 2008 09:56 GMT
Stephan Lukits schrieb:
> Andrew Thompson schrieb:
>> On May 28, 9:34 pm, Stephan Lukits <stephan.luk...@fernuni-hagen.de>
[quoted text clipped - 11 lines]
> Resizing was an important part of the question.  Now I try
> to be precise and short (excuse my english):

This code is a simplified version of what I have in mind:

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

class Test extends JFrame {
    public void paint( Graphics g ) {
        if ( isRecalculationNecessary() ) {
            setSize( calculateSize() );
            repaint();
            return;
        }
        g.drawString( "A test string", 50, 75);
    }

    boolean isRecalculationNecessary() {
        return (getSize().width != 200) || (getSize().height != 100);
    }
    Dimension calculateSize() {
    return new Dimension( 200, 100 );
    }
    public static void main( String args[] ) {
    JFrame f = new Test();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setLayout( null );
    f.setSize( 200, 100 );
    f.setVisible( true );
    }
}

It works only occasionally at resizing and always at maximizing.

regards
Stephan
Stephan Lukits - 29 May 2008 11:46 GMT
Stephan Lukits schrieb:
> [...]
> This code is a simplified version of what I have in mind:
[quoted text clipped - 28 lines]
>
> It works only occasionally at resizing and always at maximizing.

On an other box it works perfectly well!  Did I find a Java-GUI Bug?

regards
Stephan
Roedy Green - 29 May 2008 01:33 GMT
On Wed, 28 May 2008 13:34:27 +0200, Stephan Lukits
<stephan.lukits@fernuni-hagen.de> wrote, quoted or indirectly quoted
someone who said :

>if ( getSize().width != 200 || getSize().height != 100 ) {
>       System.out.println("Frame Size is different");
>       setSize( 200, 100 );

setSize should be done before you call paint.  by then the Graphics
object is nailed down -- where its bits exist and how big in the regen
buffer.
Signature


Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Stephan Lukits - 29 May 2008 08:55 GMT
Roedy Green schrieb:
> On Wed, 28 May 2008 13:34:27 +0200, Stephan Lukits
> <stephan.lukits@fernuni-hagen.de> wrote, quoted or indirectly quoted
[quoted text clipped - 7 lines]
> object is nailed down -- where its bits exist and how big in the regen
> buffer.

Hm, wouldn't the code, placed into "componentResized(ComponentEvent e)"
event handling, be on a place before I call paint?

regards
Stephan
Qu0ll - 29 May 2008 06:47 GMT
> Hello Group,
> why doesn't the following code work?
[quoted text clipped - 32 lines]
> Java(TM) SE Runtime Environment (build 1.6.0_05-b13)
> Java HotSpot(TM) Client VM (build 10.0-b19, mixed mode)

Also on the subject of JFrame resizing, why is it that I can restrict the
minimum size of a JFrame using setMinimumSize() but I cannot restrict the
maximum size using setMaximumSize()?  The latter appears to have no effect
on the sizing constraints of a JFrame.

Signature

And loving it,

-Qu0ll (Rare, not extinct)
_________________________________________________
Qu0llSixFour@gmail.com
[Replace the "SixFour" with numbers to email me]

Kenneth P. Turvey - 29 May 2008 09:13 GMT
[Snip]
> Also on the subject of JFrame resizing, why is it that I can restrict
> the minimum size of a JFrame using setMinimumSize() but I cannot
> restrict the maximum size using setMaximumSize()?  The latter appears to
> have no effect on the sizing constraints of a JFrame.

That's probably what I was thinking of.  You just can't get rid of that
snap back effect in trying to set a maximum size.  

At least I believe that's where we left it that last time this topic came
up.

Signature

Kenneth P. Turvey <kt-usenet@squeakydolphin.com>
http://www.electricsenator.net

 Duct tape is like the force.  It has a light side, and a dark side,
 and it holds the universe together ...
       -- Carl Zwanzig

Qu0ll - 29 May 2008 10:27 GMT
> [Snip]
>> Also on the subject of JFrame resizing, why is it that I can restrict
[quoted text clipped - 7 lines]
> At least I believe that's where we left it that last time this topic came
> up.

I am not actually referring to any snap-back effect.  I simply mean that if
you call setMaximumSize() on a JFrame then it will still be possible to
resize it larger than those dimensions or to maximise it larger.  What is
the point of having a setMaximumSize() method if it doesn't limit the
upsizing of that frame to those dimensions in the same way that
setMinimumSize() limits the downsizing?

Signature

And loving it,

-Qu0ll (Rare, not extinct)
_________________________________________________
Qu0llSixFour@gmail.com
[Replace the "SixFour" with numbers to email me]



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.