Center Frame & Lock in Minimum Size

Before setting the main frame visible, many (if not most) Swing applications need to center the frame and lock in a minimum size. These are basic, fundamental steps, but currently they require writing a fair amount of tedious code. This situation could be improved with a couple of convenience methods.
For example:   JFrame frame = new JFame();
   //add UI controls
   frame.pack();
   frame.center();
   frame.setMinimumSize(frame.getCurrentSize()); Any idea why these (or similar) methods are not included in Java? It sure would clean up the top level Swing class in many applications.
- Dem

This situation could be improved with a couple of convenience methods.So create a class called MyFrame and write your own. Everybody has there own idea of whats important and whats not. Thats why you have the ability to extend classes --- to add your own functionality.
have you tried:
frame.setLocationRelativeTo( null );
If your looking for a way to control the minimum size then use add a ComponentListener to the frame. Examples have been posted in the forum.

Similar Messages

  • Set minimum size for GridBagLayout

    Hi all,
    I have a dialog which using GridBagLayout. I spent quite a lot of time to test, but still got the following problems. Could anyone give me some solutions?
    1. How do I set the dialog to have a minimum size ?
    Since the dialog is sizeable, so I have to make this limitation to prevent the dialog being too small .
    I tried the following code:
            this.getContentPane().setMinimumSize(new Dimension(686,520));
            this.setMinimumSize(new Dimension(686,520));but it's still not work... , I can still resize it too small.. any other way ?
    2. How do I set its default size when dialog pop up?
    I don't know why the dialog always auto popup in maximum size.
    I have tried to use this.setSize(new Dimension(686, 536)); inside jbInit() function, but still not work...
    Please help ... Thanks a lot.

    Why dont you try this.setResizable(false);?
    Anyway I have given a sample program here using Gridbag layout and this dialog cannot be resized.
    import java.awt.BorderLayout;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import javax.swing.JButton;
    import javax.swing.JDialog;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    public class SunExample28 extends JDialog {
         public SunExample28() {
              setTitle("JDialog with Fixed size");
              JLabel nameLbl = new JLabel("Name:");
              JLabel ageLbl = new JLabel("Age:");
              JTextField nameFld = new JTextField(10);
              JTextField ageFld = new JTextField(10);
              GridBagConstraints gbc = new GridBagConstraints();
              Insets in = new Insets(5,5,5,5);
              JPanel compPanel = new JPanel(new GridBagLayout());
              gbc.gridx = 0;
              gbc.gridy = 0;
              compPanel.add(nameLbl,gbc);
              gbc.insets = in;
              gbc.gridx = 1;
              gbc.gridy = 0;
              compPanel.add(nameFld, gbc);
              gbc.insets = in;
              gbc.gridx = 0;
              gbc.gridy = 1;
              compPanel.add(ageLbl, gbc);
              gbc.insets = in;
              gbc.gridx = 1;
              gbc.gridy = 1;
              compPanel.add(ageFld, gbc);
              JButton okBtn = new JButton("OK");
              JButton cancelBtn = new JButton("Cancel");
              JPanel btnPanel = new JPanel();
              btnPanel.add(okBtn);
              btnPanel.add(cancelBtn);
              getContentPane().add(compPanel, BorderLayout.CENTER);
              getContentPane().add(btnPanel, BorderLayout.PAGE_END);
              setDefaultCloseOperation(DISPOSE_ON_CLOSE);
              setSize(500,300);
              setResizable(false);
          * @param args
         public static void main(String[] args) {
              // TODO Auto-generated method stub
              new SunExample28().setVisible(true);
    }

  • Trying to center frame, but affects my Robot method

    Hi, I am using a robot to take a screen shot of the users screen, which will then display on top of the screen with a piece of text printed. This works fine, but when I try to center the frame that holds the information the background is replaced with a grey box (with my text in). Is there any way I could get around this at all? Here is the relevant code, all help is very much appreciated. Thank-you :)
        public void createPanel()
            JFrame frame = new JFrame();
            frame.setUndecorated(true);
            frame.setLayout(new BorderLayout());
            MyDisplay test = new MyDisplay(frame);
            test.setLayout(new BorderLayout());
            frame.add(test, BorderLayout.CENTER);
            JLabel label = new JLabel("This is just a test");
            test.add(label, BorderLayout.CENTER);
            frame.pack();
            frame.setSize(500, 200);
            // make sure you pack and/or set size first...
            Dimension dS = Toolkit.getDefaultToolkit().getScreenSize();
            Dimension dF = frame.getSize();
            // make sure it's not off screen
            int x = (dS.width/2)-(dF.width/2);
            int y = (dS.height/2)-(dF.height/2);
            if(x < 0) x = 0;
            if(y < 0) y = 0;
            frame.setLocation(x, y);
            frame.setVisible(true);
            startRobot();
        public void startRobot()
            try {
                robot = new Robot();
                Rectangle r = new Rectangle(500, 200);
                background = robot.createScreenCapture(r);
            catch (Exception e) {
        }

    Swing related questions should be posted in the Swing
    forum.
    If it's swing you want to use,
    I will kindly spread the news.
    Camickr's the one to ask,
    Ask him first, don't wait till last.
    You must post in proper place,
    or the egg is on your face.
    Post where swing will be discussed,
    don't be makin' such a fuss.
    If gui is what you require,
    'bout the swing you should inquire.
    "New to Java" ain't for you,
    Swing's what you should really do.

  • JSplitPane resize problem (not a minimum size problem though)

    I created a JSplitPane and put a JPanel in both the top and bottom sides. This JSplitPane is in the center section of a BorderLayout of a JFrame. The minimum sizes are set and I can move the divider bar all I want. Instead, I am having trouble resizing the window and having the divider bar becoming shorter. When I resize the window, the width of the images dont become smaller even though they have a minimum size set to a minimal size and there is space in the panels that can be reduced.
    Here is the code that I am playing with to get this to work. Any ideas how to get this to work? It works in the SwingSet2 Demo, but there is so much extra junk in there that I cant determine the differences with what I'm doing.
    Thanks,
    J.R.
    import java.awt.*;
    import javax.swing.*;
    public class SplitPaneJFrame extends JFrame {
    JPanel statsPanel = new JPanel();
    JPanel northPanel = new JPanel();
    JPanel southPanel = new JPanel();
    JSplitPane splitPane;
    public SplitPaneJFrame () {
    setupGUI();
    private void setupGUI() {
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    setSize(screenSize.width, screenSize.height - 64);
    Container c = getContentPane();
    c.setLayout(new BorderLayout());
    JPanel p1 = new JPanel();
    p1.setMinimumSize(new Dimension(100,100));
    p1.setPreferredSize(new Dimension(500,250));
    p1.setBackground(Color.yellow);
    p1.add(new Button("This is a button for the top area"));
    JPanel p2 = new JPanel();
    p2.setMinimumSize(new Dimension(100,100));
    p2.setPreferredSize(new Dimension(500,250));
    p2.setBackground(Color.red);
    p2.add(new Button("This is a button for the bottom area"));
    splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, true, northPanel, southPanel);
    splitPane.setOneTouchExpandable(true);
    northPanel.setLayout(new BorderLayout());
    northPanel.setMinimumSize(new Dimension(100,100));
    JPanel panel = new JPanel();
    panel.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
    panel.setPreferredSize(new Dimension((int)Math.min(1012, getSize().width * .75), (int)(getSize().height * 0.75)));
    panel.setMinimumSize(new Dimension(100,100));
    panel.add(p1);
    northPanel.add(panel);
    southPanel.setLayout(new BorderLayout());
    southPanel.setMinimumSize(new Dimension(100,100));
    southPanel.add(p2);
    splitPane.setDividerLocation(0.75);
    c.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
    c.setBackground(Color.white);
    JPanel jp = new JPanel();
    jp.add(splitPane);
    c.add(BorderLayout.CENTER, jp);
    setVisible(true);
    public static void main(String args []) {
    new SplitPaneJFrame();

    Hmm. Ok, that was dumb on my part :-) Sometimes you just need a second set of eyes.
    Any Idea why, after I make the fix, the top panel does not resize at all? The bottom one does, but the inner panels of the top do not. I added a second panel to the top and it flowed under the first one panel (p1) as the outter panel (northPanel) resized, but the inner panel stays the same size regardless of how much I narrow the window.
    J.R.

  • How to set the minimum size of app window?

    Hi JavaFX experts :)
    Is it possible to limit the minimum size of application window? What would be the JavaFX equivalent of the following SWING code?
            JFrame frame = new JFrame("Application Title");
            frame.setMinimumSize(new Dimension(320, 240));
            frame.setPreferredSize(new Dimension(640, 480));Preferred or initial size was easy (width/height), but what about the minimum size?
    Stage {
        title: "Application Title"
        width: 640
        height: 480
        scene: Scene {
            content: [
                // some content
    }Thanks!

    var stage: Stage;
    var stageWidth = stage.width on replace
        if (stageWidth < 640) stageWidth = 640;
    var stageHeight = stage.height on replace
        if (stageHeight < 400) stageHeight = 400;
    stage = Stage {
        title: "Application Title"
        width: bind stageWidth with inverse
        height: bind stageHeight with inverse
        scene: Scene {
            content: [
                // some content
    }It doesn't prevent resizing to smaller dimensions but it restores to minimal dimension if done.

  • How can I select the minimum size of Elements 10? It won't install due to lack of disk space on C:

    I have just purchased and downloaded Elements 10. Can't install it due to insufficient disk space on my C drive. I have enough space on my D drive, but it will not install there, probably because my sustem files are on C drive. The Install wisard does not provide for a selection of the size of Elements 10. Is there a way to select the minimum size? If so, how can I do this?

    There is not an option to customize the installation.  The system requirements for Photoshop Elements 10 request that you at least have 4 gigabytes free, plus more for installation, how much free space do you have on your C drive?

  • How to set Minimum size of JFrame?

    Hi,
    I want to set minimum size of my JFrame. How can i set it?
    I want to maintain a minimum size while resizing the JFrame. The user should not resize beyond the minimum size of JFrame.
    Any body knows solution, please post it.
    Thanks in Advance.

    One posible solution is to add a ComponentListener to the
    JFrame that implements ... (struggles to remember) ...
    componentResized().
    Inside componentResized(), check the size of your JFrame,
    and if it is smaller than your preferred minimal dimension,
    resize it up to your minimum.
    // in the constructor, perhaps, or initialization routine
    addComponentListener(
      newComponentAdapter()
        public void componentMoved(ComponentEvent e)
            Dimension size = e.getComponent().getSize();
            size.width = Math.max(size.width, myMinimalWidth);
            size.height = Math.max(size.height, myMinimalHeight);
            e.getComponent.setSize(size);
      }); Pity there's no setMinimumSize() like the JComponent class.
    Best of luck !
    Eric

  • How to set a minimum size of a JFrame

    hi freinds,
    i have written a swing application which starts like this ,
    public class workerclass extends JFrame implements ActionListener {
    blah blah blah ..
    now can you please tell me how to set a minimum size for this application once it starts so that user cant minimize it more than that minimum size.....
    when i tried
    setDefaultLookAndFeelDecorated(true);
    it did my job by setting it to a particular size which wasnt minimizable, but this default look and feel looked ugly.
    so i used system look and feel
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    but with this how do i set the minimum size..
    please do help
    and one thing more , like
    in my main Method when i say
    new workerclass().setSize(500,500); or say(100,100) or say(300,300)
    new workerclass().setVisible(true);
    the size (***,***) , doesnt reflect when my application starts, it always starts with some damn fixed size , what can be the issue .
    waiting.........
    Thanks

    how do i set my Jrame to a minimum size so that
    minimizing beyond that can be restrictedThis is what the code is all about
    don't know your code but ..
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    public class workerclass extends JFrame implements
    ActionListener,ComponentListener {
    public void initcomponenet()
    setJMenuBar();
    pack();
    addComponentListener(this);
    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new myclass.setSize(500,500);
    new myclass().setVisible(true);
    public void componentShown(ComponentEvent e)
         public void componentResized(ComponentEvent e)
              if(getWidth()<200)//restrict width to minimum 200
                   setSize(200,getHeight());
    if(getHeight()<100)//restrict Height to minimum
    um 100
                   setSize(getWidth(),100);
         public void componentMoved(ComponentEvent e)
         public void componentHidden(ComponentEvent e)
    Thanks kanad , your code is working ... you desere lot more than a duke dollar but for now i can only give that :-( , please accept it :-)

  • How can I set the minimum size the location bar should automatically resize to

    I have moved all of my navigation buttons, location bar and tabs to be in line. This has been done to maximise the available space on screen.
    This works perfectly with two or three tabs open, but once more tabs are opened the location bar automatically shrinks to allow for more tabs and becomes unusable. I want to set the minimum size the location bar should shrink to but I do not know how to do this.
    [http://www.mediafire.com/imgbnc.php/eed5749531b3081c43186f59492500e5f089c498c0372fb6fa797b7d697826806g.jpg Screen shot displaying automatically resizing location bar]
    Any help would be appreciated.
    Thanks.

    FBZP seems to be the only way to do the minimum amout setting in standard SAP.
    You can check the BTE (Business transaction event) 00001820 for excluding the low amount items in F110. Please search SDn on how to use the BTEs.
    Regards,
    SDNer

  • Window.resizeTo in addon (overlay commonDialog) does not resize after some minimum size in FF v17 & 18

    I created an addon which overlay commonDialog.xul.
    In the load event, window.resizeTo is called and set to a very small size (like 10x10) so it is almost invisible to users.
    This works fine in FF 15. After upgrading to FF 17 or FF 18, this stops working.
    When I set the size big, it works fine. When setting it to a small size, it seems like when hitting a limit, the window size stays the same. The window size is the same when setting to 10x10 or 100x100.
    This is on a MAC, but same behavior is observed on Windows too.
    It seems like there is a minimum size predefined and the window does not get smaller than that.
    Aren't we supposed to be able to resize the window as we wish in the addon?

    If you create your own window with a unique ID then it should be possible to create more specific CSS rules for that window.
    How (when) do you resize that window when you need it to show with the default dimensions?
    If you have specific questions about developing extensions then it is best to ask advice at the MozillaZine Extension Development forum.
    *http://forums.mozillazine.org/viewforum.php?f=19
    The helpers at that forum are more knowledgeable.<br>
    You need to register at the MozillaZine forum site in order to post at that forum.

  • Link flash text to center frame

    Hello,
    I generated a flash text with dreamweaver. This text should
    be a link that opens in the center target frame. The problem is
    that I cannot choose the center frame as target (only self, parent,
    blank, top).... . What can I do to make the link open in the center
    frame?
    Thanks in advance,
    Jeremy

    that' just the code to place the swf file. You need to get
    into the .fla file...
    However, I think I see what you mean... if you're creating
    this entirely in Dreamweaver, then you probably don't have access
    to the actual actionscript. It's also limiting when you just place
    an flv within Dreamweaver because you can delete skins and many
    other things.
    how did you generate the text animation within
    Dreamweaver?

  • To set minimum size of flash panel

    I am developing a flash panel in PS CS5. I have tried many way to set minimum size of panel window, but they don't work. I have not idea how to do this now. I'm waiting for your HELP. Thank YOU!

    One posible solution is to add a ComponentListener to the
    JFrame that implements ... (struggles to remember) ...
    componentResized().
    Inside componentResized(), check the size of your JFrame,
    and if it is smaller than your preferred minimal dimension,
    resize it up to your minimum.
    // in the constructor, perhaps, or initialization routine
    addComponentListener(
      newComponentAdapter()
        public void componentMoved(ComponentEvent e)
            Dimension size = e.getComponent().getSize();
            size.width = Math.max(size.width, myMinimalWidth);
            size.height = Math.max(size.height, myMinimalHeight);
            e.getComponent.setSize(size);
      }); Pity there's no setMinimumSize() like the JComponent class.
    Best of luck !
    Eric

  • Set minimum size of JScrolPane's knob

    Hi All,
    Please suggest me a way to set a minimum size of JScrolPane's knob.
    It becomes very small when the numbers of items in the list are so much and that time knob is very small. that makes scrolling difficult to the user.
    So i want to set a minimum size of the knob.
    Please suggest the snippet of code for that

    One posible solution is to add a ComponentListener to the
    JFrame that implements ... (struggles to remember) ...
    componentResized().
    Inside componentResized(), check the size of your JFrame,
    and if it is smaller than your preferred minimal dimension,
    resize it up to your minimum.
    // in the constructor, perhaps, or initialization routine
    addComponentListener(
      newComponentAdapter()
        public void componentMoved(ComponentEvent e)
            Dimension size = e.getComponent().getSize();
            size.width = Math.max(size.width, myMinimalWidth);
            size.height = Math.max(size.height, myMinimalHeight);
            e.getComponent.setSize(size);
      }); Pity there's no setMinimumSize() like the JComponent class.
    Best of luck !
    Eric

  • Desktop Folders now restrict minimum size

    Desktop folders.
    Apple, and the world's technical press, celebrate, as they should all the great NEW features in Lion, and every OS upgrade,
    for the all the new an innovative great features.
    It is left to a small minority of people who need computers to get a day's work done, and whose voice is seldom heard by Apple,
    to encounter the endless surprises in great features that are put in the trash by Apple for no good reason. Each OS upgrade
    takes away great features, an Lion does this as well.
    The topic at this email is desktop folders.
    You could, until now, drag the folder border to encompass a single file name, whic I sometimes really like to do. NOW there is
    an arbitrary and pointless SIZE LIMITATION to how small you can make an open folder on your desktop. The minimum size
    is rather large, about the space for 10 files in list view.
    Why Apple engineers so gleefully and unapologetically trash great features while celebrating what they call new features
    is incomprehensible to me.
    Folder views in Lion are the sloppiest and most unpredicatble so far.
    For all of its celebrated innovation, Apple engineers also have a really long shadow of destruction and removal
    that is not encouraging, that is in many ways rather offensive, as it flys in the face of common sense
    and is a rotten spot in the endless posturing Apple makes about respect for their customers, in particular
    the long-standing base of users that have stayed with Apple when it was almost down the tubes. I have been
    an Apple user since OS 5 at least.

    Widen the grid spacing; however, if the name is still too long, it will display like that.
    Use List or Column view, or hover over the name and it will popup the full name.

  • Minimum size of SGA for the recovery catalog db ?

    I've created a recovery catalog database and by default the total size of the SGA is 180MB. Problem is, there isnt' much memory on development server to run the target and recovery catalog database (catdb).
    Would it be possible to startup catdb with an SGA of size 10 MB or so? What's the minimum size? I figured since catdb is only used as for the recovery catalog and would rarely be access or changed, it doesn't need to use that much memory.
    Thanks in advance

    Hi David,
    In 10g, the minimal shared_pool_size is 120M, otherwise you will get
    ORA-00371: not enough shared pool memory, should be atleast 123232153 bytes
    You can set java_pool_size to 0, which is NOT the default.
    You could also set the large_pool_size to 0, but I am not sure this is recommended.
    In 8.0 shared_pool_size could be as low as 300K. Read your Reference manual for the values specifics to your version
    HTH
    Laurent Schneider
    OCM DBA

Maybe you are looking for