I'm writign a POS system(point of sale) for fun. A typical feature of a POS system is buttons that have different background colors for different products. But I don't mean the background behind the button's image, i mean like in this random picture I got off google images:
Edit: Note I changed the look and feel to the system one
I need to do something like THAT with my buttons. I know a few ways this COULD be possible:
- Creating a look and feel for my buttons?
- Completely overriding the paintComponenet method in my JButtons(But that's a pretty pathetic way to do it... and the border + text isn't drawn when I override that... obviously a bad idea)
- Using button.setContentAreaFilled(false);, and putting a JPanel of the same size as the button behind the button.
I don't really know how to create my own look and feel, and it sounds like a pain, especially just for 1 button, and I heard something about breaking the look and feel which scared me away from that idea. The 3rd way sounds plausible, and not extremely difficult, but I what is the very best way to do what I am trying to do?
Right now my code for creating butons is this:
JButton b = new JButton(text);
b.addActionListener(this);
b.setFont(Main.f);
b.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
buttons.add(b);
return b;
I've messed around with this code(Mostly for fun, not practicality):
JButton b = new JButton(text){
public void paintComponent(Graphics g){
super.paintComponent(g);
Color bg = getBackground();
int borderchange = -50;
g.setColor(new Color(
Math.max(0,bg.getRed()+borderchange),
Math.max(bg.getGreen()+borderchange,0),
Math.max(bg.getBlue()+borderchange,0)));
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(getBackground());
int border = 4;
g.fillRect(border,border,getWidth()-border,getHeight()-border);
g.setColor(Color.BLACK);
g.setFont(getFont());
g.drawString(getText(),getWidth()/2,getHeight()/2);
}
};