Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: Hi how can I get the size of the JButton!! Help!!

  1. #1
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default Hi how can I get the size of the JButton!! Help!!

    HI IS ANY KNOWS HOW TO GET CURRENT SIZE OF THE JBUTTON, IM USING LAYOUT MANAGER I TRY TO CALL THE getSize(), getHeight(), getBounds() but none of these are working for me all the output is zero..

    Please i need your help...

    Java Code:
    panel1 = new JPanel(new GridLayout(1,1,10,10));
            	
        	btnAdd = new JButton("Add Student Info");
          	
        	GridBagConstraints cst = new GridBagConstraints();
      
        	panel1.add(btnAdd);
     
        	System.out.println(btnAdd.getSize());
        	System.out.println(btnAdd.getHeight());
        	System.out.println(btnAdd.getBounds());
        	
     
            getContentPane().add(panel1);
            
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            setBounds((screenSize.width-475)/2, (screenSize.height-354)/2, 475, 354);
    HERES THE OUTPUT IN THE TERMINE CONSOLE:

    java.awt.Dimension[width=0,height=0]
    0
    java.awt.Rectangle[x=0,y=0,width=0,height=0]

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default Re: Hi how can I get the size of the JButton!! Help!!

    As long as the JButton (or any Component for that matter) isn't visible it doesn't have a size or position ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default Re: Hi how can I get the size of the JButton!! Help!!

    Quote Originally Posted by JosAH View Post
    As long as the JButton (or any Component for that matter) isn't visible it doesn't have a size or position ...

    kind regards,

    Jos
    It is visible, when I run it I can see the frame with jbuttons.

    In my main method:

    Java Code:
    public static void main(String[]args){
    
    MyButtonDemo btnDemo = MyButtonDemo();
    btnDemo.setDefaultCloseOperation(EXIT_ON_CLOSE);
    btnDemo.setBounds(0,0,350,350);
    btnDemo.setVisible();
    }

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Hi how can I get the size of the JButton!! Help!!

    Quote Originally Posted by RichersooN View Post
    It is visible
    Not when you sysout the size in your first code, it isn't.

    Why do you think you need to know the size of the button?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default Re: Hi how can I get the size of the JButton!! Help!!

    Quote Originally Posted by DarrylBurke View Post
    Not when you sysout the size in your first code, it isn't.

    Why do you think you need to know the size of the button?

    db
    The JButton has an image inside it, I want the "image to be resize larger if the gui has been maximized or resize smaller if the gui has been minimized" to be exactly fit inside the JButton.

  6. #6
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Hi how can I get the size of the JButton!! Help!!

    Get the size of the button after calling pack() on the parent JFrame, or making the JFrame visible - which will layout your components using the LayoutManagers and allow you to get the initial size.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Hi how can I get the size of the JButton!! Help!!

    doWhile likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. ActionListener for JButton after changing Button to JButton
    By ravi.joshi53 in forum Java Applets
    Replies: 2
    Last Post: 10-07-2011, 07:35 AM
  2. JButton Size Help?
    By bpx95 in forum New To Java
    Replies: 1
    Last Post: 05-14-2011, 07:02 AM
  3. JButton Size
    By jboy in forum New To Java
    Replies: 7
    Last Post: 10-14-2009, 04:20 PM
  4. 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
  5. Jbutton size
    By mrvigneshmca in forum AWT / Swing
    Replies: 2
    Last Post: 03-18-2009, 03:51 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
  •