Results 1 to 2 of 2
  1. #1
    rru96 is offline Senior Member
    Join Date
    Sep 2012
    Rep Power

    Default Swing - setSize for JButton not giving expected result

    I am just starting to step into Swing and wanted to set up a simple JFrame with a JButton on it. I was not concerned as to where the JButton ended up, as long as it ended up smaller than the actual JFrame. As of now, it is taking the entire Frame.

    Java Code:
    import javax.swing.*;
    public class FirstRectangle{
              public static void showWINDOW(){
                        JFrame f = new JFrame("TITLE OF JFRAME");
                        JButton but1 = new JButton("B.A.B.");
                        but1.setSize(50,50);   // I have tried placing it here and above, right after adding the button. ***** EXPECTED a 50x50 button
                        public static void main(String[] args){
                                  javax.swing.SwingUtilities.invokeLater(new Runnable(){
                                            public void run(){
    What am I missing that is causing the button to not resize on
    Java Code:
    ? Is this something that would be solved with layouts? I heard a little about gridBagLayouts and GridBagConstraints the other day, was hoping to avoid that for a few more days.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    New Zealand
    Rep Power

    Default Re: Swing - setSize for JButton not giving expected result

    Perhaps FirstRectangle didn't like that you yelled at it :)

    Seriously, you're right. It's all about layouts, and you can't really avoid that. Components get their size (and position) from the layout manger of the container within which they are contained. That makes sense (at least from my pov) - left to themselves they'd be squabbling about who goes where.

    Yes, there is a setSize(), but ignore it for now.

    The default layout for the container which contains the stuff in a frame is a so-called BorderLayout. Adding to the centre (again the default) makes the thing added take up all the available space. Not what you want. But you can also add things to the "edge" and, in that case they will use preferred width or height (as appropriate).


    Java Code:
    whereever.add(whatever, BorderLayout.SOUTH);
    BorderLayout is your friend - especially when you consider that the things added can, themselves, be containers. But there are many other layout managers available. See any tutorial (eg google "Java tutorial" for Oracle's)

Similar Threads

  1. Replies: 5
    Last Post: 06-25-2012, 12:02 PM
  2. Would anyone mind giving me feedback on a Swing Transition effect?
    By ozzyman in forum Reviews / Advertising
    Replies: 1
    Last Post: 03-23-2012, 12:34 AM
  3. Replies: 2
    Last Post: 10-25-2011, 10:26 PM
  4. New AWT/SWING user, help in JButton
    By corrax in forum AWT / Swing
    Replies: 7
    Last Post: 04-02-2011, 03:27 AM
  5. [SWING]Adding JButton(class1) to JPanel(class2)
    By equal in forum New To Java
    Replies: 8
    Last Post: 02-20-2011, 01:09 AM

Posting Permissions

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