• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Custom Buttons?

 
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Destiny's powerful hand has made the bed of my future. And this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic