Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Custom Swing Button

Posted on 2002-07-18
5
Medium Priority
?
645 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 400 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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This video teaches viewers about errors in exception handling.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

604 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