JFrame maximum size

Hi! I'm using a JFrame in a game I'm developing, and this should be resizeable within some bounds. This works fine for the minimum size, the user is unable to even try to resize the frame smaller than what I've set as the minimum size. But it seems like maximum size is a different issue - I can't find a way to bound how large the user may expand the frame. Of course, when it's extended to a larger size than my specified maximum, it's resized back to the maximum size (using a ComponentListener to detect resize attempts - this has to be done because I need a constant aspect ratio - and a maximum size). But I don't like this solution very much, I would rather find it impossible to even try to extend the frame larger than the bounds - in the same way one can set the bounds for the minimum size. Please help me!
Any suggestions anyone?

See [http://forums.sun.com/thread.jspa?threadID=5342801]
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_CLOSE);
      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

Similar Messages

  • Problem with the JFrame maximum size

    Dear all,
    I need to make my application expand to the maximum screen size when
    the user first starts it. I am using hte following code:
    frame.setSize(getMaximumSize());          
    however if my JFrame has a border layout and I have added a component at the page end
    "add(new JLabel("hello"), BorderLayout.PAGE_END);"
    this component is not displayed on the screen until I maximize the JFrame manually by clicking on
    the default "Maximize" button at the right top corner?
    Does anyone know how to get the JFrame occupy the maximum screen size where all of the
    components would be visible?
    Kanita

    Solved:
    http://forum.java.sun.com/thread.jsp?forum=57&thread=546044

  • How Do You Set the Maximum Size of a JFrame?

    Can someone show me a quick example? The JFrame can be empty, I just want to know how to set it's maximum size. I tried using setMaximumSize() and it doesn't work for some reason but setMinimumSize() works fine.
    I tried it using a BoxLayout and even using null as a layout manager and can't get it to work. I heard that the other layout managers don't allow you to set the maximum size of a JFrame so that is why I tried it with the layouts that I did.
    I tried the following code (imported the necessary classes), but it does not work...
    public class MyFrame extends JFrame
        public MyFrame()
            super("TestFrame");
            setLayout( new BoxLayout(getContentPane(), BoxLayout.X_AXIS)  );       
            setMaximumSize( new Dimension(400,200));
            setSize(300,150);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setVisible( true );                
    }// end class

    Reposted from
    {color:#0000ff}http://forum.java.sun.com/thread.jspa?threadID=5236259{color}
    @OP:
    When you repost a topic, please always provide a link to the previous post so people can check what suggestions were already offered (and didn't solve the problem).
    Better still, give a short summary of the advice you have already tried.
    I suggested setBackground(...) so you can see whether your MyFrame is actually occupying the entire real estate of the window. Looks as if trying it out (which I would have done for you if I'd been at home, where my JDK is) would have been too much effort for you.
    I'm pretty much a novice in Swing, but I can tell you this: setLayout controls the layout of components added to the JFrame, not the layout of the frame with respect to its parent container.
    Luck, Darryl

  • How Do You Set a JFrame's Maximum Size?

    Can someone show me a quick example? The JFrame can be empty, I just want to know how to set it's maximum size. I tried using setMaximumSize() and it doesn't work for some reason but setMinimumSize() works fine.
    I tried it using a BoxLayout and even using null as a layout manager and can't get it to work. I heard that the other layout managers don't allow you to set the maximum size of a JFrame so that is why I tried it with the layouts that I did.

    Darryl.Burke wrote:
    Are you displaying your MyFrame as a top-level window or adding it to a parent JFrame with null or BoxLayout set?
    Maybe the method you're looking for is setMaximizedBounds(Rectangle bounds)
    You could also try this: setBackground(...) to something other than the default background and run your program again.
    I'm at work and can't try ou any of my suggestions, so please post back if you get any progress.
    dbsetMaximizedBounds is a little better but not really what I was looking for. I can still make the window go passed the set boundaries when the window hasn't been maximized.
    Also, I checked that method setBackground, that only seems to change the color, is that the same method you were referring to?

  • How to determine the maximum size of JFrame?

    I am using JDK1.2 to develop an application. I have
    a problem to handle the maximum size of of JFrame.
    If the user clicks the maximum icon or double-clicks
    the title bar from the JFrame, the JFrame becomes
    maximum. How can I determine the state of maximization? I need to bring different views if the
    JFrame is maximum or minimum. I knew JDK1.4 can
    handle this problem. Or using JNI, either. Does
    anyone know other ways to handle this?
    Thanks,
    Peter

    So that you won't have to wait forever:
    Sorry, pal, the only way is JNI. I researched this earlier for 1.2, but can't find it right now in the database. They finally added it to 1.4.
    Good luck.

  • How to set an empty JPanel to maximum size?

    Hello!
    I created class extends JFrame.
    I want this maximum size so I set the minimum size as screen size:
    setSize(screenWidth,screenHeight);
    Now I added an empty JPanel,
    and I want it to be the maximum size.
    But the problem is - I don't know what is the maximum of the current size without the frame borders. I mean, after set it to maximum size (I don't know how),
    I could get the width and height of the current size without the frame borders by:
    frame.getContentPane().getSize();
    In conclusion, I want to set an empty JPanel to maximum size and get JFrame size without the frame borders.
    Thanks!

    800512 wrote:
    Now I added an empty JPanel,
    and I want it to be the maximum size.Define "maximum size". You want it to fill the screen or you want it to be the maximum size inside its container (JFrame in this case)?
    You cannot make a component break out of its container, that's for sure. So assuming you want the component to grow to the maximum size inside its container, that should already happen when you add the component to the CENTER position of the JFrame's container. By default it has a BorderLayout.

  • GUI - Set Maximum Size

    Hello,
    I tried setting the maximum size of the screen using this:
    WelcomeScreen screen2 = new WelcomeScreen();
                             screen2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                             //screen1.setSize(550, 100);
                             screen2.setSize(screen2.getMaximumSize());But it is bigger than the screen, not by much, but still bigger and also, the maximize icon is present (normally when I window is fully maxed it will appear as a restore icon)
    Please help!

    Still posting in the wrong forum.
    Do you not understand the concept of actually searching a forum "BEFORE" posting a question?
    This question is asked and answered all the time in the Swing forum. So search there. If you don't find your answer then you post your question there so that people who actually do search the forum first can benefit from the answer given.

  • Can't change Canvas's Maximum Size

    Hi, I am trying to make a program which has a frame which is fullscreen and a canvas which is the same size.
    So far I have got this:
    JFrame frame = new JFrame("Screensaver");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Toolkit tk = Toolkit.getDefaultToolkit(); 
    screenWidth = ((int) tk.getScreenSize().getWidth()); 
    screenHeight = ((int) tk.getScreenSize().getHeight()); 
    frame.setSize(screenWidth, screenHeight); 
    frame.setUndecorated(true);
    frame.setResizable(false);
    Canvas canvas = new Canvas();
    Dimension screenSize = new Dimension(screenWidth, screenHeight);
    canvas.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { System.exit(0); } });
    frame.getContentPane().add(canvas, BorderLayout.CENTER);          
    frame.setVisible(true);
    canvas.setMaximumSize(screenSize);
    canvas.setSize(screenSize);Note: At the beginning of the class is:
    public int screenWidth.;
    public int screenHeight;But the canvas still won't draw larger than the default maximumSize.
    As soon as I change the maximumSize, and then look at it again, it's gone back to the default!
    Anyone have any idea why?

    Do you want a [full screen exlcusive|http://java.sun.com/docs/books/tutorial/extra/fullscreen/index.html] window or just a window that happens to be the size of the desktop (minus the area of the taskbar)?
    But the canvas still won't draw larger than the default maximumSize.I'm not sure how to interpret this statement. You've set the maximum size to the the desktop. So are you saying you can't draw beyond the desktop? Are you using canvas.getGraphics() by any chance?
    As soon as I change the maximumSize, and then look at it again, it's gone back to the default!The canvas is added to the content pane in the BorderLayout.CENTER region. And it's the only component in there. Hence, it will be the same size as the content pane (which is the same size as the frame) regardless of what size or preferred size you may set on it. This is to be expected though. BorderLayout is allowed to resize the center component to take up the extra space.

  • Set Maximum size for a subpanel created with splitter bars in LV8

    Does anyone know how to set the Maximum size for a subpanel created with asplitter bar? If I show one cluster in the subpanel and use the scroll bar to scroll, I can see more (blank) area than I want. I just want the user to be able to scroll a cluster within a subpanel while the cluster is larger than the view area of the subpanel. I don't want to let the user scroll outside the cluster.
    Ravi Beniwal

    I apologize for the mixup in terminology. Please read Pane wherever the word Subpanel was used.
    Please see the attached VI for a description of my problem.
    I would really appreciate any help with this.
    Ravi Beniwal
    Attachments:
    Pane Scrolling Problem.vi ‏10 KB

  • Set maximum size in Text Form Field Options for a field in bi publisher RTF

    Hi All,
    How to set maximum size in Text Form Field Options for a field in bi publisher RTF.
    I have a RTF whch is having a field in that i need to add some validation condition but after adding certain condition in Add help text tab ,it is not accepting after certain length, how i can increase the length to unlimited,please help me on this
    Thnaks

    Form fields have some restrictions if your are using version lower than 11g.
    They can accommodate only 393 chars. You can add the text in both status bar and help key, which can in total consume 393 chars.
    If your code logic is more than that, it can be split into multiple form fields as Avinash suggested or you can use sub template logic and handle coding over there. Again in sub template code can be within/outside form fields.
    So there is no option for user to increase the size of form field.

  • SharePoint Foundation 2010 maximum size WID

    Hi,
    I've installed SharePoint Foundation 2010 using the Windows Internal Database. I'm a bit confused about the maximum size of the database. On several websites I read that the max size for an SQL express database is 4GB.
    Is this the same as the Windows Internal Database? Or can it grow bigger?

    Are you sure you mean the Windows Internal Database? The SharePoint foundation installer will install SQL Server Express edition.

  • Windows Server 2008 R2 Security Event Log Maximum Size

    I have a customer with logging requirements on domain controllers that are exceeding the maximum log size they have configured for the security log.  When they attempted to increase the maximum size of the security event log via Group Policy, the settings
    did not take effect.  When an attempt was made to increase the security event log manually on the domain controller via the properties of the log, an error is generated whenever the value was changed.
    The Maximum Log Size specified is not valid.  It is too large or too small. The Maximum Log Size will be set to the following: 196608 KB
    The 196608 KB value is the value that it is currently set at.  Testing on other logs, application, system, has lead to the same result.  
    wevtutil.exe sl security /ms:<n> produces similar results.  There is no error message given but the value doesn't change when you run wevtutil.exe gl security
    When viewing the registry value MaxSize under HKLM\Current Control Set\Services\EventLog\Security the change is reflected, but the log does not seem to get any larger.  
    What one would expect to be a two minute change in a group policy object has turned into something much more difficult.  Any idea what could be causing this?
    Joseph M. Durnal MCM: Exchange 2010 MCITP: Enterprise Messaging Administrator, Exchange 2010 MCITP: Enterprise Messaging Administrator, MCITP: Enterprise Administrator

    I verified that it was not another policy - the domain is pretty simple without many policies, only policies applied are:
    Default Domain Policy (no event log settings)
    Company Domain Policy (no event log settings)
    Default Domain Controller Policy (no event logs settings)
    Company Domain Controller Policy (...\Event Log\Maximum security log size 4194240 kilobytes)
    The value was 196608 before, the plan was to change the group policy setting to 4194240 and I expected it to be that easy.  However, the values didn't change.
    4194240 is divisible by 64
    Used multiple tools to try and change
    Group Policy
    Event Viewer
    wevtutil.exe
    registry editor
    While some of the methods display a larger event log, the actual size of the event log still seems to be limited to 196608 kb.  
    Thanks,
    Joe
    Joseph M. Durnal MCM: Exchange 2010 MCITP: Enterprise Messaging Administrator, Exchange 2010 MCITP: Enterprise Messaging Administrator, MCITP: Enterprise Administrator

  • How can i increase the maximum size of a text column in Access

    hello,
    i am inserting data in the MS-Access database through my servlet and am getting the exception whenever the length of the text data is more than 255 characters as in Access the maximum size for character data column is 255 can anyone tell me if there is any way by which i can increase the maximum limit of the column size in Access ,i.e. to make it more than 255.

    A text field in Access has a maximum size of 255 characters. If you want to store text information in Access larger than 255, you need to use the memo rather than text data type. There are special considerations for using an Access memo data type and accessing it through JDBC. Specifically, I believe it must be accessed as a binary object (BLOB), rather than text when using the JDBC-ODBC bridge. There are lots of discussions within these forums regarding how to manage, and the issues with using the Access memo data type.
    Good luck!

  • I want to set a maximum size for pictures when FF shows websites. How can i do that? NO I DONT MEAN "HOW TO ZOOM".

    I have FF 34 but this problem has been there for a lot of earlier versions. On many webpages there are pictures that cover my whole screen, and I want to set maximum size for showing images on webpages.
    I ONLY wanr to change the maximum size of the images, AND IAM NOT asking about how to zoom the webpage.

    Do you want to hide large images or merely make them smaller by setting a max-width and max-height?
    You would probably have to use JavaScript (bookmarklet) to achieve this reliably.
    There are bookmarklets to make large images fit in the window to avoid a horizontal scroll bar
    You can try this code in the <b>userContent.css</b> file (adjust the max-width accordingly).
    *http://kb.mozillazine.org/userContent.css
    <pre><nowiki>img { max-width: 1024px !important; height: auto !important; }</nowiki></pre>
    The customization files userChrome.css (user interface) and userContent.css (websites) are located in the <b>chrome</b> folder in the Firefox profile folder.
    *http://kb.mozillazine.org/Editing_configuration
    *Create the chrome folder (lowercase) in the <xxxxxxxx>.default profile folder if this folder doesn't exist
    *Use a plain text editor like Notepad to create a (new) userContent.css file in the chrome folder (file name is case sensitive)
    *Paste the code in the userContent.css file in the editor window
    *Make sure that you select "All files" and not "Text files" when you save the file via "Save file as" in the text editor as userContent.css.<br>Otherwise Windows may add a hidden .txt file extension and you end up with a not working userContent.css.txt file

  • Maximum Size of Hard Drive in Mac Pro (Early 2008)

    What is the Maximum size of Hard Drive that I can install in drive bay 3 or 4?
    Computer info below:
    Model Name: Mac Pro (Early 2008)
    Model Identifier: MacPro3,1
    Processor Name: Quad-Core Intel Xeon
    Processor Speed: 2.8 GHz
    Number Of Processors: 2
    Total Number Of Cores: 8
    L2 Cache (per processor): 12 MB
    Memory: 14 GB
    Bus Speed: 1.6 GHz
    Boot ROM Version: MP31.006C.B05
    SMC Version (system): 1.25f4
    Hardware UUID: FF121465-84A1-52F2-A94A-58A60A20F4AF

    WE don't know, all there are today are 2TB and those are fine. I think you could RAID 6 for 10TB and maybe then?
    Serious, there are none. Do you really expect to hit some ceiling?
    http://forums.xlr8yourmac.com/drivedb/search.drivedb.lasso

Maybe you are looking for