Solved

Custom Swing Button

Posted on 2002-07-18
5
625 Views
Last Modified: 2013-11-23
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..
0
Comment
Question by:sachinbafna
5 Comments
 
LVL 4

Expert Comment

by:mberumen
ID: 7162598
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
 

Author Comment

by:sachinbafna
ID: 7163972
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
 

Author Comment

by:sachinbafna
ID: 7164011
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
 
LVL 2

Accepted Solution

by:
keithlong earned 100 total points
ID: 7240475
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
 
LVL 35

Expert Comment

by:girionis
ID: 8917570
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

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Books that can get me started on JAVA 2 54
parentbit challenge 3 52
JAVA part two 5 41
Java Timer (static) 9 15
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now