Link to home
Start Free TrialLog in
Avatar of sachinbafna
sachinbafna

asked on

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..
Avatar of mberumen
mberumen

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);
    }

}
Avatar of sachinbafna

ASKER

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.
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.
ASKER CERTIFIED SOLUTION
Avatar of keithlong
keithlong

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of girionis
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