This week's book giveaways are in the Cloud and AI/ML forums.
We're giving away four copies each of Cloud Native Patterns and Natural Language Processing and have the authors on-line!
See this thread and this one for details.
Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Custom Buttons?

 
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!