Results 1 to 8 of 8

Thread: JButton Size

  1. #1
    jboy is offline Member
    Join Date
    Aug 2009
    Posts
    56
    Rep Power
    0

    Default JButton Size

    Hi,

    I have a bit of trouble trying to enlarge the size of the button that I have on my JPanel or I should say I need to change the button size to a specific size:
    Java Code:
    ..
    ..
    ..
    JButton button = new JButton("OK");
    button.setFont(new java.awt.Font("Arial", 1, 25));            
    add(button);
    ..
    ..
    ..
    Thanks,

  2. #2
    matzahboy is offline Member
    Join Date
    Nov 2008
    Posts
    67
    Rep Power
    0

    Default

    Use the setPreferredSize(Dimension preferredSize) method.

    Example:
    Java Code:
    int width=50;
    int height=40;
    button.setPreferredSize(new Dimension(width, height));

  3. #3
    jboy is offline Member
    Join Date
    Aug 2009
    Posts
    56
    Rep Power
    0

    Default

    Thank you,
    Also, I need to change te position of the button in the panel, I am not sure which method I should use to change the location of the button to a specific location?

  4. #4
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    837
    Rep Power
    6

    Default

    Try with this method for location.
    setLocation
    public void setLocation(int x,
    int y)Moves this component to a new location. The top-left corner of the new location is specified by the x and y parameters in the coordinate space of this component's parent.

    Parameters:
    x - the x-coordinate of the new location's top-left corner in the parent's coordinate space
    y - the y-coordinate of the new location's top-left corner in the parent's coordinate space
    Since:
    JDK1.1
    See Also:
    getLocation(), setBounds(int, int, int, int)

    --------------------------------------------------------------------------------
    Ramya:cool:

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Learn how to use Layout Managers to control the postion of components on a panel. Read the section from the Swing tutorial on "Using Layout Managers".

  6. #6
    Willi's Avatar
    Willi is offline Member
    Join Date
    Oct 2009
    Posts
    40
    Rep Power
    0

    Default

    Quote Originally Posted by camickr View Post
    Learn how to use Layout Managers to control the postion of components on a panel. Read the section from the Swing tutorial on "Using Layout Managers".
    Yeah! Layout Manager is the best way to manage position of a component in a frame. :D
    Write a program to achieve anything you want in your life!

  7. #7
    jboy is offline Member
    Join Date
    Aug 2009
    Posts
    56
    Rep Power
    0

    Default

    Hi,
    Thanks for the information, however none of the methods you mentioned seem to work for me, I have a button panel without any border inside the main panel. I tried a lot of methods but the button panel never moved to a different location within the main panel..Please tell me why this is happening..

  8. #8
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Please tell me why this is happening..
    How can we tell you why this is happening? The examples from the Swing tutorials give you "working code". If it works in the tutorial, then obviously you didn't read the tutorial or try the demo code.

    If you need more help then post your SSCCE.

Similar Threads

  1. How to set enabled my JButton...?
    By mlibot in forum New To Java
    Replies: 3
    Last Post: 10-13-2009, 11:24 AM
  2. Setting frame size to the size of an image
    By Yoruichi in forum AWT / Swing
    Replies: 5
    Last Post: 04-22-2009, 04:37 PM
  3. Jbutton size
    By mrvigneshmca in forum AWT / Swing
    Replies: 2
    Last Post: 03-18-2009, 03:51 PM
  4. Help with JButton
    By geoffreybarwise in forum New To Java
    Replies: 4
    Last Post: 05-21-2008, 10:48 AM
  5. Few action in one Jbutton
    By kubiasty in forum New To Java
    Replies: 0
    Last Post: 07-25-2007, 10:19 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
  •