GeeCON Prague 2014*
The moose likes Swing / AWT / SWT and the fly likes Custom Buttons? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Custom Buttons?" Watch "Custom Buttons?" New topic
Author

Custom Buttons?

James Hodgkiss
Ranch Hand

Joined: Jan 22, 2004
Posts: 401
I want to create a GUI that has custom buttons (oval-shaped, coloured, etc.).

Can anyone tell me the best/easiest way of doing this? If Look and Feel is my only option, how would I go about creating a new Look And Feel?

Cheers,
James
Ko Wey
Ranch Hand

Joined: Sep 08, 2003
Posts: 67
Try something like this:

//file Ellipse.java

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

public class EllipseButton extends JPanel
implements MouseListener{

private String title = null;
private Vector listeners = null;
private boolean hit = false;

public EllipseButton (String title){
super();
this.title = title;
listeners = new Vector();
addMouseListener(this);
}

public Dimension getPreferredSize(){return new Dimension(150,75);}

public void paintComponent(Graphics g){
Graphics2D g2D = (Graphics2D)g;
super.paintComponent(g);
if (hit==true){
g2D.setColor(Color.green);
}else{
g2D.setColor(Color.yellow);
};
g2D.fillOval(0,0,getWidth()-2,getHeight()-2);
g2D.setColor(Color.black);
g2D.drawOval(0,0,getWidth()-2,getHeight()-2);
g2D.drawString(title,10,getHeight()/2);
}

public void mousePressed(MouseEvent e){
hit=true;
repaint();
}

public void mouseReleased(MouseEvent e){
hit=false;
repaint();
}

public void mouseClicked(MouseEvent e){
fireEvent(new ActionEvent(this,0,title));
}

public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}

public void addActionListener(ActionListener listener){
listeners.addElement(listener);
}

public void removeActionListener(ActionListener listener){
listeners.removeElement(listener);
}

private void fireEvent(ActionEvent event){
for (int i = 0;i<listeners.size() ;i++ ){
ActionListener listener = (ActionListener)listeners.elementAt(i);
listener.actionPerformed(event);
};
}

public static void main(String[] args){//TEST
JFrame jFrame = new JFrame();
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
EllipseButton button = new EllipseButton("Special button");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("I am clicked!");
}
});
Container cont = jFrame.getContentPane();
cont.setLayout(new FlowLayout());
cont.add(new JLabel("TEST ME:"));
cont.add(button);
jFrame.pack();
jFrame.setVisible(true);
}

}//end class

Raghavendra nandavar
Ranch Hand

Joined: Jun 26, 2005
Posts: 231
Don't know much about AWT/Swings, but easiest way would
be to use images (Of whatever shape and size) and adding
listeners to it...
Ko Wey
Ranch Hand

Joined: Sep 08, 2003
Posts: 67
Mr Raghavendra nandavar, this is what the example tried to demostrate:
in paintComponent(Graphics g) one can just as easily put images (g.drawImage(some image,...))
or paint lines, ellipses, rectangles,...
Mouselistener's mouseClicked method to fire an Actionevent, which is what one normally expects from a button... MouseListeners mousePressed and mouseReleased were used to show the user he touched the custom button
James Hodgkiss
Ranch Hand

Joined: Jan 22, 2004
Posts: 401
Ko and Raghavendra,

I've done it using images in a JPanel, but was wondering if there was a quicker way. Many thanks for your replies - much appreciated.

Cheers,
James
Ko Wey
Ranch Hand

Joined: Sep 08, 2003
Posts: 67
May be you could take some time to make a (abstract) generic custom button ( with the method paintComponent(g) abstract) and each time you want a special button, you extend this generic button: you just have to tweek paintComponent(g) to whatever your fantasy dictates. This is not working, this is art and fun!

KEEP IT IN A SAFE PLACE!!!

Here a ovalbutton:



And here and imageButton

 
Consider Paul's rocket mass heater.
 
subject: Custom Buttons?