Results 1 to 8 of 8
  1. #1
    new2java2009 is offline Member
    Join Date
    Aug 2009
    Posts
    60
    Rep Power
    0

    Default Button is sometimes truncated

    What might be the reason that the button sometimes gets truncated? In the normal case, the button Cancel is big enough to have the whole word 'Cancel' spelled out, but on some machines, maybe the reslution is set different, the cancel button is smaller, only the text 'Can...' is visible.

    Anybody know how I can fix that? Thanks!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Is this Swing? AWT? SWT? Other?

    If Swing, you may need to look into how you are laying out your components in their containers. Have you gone through the Sun Swing layout tutorials? If not you will want to do so. Google: Java Swing Laying out Components within Container tutorial

    first hit.

    Much luck!

  3. #3
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    Yea it did that for me as well when I used the BoxLayout. The truncation went away after I added a rigid area between the two buttons.

    Like Fubarable said, it depends on the situation. Sometimes the layout can't use ur preferredSize (if you didn't do setPreferredSize(Dimension d), then it is an easy fix. All you have to do is button.setPreferredSize(new Dimension(width, height)); ) because it can't fit the button or w/e the reason might be. setMinimum() and setMaximum() might help.
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  4. #4
    new2java2009 is offline Member
    Join Date
    Aug 2009
    Posts
    60
    Rep Power
    0

    Default

    I use gridBagLayout.

    Following are code for the buttons.
    Java Code:
    			/*
    			 * Define Cancel and OK buttons
    			 */	
    			cancelButton = new TButton();
    			cancelButton.setText("Cancel");
    			cancelButton.setPreferredSize(new Dimension(65, 23));
    			
    			okButton = new TButton();
    			okButton.setText("OK");
    			okButton.setPreferredSize(new Dimension(65, 23));
    
    			/* 
    			 * OK button
    			 */
    			GridBagConstraints gridBagConstraints = new GridBagConstraints(				// OK Button
    					60, 0, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST,
    					GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0);		
    			buttonPanel.add(okButton, gridBagConstraints);
    			
    			/*
    			 * Cancel button
    			 */
    			GridBagConstraints gridBagConstraints_1 = new GridBagConstraints(			// Cancel Button
    					70, 0, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST,
    					GridBagConstraints.NONE, new Insets(0, 0, 0, 10), 0, 0);		
    			buttonPanel.add(cancelButton, gridBagConstraints_1);

  5. #5
    new2java2009 is offline Member
    Join Date
    Aug 2009
    Posts
    60
    Rep Power
    0

    Default

    I also noticed that after I change the display setting (resolution) of my PC, the Cancel button and OK button behaves differently. The width of the OK button changes when I resize the screen. While the Cancel button's size stays the same no matter how small I resize the screen. Why are they behaving differently? How can I make the Cancel button hehave the same as the OK button?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Don't use setPreferredSize for the JButtons but rather let the JVM determine what would in fact be the preferred size for the buttons, otherwise your preferred size might run counter to what the current OS and video set up allows for the button text and your button may be too small to hold all the text.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default

    Also note that you don't need a new GridBagConstraints object for each component you add to the GridBagLayout. And why on earth are your gridx values 60 and 70? Does your layout have that many columns? Note that the gridx and gridy values are NOT pixel coordinates.

    Recommended reading: How to Use GridBagLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)

    And as already mentioned, DON'T setPreferredSize.

    db

  8. #8
    new2java2009 is offline Member
    Join Date
    Aug 2009
    Posts
    60
    Rep Power
    0

    Default

    Thank you all, yes, the way I set the GridBagLayout was not right. Changing the gridx definately helps.

Similar Threads

  1. Button Glitch
    By dunafrothint in forum AWT / Swing
    Replies: 4
    Last Post: 02-16-2010, 05:06 PM
  2. Button
    By Tb0h in forum New To Java
    Replies: 6
    Last Post: 07-22-2009, 02:28 AM
  3. Replies: 4
    Last Post: 10-02-2008, 07:32 PM
  4. Using SWT Button
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-08-2008, 10:05 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
  •