Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / August 2007

Tip: Looking for answers? Try searching our database.

JButton.setMargin broken

Thread view: 
Roedy Green - 26 Aug 2007 08:01 GMT
It seems that Swing ignores JButton.setMargin. Is that your finding?
Is this a bug or is there some excuse for it?

Here is my workaround - using a compound border to get the margin.

public class JEButton extends JButton {
   /**
    * constructor
    *
    * @param text string to label the JButton
    */
   public JEButton( String text )
       {
       super( text );
       this.setFocusPainted( false );
       this.setBorder( border );
       this.setFont( new Font( "Dialog", Font.BOLD, 16 ) );
       // set margin does not work.

       // Leave foreground and background alone to get gradient.
       // Client should do a setToolTip, possibly a requestFocus
       }

   private static final Border border;

   static
       {
       final Border innerBorder =
               BorderFactory.createEmptyBorder( 2, 5, 2, 5 );
       final Border outerBorder =
BorderFactory.createRaisedBevelBorder();
       border = BorderFactory.createCompoundBorder( outerBorder,
innerBorder );
       }
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Andrew Thompson - 26 Aug 2007 08:37 GMT
>It seems that Swing ignores JButton.setMargin. Is that your finding?

No.  This code produces one small and one big
button here, using Java 1.6.

<sscce>
import java.awt.*;
import javax.swing.*;

class ButtonMargin {

 public static void main(String[] args) {
   JPanel p = new JPanel();

   JButton btn1 = new JButton("button 1");
   p.add(btn1);

   JButton btn2 = new JButton("button 2");
   Insets insets = new Insets(100,100,100,100);
   btn2.setMargin(insets);
   p.add(btn2);

   p.validate();

   JOptionPane.showMessageDialog( null, p );
 }
}
</sscce>

>Is this a bug or is there some excuse for it?

The layouts being used in the code that fails?

Signature

Andrew Thompson
http://www.athompson.info/andrew/

Roedy Green - 26 Aug 2007 10:41 GMT
>No.  This code produces one small and one big
>button here, using Java 1.6.

Maybe it is an interaction with GridBagLayout.
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Andrew Thompson - 26 Aug 2007 15:54 GMT
>>No.  This code produces one small and one big
>>button here, using Java 1.6.
>
>Maybe it is an interaction with GridBagLayout.

Maybe.  Without code it is hard to tell.

GBL does tend to muck up when there is no minimum*
size set for a component, when that component cannot
be assigned its preferred size.

* As far as I vaguely recall.  There are very few things
cannot be achieved with other layouts, so I tend to avoid
using it, mostly.

Signature

Andrew Thompson
http://www.athompson.info/andrew/

Christian Kaufhold - 26 Aug 2007 12:17 GMT
> It seems that Swing ignores JButton.setMargin. Is that your finding?
> Is this a bug or is there some excuse for it?

The margin is modelled as an extra border (e.g.
javax.swing.plaf.basic.BasicBorders.MarginBorder), usually installed
by the LookAndFeel. If you replace a button's border, put a margin
border (using CompoundBorder) in the place where you want to have it,
relative to the other border(s).

Christian
Roedy Green - 27 Aug 2007 02:39 GMT
>The margin is modelled as an extra border (e.g.
>javax.swing.plaf.basic.BasicBorders.MarginBorder), usually installed
>by the LookAndFeel. If you replace a button's border, put a margin
>border (using CompoundBorder) in the place where you want to have it,
>relative to the other border(s).

That's what I did.  Thanks for explaining why it failed.
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Roedy Green - 27 Aug 2007 03:12 GMT
On Sun, 26 Aug 2007 07:01:17 GMT, Roedy Green
<see_website@mindprod.com.invalid> wrote, quoted or indirectly quoted
someone who said :

>It seems that Swing ignores JButton.setMargin. Is that your finding?
>Is this a bug or is there some excuse for it?

see http://mindprod.com/jgloss/jbutton.html#GOTCHAS
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.