🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #9959

Re: JButton borders

#9959
From: John P. McGrath
Date: Sun, 19 Dec 1999 00:00
57 lines
2585 bytes
On Sun, 19 Dec 1999 06:46:46 +0200, George Lefter wrote:

> hello. how can i draw a rectangle exactly on a button's border/edges?
> the code below draws the rectangle above and in the left of the button..
> how should i change the line g.drawRect(..) ?

Instead of trying to draw this yourself, you could set the border 
property for the button.  Use something like:

    button.setBorder( BorderFactory.createLineBorder( Color.black ) );

If the button already has a border, and you want to create an additional 
one, then you could use a CompoundBorder, including the border that it 
has, plus the new one you want to add.


> public class myFrame extends JFrame {
>    static JButton b1;
>    public void paint(Graphics g) {
>       g.drawRect( b1.getX(), b1.getY(), b1.getWidth(),
>                                            b1.getHeight() );  
>    }
>    public static void main(String s[]) {
>       b1= new JButton("A Button");
>       Container p= this.getContentPane();
>       p.setLayout(null);
>       p.add(b1);
>       b1.setBounds( 100, 100, 75, 30);
>       ...
>    }
> }

A few notes on the above code:

If you are going to write your own paint code for a Swing component, you 
usually should override paintComponent() instead of paint().  The paint() 
method calls paintComponent(), but it also handles painting the child 
components, and painting the borders.  Note that whether it has any 
children is part of the implementation of the component, so you probably 
should not assume it does not.  Even if the current implementation does 
not, a future one may.

Also, for both paint() and paintComponent(), you usually should call the 
superclass implementation first, unless you want to eliminate some of its 
behavior.  If you do not, you may be eliminating some behavior that you 
want.  For instance, the superclass handles painting the component 
background.  For example:

    public void paintComponent( Graphics g ) {
        super.paintComponent( g );
        //  do additional stuff.
    }

-- 
John P. McGrath
<JPMcGrath@fast.net>
<jpm@nscs.fast.net>
Technical Details
Message-ID: <MPG.12c64d5810352fee989708@forums.borland.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <385C6336.DFBE63B1@void.art.ro>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 111d45,c49df965457840ee
X-Google-Attributes: gid111d45,public
From: John P. McGrath <JPMcGrath@fast.net>
Subject: Re: JButton borders
Date: 1999/12/19
Message-ID: <MPG.12c64d5810352fee989708@forums.borland.com>
X-Deja-AN: 562558509
References: <385C6336.DFBE63B1@void.art.ro>
Organization: Another Netscape Collabra Server User
Newsgroups: borland.public.jbuilder.java.swing