Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

Custom JButton

Thread view: 
Saj - 11 Apr 2006 07:30 GMT
hey i want to create a customized JButton.......... can anyone help me
out plz........ i want to create a button in any given shape (say e.g.
in the shape of an animal like elephant or dog or etc...) so how can i
change the shape of a button??????? i do NOT want to, however, display
an image on the button rather want to reshape the button in that
form.....plz help
Alex Hunsley - 11 Apr 2006 12:17 GMT
> hey i want to create a customized JButton.......... can anyone help me
> out plz........ i want to create a button in any given shape (say e.g.
> in the shape of an animal like elephant or dog or etc...) so how can i
> change the shape of a button??????? i do NOT want to, however, display
> an image on the button rather want to reshape the button in that
> form.....plz help

Firstly, if you take more care with your writing, it will grate less on
the people reading your post, and they will concentrate more on your
question and helping you. So try using some capital letters (and not for
SHOUTING), and use punctuation marks only once. (what do you think
'???????' does? Make people more likely to answer? It may have the
opposite effect.)

As for your question, you may want to try overriding JButton and
override the paintComponent() method to do some custom rendering. That
rendering would paint a bitmap of the shape you want. Note also that if
you want it to behave visually like a button you should be prepared to
change how you render it for the 'button held down' state as well (e.g.
add drop shadow effect to the graphic or something similar: look at how
normal buttons behave when you hold down the mouse button over them).
Drazen Gemic - 11 Apr 2006 19:28 GMT
This is not JButton overriden, but it might give you some ideas.
This is a nice looking hyperlink component derived from JComponent.

The code is mine and it is just released under BSD license.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class ActiveLabel extends JComponent {
public final int XMARGIN=2,YMARGIN=2;
public final int ACTIVE=1,IDLE=0,PRESSED=2;
static final Color[] clr = { Color.blue, Color.red, Color.black };
ActionListener lst;
String txt,cmd;
public int state;
int align;
// ctor
public ActiveLabel(String str,int alg)
   {
   align=alg;
   cmd=txt=str;
   state=IDLE;
   enableEvents(AWTEvent.MOUSE_EVENT_MASK );
   lst=null;
   }
public ActiveLabel(String str)
   {
   this(str,Label.LEFT);
   }
public ActiveLabel()
   {
   this("",Label.LEFT);
   }
// paint
public void paint(Graphics g)
   {
   FontMetrics fm=getFontMetrics(getFont());
   int wt=fm.stringWidth(txt);
   int ht=fm.getHeight();
   Rectangle r=getBounds();
   int w=r.width;
   int h=r.height;
   int x=(w-wt) >> 1;
   int y=((h+ht) >> 1)-2;
   if(align == Label.LEFT) x=0;
   else if(align == Label.RIGHT) x=w-wt;
   g.setColor(clr[state]);
   g.drawString(txt,x,y);
   g.drawLine(x,y+1,x+wt,y+1);
   }
// getMinimumSize
public Dimension getMinimumSize()
   {
   FontMetrics fm=getFontMetrics(getFont());
   int h=fm.getHeight() + YMARGIN + YMARGIN;
   int w=fm.stringWidth(txt) + XMARGIN + XMARGIN;
   return new Dimension(w,h);
   }
// minimumSize
public Dimension minimumSize()
   {
   return getMinimumSize();
   }
// preferredSize
public Dimension preferredSize()
   {
   return getMinimumSize();
   }
// getPreferredSize
public Dimension getPreferredSize()
   {
   return getMinimumSize();
   }
//
public void released()
   {
   int a=state;
   state=ACTIVE;
   if(a != state) repaint();
   postAction();
   }
//
public void pressed()
   {
   int a=state;
   state=PRESSED;
   if(a != state) repaint();
   }
//
public void entered()
   {
   int a=state;
   state=ACTIVE;
   if(a != state) repaint();
   }
//
public void exited()
   {
   int a=state;
   state=IDLE;
   if(a != state) repaint();
   }
//
public void processEvent(AWTEvent e)
   {
   if(e.getID() == MouseEvent.MOUSE_PRESSED) pressed();
   else if(e.getID() == MouseEvent.MOUSE_ENTERED) entered();
   else if(e.getID() == MouseEvent.MOUSE_EXITED) exited();
   else if(e.getID() == MouseEvent.MOUSE_RELEASED) released();
   super.processEvent(e);
   }
//
public void setActionCommand(String c)
   {
   cmd=c;
   }
//
public void addActionListener(ActionListener l)
   {
   lst=AWTEventMulticaster.add(lst,l);
   }
//
public void removeActionListener(ActionListener l)
   {
   lst=AWTEventMulticaster.remove(lst,l);
   }
//
private void postAction()
   {
   if(lst != null)
      {
      ActionEvent event = new
ActionEvent(this,ActionEvent.ACTION_PERFORMED,cmd);
      lst.actionPerformed(event);
      }
   }
}
Alex Hunsley - 12 Apr 2006 01:04 GMT
> This is not JButton overriden, but it might give you some ideas.
> This is a nice looking hyperlink component derived from JComponent.
[quoted text clipped - 10 lines]
> static final Color[] clr = { Color.blue, Color.red, Color.black };
> ActionListener lst;

Thanks for contributing your code, Drazen!

A little feedback for you...
Are you aware that class members without an explicit access modifier
(private, protected or public) are made public to other classes in the
same package? So any class in the same package can reach in and directly
read (or modify) your ActionListener list which breaks encapsulation. If
something should be private, I suggest you make it explicitly private.

Also, I suggest that you mark your constants 'static' as well as final,
e.g.:
public static final int XMARGIN=2,YMARGIN=2;
      ^^^^^^

[snip]
> public void paint(Graphics g)
>    {
>    FontMetrics fm=getFontMetrics(getFont());
[snip]

Btw, it's usual to override paintComponent rather than paint, unless you
have good reason. By overriding paint directly you are breaking the
rendering of any borders that may have been set on the component...

Also I notice your component has its text set at construction time, and
the text doesn't change thereafter. So it's a little wasteful to do
those calculations on the size of the text etc. each and every time
paint gets called (or getMinimumSize) - you may as well siphon off that
work into a seperate method, and only call it the first time it is
needed, and then cache the results for later reuse.
Drazen Gemic - 12 Apr 2006 13:35 GMT
Thanks for the suggestion. This class was created in a very short
time and it is a part of a project that was (yes, I swear it was),
finished on schedule.

DG
Alex Hunsley - 12 Apr 2006 18:21 GMT
> Thanks for the suggestion. This class was created in a very short
> time and it is a part of a project that was (yes, I swear it was),
> finished on schedule.

Well, if you ever feel like doing some refactoring, you have some
suggestion handy!
cheers,
alex


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.