Custom Swing Button

I want to develop a swing button, which is custom made and does not looks like the default metal. i dont want to apply motif or windows style.
basically the swing button should look different from the default Metal look and feel. Ex:-Somthing like a protruted button, which on click would be pressed inside.
help me ??? CODE pls... CODE..
sachinbafnaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mberumenCommented:
You can create a custom class that extends Canvas.  Here's a basic example.  This creates an oval button that changes from green to red when pressed,  you can include an image in the canvas and change the image on mousepress.  I'll see if I have an example somewhere

import java.awt.*;

/**
 * <strong>ButtonCanvasExample</strong> -- implementing
 * a simple custom button as a subclass of <code>Canvas</code>.
 *
 * <p>
 * Experiment:
 * <ul>
 *   <li>Click on both the regular button and on
 *       the green oval button.
 *   <li>Click slowly to see how each button shows
 *       its pressed state.
 * </ul>
 */
public class ButtonCanvasExample extends java.applet.Applet {

    Button myButton = new Button("a regular button");
    ButtonCanvas myCanvas = new ButtonCanvas(40, 20);
    TextArea eventLog = new TextArea();
    int actionCount = 0;

    /** Builds the applet's interface. */
    public void init() {
     Panel buttonPanel = new Panel();
     buttonPanel.add(myButton);
     buttonPanel.add(myCanvas);
     
     setLayout(new BorderLayout());
     add("North", buttonPanel);
     add("Center", eventLog);
    }

    /** Displays action event information. */
    public boolean action(Event e, Object what) {
     ++actionCount;
     eventLog.appendText("Action event #" + actionCount
                   + "  target's class = "
                   + e.target.getClass().getName() + "\n");
     return true;
    }

}


/**
 * A Canvas subclass that acts as simple button.
 * It changes color to indicate whether it is pressed or not.
 * It generates an action event for each mouseUp event it
 * receives.
 */
class ButtonCanvas extends Canvas {

    int bWidth;
    int bHeight;
    boolean buttonPressed = false;

    /**
     * Creates an oval ButtonCanvas that fits inside
     * a rectangle with the specified width and height.
     */
    public ButtonCanvas(int width, int height) {
     bWidth = width;
     bHeight = height;
    }

    /**
     * Sets the Canvas's size;
     * otherwise it would default to (0, 0).
     */
    public Dimension preferredSize() {
     return new Dimension(bWidth, bHeight);
    }

    /**
     * Sets the Canvas's minimum size to equal its preferred size.
     */
    public Dimension minimumSize() {
     return preferredSize();
    }

    /**
     * Changes the appearance of the ButtonCanvas to reflect
     * the mouseDown as a button press.
     * Returns true to indicate that the event has been
     * completely handled.
     */
    public boolean mouseDown(Event e, int x, int y) {
     buttonPressed = true;
     repaint();
     return true;
    }

    /**
     * Changes the appearance of the ButtonCanvas to reflect
     * the mouseUp as a button release.
     * Generates an action event and posts it to this object.
     * Returns true to indicate that the event has been
     * completely handled.
     */
    public boolean mouseUp(Event e, int x, int y) {
     buttonPressed = false;
     repaint();
     postEvent(new Event(this, Event.ACTION_EVENT, null));
     return true;
    }

    /** Draws an oval button in two different colors. */
    public void paint(Graphics g) {

     if (buttonPressed) {
         g.setColor(Color.red);
     } else {
         g.setColor(Color.green);
     }

     g.fillOval(0, 0, bWidth, bHeight);
    }

}
0
sachinbafnaAuthor Commented:
hi,
Thx a lot for the reply. The example was really very good.
But, I want a button and not an image. Actually I require this for a big Swing based application. That is why I dont want an image to be loaded over the button. It is an application and not an applet program. What i know is, I think you need to call the paint() method and its other associated methods.

Actually, If u would have seen the buttons in IBM Visual Age for Java, or the buttons during Oracle Installation. Something like that I wnat. pls Help me.
0
sachinbafnaAuthor Commented:
hi,
Thx a lot for the reply. The example was really very good.
But, I want a button and not an image. Actually I require this for a big Swing based application. That is why I dont want an image to be loaded over the button. It is an application and not an applet program. What i know is, I think you need to call the paint() method and its other associated methods.

Actually, If u would have seen the buttons in IBM Visual Age for Java, or the buttons during Oracle Installation. Something like that I wnat. pls Help me.
0
keithlongCommented:
I can't give you a button example that looks good, don't have the time to do fancy effects, but what I can give you is an example where you can do it yourself.

It seems, no-one in these forums answers questions that are based on the Swing UI model and thats what this is.

I'm sure you can do a lot of really nice effects from extending the BasicButtonUI good luck

Regards

Keith Long
www.technogenius.co.uk

import javax.swing.JButton;
import javax.swing.JFrame;

import javax.swing.ImageIcon;

public class ButtonTest extends JButton
{
     public ButtonTest(String title)
     {
          super(title);
          setUI(TestButtonUI.createUI(this));

     }

     public static void main(String argv[])
     {
          ButtonTest button = new ButtonTest("(C) www.technogenius.co.uk");
         
          JFrame test = new JFrame("ButtonTest (C) www.technogenius.co.uk");
          test.getContentPane().add(button);

          test.setSize(300,300);
          test.setVisible(true);
         
     }
}

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;

import javax.swing.AbstractButton;
import javax.swing.JComponent;

import javax.swing.plaf.ComponentUI;

import javax.swing.plaf.basic.BasicButtonUI;

public class TestButtonUI extends BasicButtonUI
{
     public static ComponentUI createUI(JComponent c)
     {
          return new TestButtonUI();
     }

     protected void paintFocus(Graphics g, AbstractButton b,
          Rectangle viewRect, Rectangle textRect, Rectangle iconRect)
     {
            Color color = g.getColor();

          // some dull effect
           g.setColor(Color.red);
          for (int i=0; i < 30; i=i+10)
               g.drawRect(0, 0, viewRect.width -i, viewRect.height -i);

          g.setColor(color);
     }
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
girionisCommented:
No comment has been added lately, so it's time to clean up this TA.

I will leave a recommendation in the Cleanup topic area that this question is:

- points to keithlong

Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

girionis
Cleanup Volunteer
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.