Results 1 to 8 of 8
  1. #1
    Valhallon is offline Member
    Join Date
    Sep 2009
    Posts
    8
    Rep Power
    0

    Question setMaximumSize() on a JFrame problem

    Hi, I will just post some simple code to explain.

    JFrame myFrame = new MainGrid();
    JFrame.setDefaultLookAndFeelDecorated(true);
    myFrame.setMinimumSize(new Dimension(800,550));//Works
    myFrame.setMaximumSize(new Dimension(1000,750));//Doesnīt work

    The method setMininumSize works perfectly on my GUI, but then how come that I canīt set the maximum size of my window? It looks like it ignores the code.

    Ty, Val.

  2. #2
    Hugo is offline Member
    Join Date
    Oct 2009
    Posts
    16
    Rep Power
    0

    Default Making JFrame Maximum Size

    The following might help you solve the problem:

    Java Code:
    public class Program extends JFrame
    {
       public Program()
       {
          Rectangle maxBounds = GraphicsEnvironment.getLocalGraphicsEnvironment().
            getMaximumWindowBounds();
    
            this.setSize(maxBounds.width, maxBounds.height);
       }//end of constructor
    }//end of class Program
    Good Luck

  3. #3
    Valhallon is offline Member
    Join Date
    Sep 2009
    Posts
    8
    Rep Power
    0

    Default

    Iīve tried it, but no success.

    I mean, I dont want my JFrame to get bigger than 1000x750.

  4. #4
    Hugo is offline Member
    Join Date
    Oct 2009
    Posts
    16
    Rep Power
    0

    Default About JFrame Resizing

    Hi Valhallon

    I think I understand what your'e trying to get at now.

    The following is a quote from a different forum. I tried to paste the links to it, but unfortunately java-forums.org doesn't allow me to post hyperlinks until I have 20 posts or greater.

    Try to Google: Java Programming - Re: JFrame maximum size

    ...and you will find the original post at forums.sun

    The real issue is that setMinimumSize is declared in Window whereas setMaximumSize is declared in Component. Thus the two methods do not have complementary connotations.

    Experimenting with workarounds posted on those bug report pages, I think this is the cleanest solution, even if it does involve using a painting method for something other than painting -- normally to be avoided. SSCCE:

    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Point;
    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;

    public class MaxSizeFrame {

    public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {

    @Override
    public void run() {
    new MaxSizeFrame().makeUI();
    }
    });
    }

    public void makeUI() {
    final JFrame frame = new JFrame("") {

    @Override
    public void paint(Graphics g) {
    Dimension d = getSize();
    Dimension m = getMaximumSize();
    boolean resize = d.width > m.width || d.height > m.height;
    d.width = Math.min(m.width, d.width);
    d.height = Math.min(m.height, d.height);
    if (resize) {
    Point p = getLocation();
    setVisible(false);
    setSize(d);
    setLocation(p);
    setVisible(true);
    }
    super.paint(g);
    }
    };
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setSize(400, 400);
    frame.setMaximumSize(new Dimension(500, 500));
    frame.setMinimumSize(new Dimension(300, 300));
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    }
    }

    This way, instead of repeated flickering, there is just one flicker as the specified maximum size is crossed.

    db
    Try it and see if it helps...

  5. #5
    Valhallon is offline Member
    Join Date
    Sep 2009
    Posts
    8
    Rep Power
    0

    Default

    Yea Iīve tested that code in a separate code and it can solve the problem.

    My only doubt at the moment is how to implement that in my frame. I mean the code alone works the way I want, but after I use it in my program it does not. Maybe Iīm using the code wrongly I dunno, hereīs the code:

    @Override
    public void run() {
    new MainGrid().makeUI();//My frame, produces my Window
    }//without any max or min sizes.
    });
    }

    public void makeUI() {
    final JFrame frame = new JFrame("") {//Procudes another random

  6. #6
    Hugo is offline Member
    Join Date
    Oct 2009
    Posts
    16
    Rep Power
    0

    Default JFrame Resize Problem

    Hi Valhallon

    Yes. I have tested that piece of code as well and it seems to be working fine. I wish I could look more into the problem and of course it is hard if I can't see more of the source code to elaborate on the problem (but it's ok) On the other hand there are just a few things I need to know.

    The MainGrid class, is it extending JFrame? (maybe a silly question, but still need to make sure, because a class could extend JDialog as well)

    Are you calling any methods in the MainGrid class that might affect the instantiation of the MainGrid instance? For e.g. is there not a chance that you could have tried to call a setSize() method or something that could affect the way the program is reacting?

    Quoting the last lines on your post:

    public void makeUI() {
    final JFrame frame = new JFrame("") {//Procudes another random
    Have you included all the source code following the last left brace?

    This is important, because it is required for the program in order to function properly. From experience I can tell that the code above should produce a compile-time error.

    Also something that you need to take into consideration is that you will probably need to move all the source code from your MainGrid class to the "final JFrame" part above, because that will be the new frame on which all of your code will reside.

    This is just an example on what I tried to do with the makeUI() method. I still admit that the source code wasn't originally developed by myself, but I want to use it as an example to try and help you reach some sort of possible solution, so here goes:

    I would like you to see what I tried to do with the textField. The textField was placed successfully on the frame and the code still worked fine.

    Java Code:
    public void makeUI() {
    
            [B]JTextField txt1 = new JTextField(5);[/B]
    
            final JFrame frame = new JFrame("") {
    
                @Override
                public void paint(Graphics g) {
                    Dimension d = getSize();
                    Dimension m = getMaximumSize();
                    boolean resize = d.width > m.width || d.height > m.height;
                    d.width = Math.min(m.width, d.width);
                    d.height = Math.min(m.height, d.height);
                    if (resize) {
                        Point p = getLocation();
                        setVisible(false);
                        setSize(d);
                        setLocation(p);
                        setVisible(true);
                    }
                    super.paint(g);
                }
            };
    
            [B]frame.setLayout(new FlowLayout());[/B]
    
            [B]frame.add(txt1);[/B]
    
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(400, 400);
            frame.setMaximumSize(new Dimension(500, 500));
            frame.setMinimumSize(new Dimension(300, 300));
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    One last thing. Even the best software developer out there knows that debugging is a valuable tool when it comes to these problems. If you really get stuck and don't want to post your source code on forums (and I respect that), debug every line of code and really try to understand what every line of source code in your program tries to accomplish.

    I really hope this comment was useful.

    Good luck!
    Last edited by Hugo; 10-09-2009 at 03:22 PM.

  7. #7
    Valhallon is offline Member
    Join Date
    Sep 2009
    Posts
    8
    Rep Power
    0

    Default

    Hi Hugo! Sorry for my delayed answer, I only saw your reply today heh.

    Iīve tried your last code again and now its working 100% fine :)

    I figured that I was making 1 big error. Instead of initializing a normal JFrame like this:
    final JFrame frame = new JFrame("") {

    I should have put my own class: final MainGrid frame = new MainGrid() {

    Thats why it was creating 2 JFrames instead of my main one.

    About my code, I would have posted it all, but my main class is HUGE lool so I decided just to put some simple code to show the real problem. About debugging, thatīs another problem. My program implements like 4 or 5 listeners, so I canīt debugg it properly. Just one simple action like moving my mouse will cause it to run some "infinite" code without even any breakpoints.

    Finally, just wanna thank your help and sorry once again for the delayed answer.

    Cumps, VaL.

  8. #8
    Hugo is offline Member
    Join Date
    Oct 2009
    Posts
    16
    Rep Power
    0

    Default JFrame Resize Problem

    Hi Valhallon

    I'm glad you fixed the problem.

    Good luck with your project! :)

Similar Threads

  1. JFrame resizing problem
    By Bluefox815 in forum AWT / Swing
    Replies: 7
    Last Post: 02-24-2011, 08:56 PM
  2. Problem-----JFrame
    By HariPrasad in forum AWT / Swing
    Replies: 2
    Last Post: 07-28-2009, 01:34 PM
  3. Simple JFrame Problem
    By DC1 in forum New To Java
    Replies: 4
    Last Post: 06-06-2008, 08:09 AM
  4. JFrame problem
    By vassil_zorev in forum AWT / Swing
    Replies: 1
    Last Post: 01-25-2008, 03:53 AM
  5. JFrame problem
    By saytri in forum New To Java
    Replies: 6
    Last Post: 01-11-2008, 06:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •