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 / General / October 2007

Tip: Looking for answers? Try searching our database.

Problem with basic JFrame layout manager

Thread view: 
Mark Space - 05 Oct 2007 00:18 GMT
Hey all, here's a problem I can't lick.

Using just a basic JFrame, it should have a basic BorderLayout layout
manager by default.  The docs strongly imply it, and it's manifest in
the code (source and runtime).

However, if I add my own component to the content pane, the JFrame when
displayed is as small as possible, only just large enough to display
it's decorations.  I implement all the size methods, and it still
doesn't seem to do anything with them.

Does anyone know what I might be doing wrong?

Sample code follows.  If I print out the classes involved (the first and
the second commented-out blocks) everything is as you'd expect.  A
BorderLayout is present by default.

If I manually set the size of the content pane (first and third
commented-out blocks), the display is correct.

/*
 * Captcha.java
 *
 * Created on Oct 3, 2007, 12:38:11 PM
 *
 */

package captcha.oak;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.LayoutManager;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import static javax.swing.WindowConstants.*;

public class Captcha extends java.awt.Component {

    private static final int WIDTH = 250;
    private static final int HEIGHT = 85;
    private BufferedImage image;

    /** Creates and displays a test captcha.
     *
     * @param args The command line arguments are ignored.
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Captcha c = new Captcha();
        JFrame jf = new JFrame("Captcha");

        jf.setDefaultCloseOperation(EXIT_ON_CLOSE);
        jf.getContentPane().add(c, BorderLayout.CENTER);

//        java.awt.Container cp = jf.getContentPane();

//        LayoutManager lm = cp.getLayout();
//        System.err.println( cp.getClass().getName()+":: "+cp);
//        System.err.println( lm.getClass().getName()+"::"+lm );
//        System.err.println( lm.getClass().getSuperclass().getName());

//        cp.setPreferredSize(c.getPerferredSize());

        jf.pack();

        jf.setVisible(true);
    }

    public Captcha() {

        int[] data = new int[WIDTH * HEIGHT];

        for (int i = 0; i < data.length; i++) {
            int red = randomInt(255);
            int green = randomInt(255);
            int blue = randomInt(255);
            data[i] = (red << 16) | (green << 8) | blue;
        }
        this.image = new BufferedImage(WIDTH, HEIGHT,
BufferedImage.TYPE_INT_RGB);
        image.setRGB(0, 0, WIDTH, HEIGHT, data, 0, WIDTH);
    }

    private int randomInt(int i) {
        return (int) (java.lang.Math.random()* i);
    }

    public void paint(Graphics g) {
        g.drawImage(image, 0, 0, this);
    }

    public Dimension getPerferredSize()
    {
        return getSize();
    }
    public Dimension getSize()
    {
        return new Dimension(WIDTH,HEIGHT);
    }

    public Dimension getMaximumSize()
    {
        return getSize();
    }

    public Dimension getMinimumSize()
    {
        return getSize();
    }
}
Eric Sosman - 05 Oct 2007 02:13 GMT
> Hey all, here's a problem I can't lick.
> [...]
> I implement all the size methods,  [...]

    Are you sure?

> [...]
>     public Dimension getPerferredSize()
                           ^^

    Are you *really* sure?

Signature

Eric Sosman
esosman@ieee-dot-org.invalid

Mark Space - 05 Oct 2007 03:23 GMT
>> Hey all, here's a problem I can't lick.
>> [...]
[quoted text clipped - 7 lines]
>
>     Are you *really* sure?

Thanks man.  Sometimes I just need a second pair of eyes...
Stefan Ram - 05 Oct 2007 03:38 GMT
>>>     public Dimension getPerferredSize()
>>                            ^^
>>     Are you *really* sure?
>Thanks man.  Sometimes I just need a second pair of eyes...

 Try

http://download.java.net/jdk7/docs/api/java/lang/Override.html
Mark Space - 05 Oct 2007 04:52 GMT
>>>>     public Dimension getPerferredSize()
>>>                            ^^
[quoted text clipped - 4 lines]
>
> http://download.java.net/jdk7/docs/api/java/lang/Override.html

That's a good idea too.  But because it was such a short little program,
I skipped them. Sometimes skipping the fundamentals just doesn't pay.


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.