Member Avatar for jaredleo999

Hi everyone, hope all is well

I have a project which models an ATM machine. There needs to be an option in the welcome screen where the user has the option to set all of the numeric buttons in each class to a certain colour(red, blue or yellow).

Because all of the numeric buttons are not inherited, i do not know of a method which can set a specific set of buttons to a certain colour..

Below is my code of the welcome screen and the balance screen to show how they are linked:

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Dimension;
import java.lang.*;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JComponent;

public class Welcome extends JFrame {
	private Toolkit toolkit;
	private static String passy = "123456";
	JPasswordField password = new JPasswordField(6);
	
	public Welcome() 
	{
		
		
        
        	setSize(1440,900);
		setTitle("Welcome");
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		JPanel panel = new JPanel();
		getContentPane().add(panel);
		panel.setLayout(null);	
		JLabel label = new JLabel("Enter Pin Code:");
		password.setEchoChar('*');
		password.addActionListener(new AL());
		label.setBounds(585,170,290,30);
        	password.setBounds(620,233,200,25);
        	Font font = new Font("Verdana", Font.BOLD, 32);
        	label.setFont(font);
        	label.setForeground(Color.BLACK);
        	
        	
        	
        	
        	
        	
        	
	final  JButton Backg = new JButton();
	Backg.setBounds(565,295,310,410);
        final  JButton b1 = new JButton("1");
	b1.setBounds(570,300,100,100);
	b1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
        password.setText(String.copyValueOf(password.getPassword()) + b1.getText());
        }});
        final JButton b2 = new JButton("2");
	b2.setBounds(670,300,100,100);
	b2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
        password.setText(String.copyValueOf(password.getPassword()) + b2.getText());
        }});
	final JButton b3 = new JButton("3");
	b3.setBounds(770,300,100,100);
	b3.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
        password.setText(String.copyValueOf(password.getPassword()) + b3.getText());
        }});
	final JButton b4 = new JButton("4");
	b4.setBounds(570,400,100,100);
	b4.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
        password.setText(String.copyValueOf(password.getPassword()) + b4.getText());
        }});
	final JButton b5 = new JButton("5");
	b5.setBounds(670,400,100,100);
	b5.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
        password.setText(String.copyValueOf(password.getPassword()) + b5.getText());
        }});
	final JButton b6 = new JButton("6");
	b6.setBounds(770,400,100,100);
	b6.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
        password.setText(String.copyValueOf(password.getPassword()) + b6.getText());
        }});
	final JButton b7 = new JButton("7");
	b7.setBounds(570,500,100,100);
	b7.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
        password.setText(String.copyValueOf(password.getPassword()) + b7.getText());
        }});
	final JButton b8 = new JButton("8");
	b8.setBounds(670,500,100,100);
	b8.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
        password.setText(String.copyValueOf(password.getPassword()) + b8.getText());
        }});
	final JButton b9 = new JButton("9");
	b9.setBounds(770,500,100,100);
	b9.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
        password.setText(String.copyValueOf(password.getPassword()) + b9.getText());
        }});
	final JButton b0 = new JButton("0");
	b0.setBounds(670,600,100,100);
	b0.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
        password.setText(String.copyValueOf(password.getPassword()) + b0.getText());
        }});
	final JButton Enter = new JButton("Enter");
	Enter.setBounds(770,600,100,100);
	Enter.addActionListener(new AL());
        final JButton Clear = new JButton("Clear");
	Clear.setBounds(570,600,100,100);
	Clear.addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent event) {
        password.setText("");}});
        
	
	panel.add(b1);
	panel.add(b2);
	panel.add(b3);
	panel.add(b4);
	panel.add(b5);
	panel.add(b6);
	panel.add(b7);
	panel.add(b8);
	panel.add(b9);
	panel.add(b0);
	panel.add(Enter);
	panel.add(Clear);
        panel.add(password);
	panel.add(label);
	panel.add(Backg);
	}
       
       class AL implements ActionListener{
        	public void actionPerformed(ActionEvent e) {
        	//JPasswordField input = (JPasswordField) e.getSource();
        		char [] pass = password.getPassword();
        	String p = new String(pass);
        	if (p.equals(passy)){
        		JOptionPane.showMessageDialog(null, "Correct");
        		setVisible(false);
		dispose();
		ATM();
        	}
        	else 
        		JOptionPane.showMessageDialog(null, "InCorrect");
        	
        	}
        }
        
         public void Keypad() {
 	 Keypad keypad = new Keypad();
 	 keypad.setVisible(true);
 }
public void ATM() {
     ATM atm = new ATM();
     atm.setVisible(true);  
 }  



}
        
        ___________________________________________
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Dimension;
import java.lang.*;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JComponent;


public class Balance extends JFrame {

public Balance() {

setSize(1440,900);
setTitle("Balance");
setDefaultCloseOperation(EXIT_ON_CLOSE);

	Driver D = new Driver();
	String £ = "£";
	String Blnc = Integer.toString(D.Money);
        JPanel panel = new JPanel();
        getContentPane().add(panel);
	panel.setLayout(null);	
	
	JLabel label = new JLabel("Your Balance is:");
	label.setBounds(580,170,450,50);
	JLabel Balance = new JLabel(£ + Blnc);
	Balance.setBounds(705,233,200,25);
	Font font = new Font("Verdana", Font.BOLD, 32);
        label.setFont(font);
        label.setForeground(Color.BLACK);
		
	final  JButton Backg = new JButton();
	Backg.setBounds(565,295,310,410);
        final  JButton b1 = new JButton("1");
	b1.setBounds(570,300,100,100);
        final JButton b2 = new JButton("2");
	b2.setBounds(670,300,100,100);
	final JButton b3 = new JButton("3");
	b3.setBounds(770,300,100,100);
	final JButton b4 = new JButton("4");
	b4.setBounds(570,400,100,100);
	final JButton b5 = new JButton("5");
	b5.setBounds(670,400,100,100);
	final JButton b6 = new JButton("6");
	b6.setBounds(770,400,100,100);
	final JButton b7 = new JButton("7");
	b7.setBounds(570,500,100,100);
	final JButton b8 = new JButton("8");
	b8.setBounds(670,500,100,100);
	final JButton b9 = new JButton("9");
	b9.setBounds(770,500,100,100);
	final JButton b0 = new JButton("0");
	b0.setBounds(670,600,100,100);
	final JButton Enter = new JButton("Enter");
	Enter.setBounds(770,600,100,100);
        final JButton Clear = new JButton("Clear");
	Clear.setBounds(570,600,100,100);
	final JButton back = new JButton("Back");
        back.setBounds(0,10,200,30);
        back.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
       setVisible(false);
           dispose();
        ATM();
        }});

	
       JButton close = new JButton("Close");
       close.setBounds(1240,10,200,30);
       close.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent event) {
               System.exit(0);
          }
       });
	panel.add(b1);
	panel.add(b2);
	panel.add(b3);
	panel.add(b4);
	panel.add(b5);
	panel.add(b6);
	panel.add(b7);
	panel.add(b8);
	panel.add(b9);
	panel.add(b0);
	panel.add(Enter);
	panel.add(Clear);
	panel.add(label);
	panel.add(Backg);
	panel.add(back);
	panel.add(close);
	panel.add(Balance);
}

 public void ATM() {
    	    
    	
     ATM atm = new ATM();
     atm.setVisible(true);
     
 }
 
 
}

Sorry about all the code, but i am unsure how to explain this in any other way.
Thanks

You need to main the references to the buttons if you want to be able to alter their properties. An array or list would suffice.

Just so you're aware, many of the LookAndFeel implementations don't show button background changes well. JLabels set to opaque with a raised bevel border might work better for you.

This is an old bug that raised in the java version 1.4.2 on Windows OS with the WindowsLookAndFeel; if you try to set the background color only the edges of the JButton are colored ,
This bug is noticed not only on JButton but on range of swing object like text field, tabs ., progress bar , tool bar buttons…; the situation in touch screens is more worst.
To avoid this you should override the paintComponent() method;

or to call setContentAreaFilled(false) followed by setOpaque(true) on the button like this:
button.setContentAreaFilled(false);
button.setOpaque(true);
but you still have to deal with borders.

Another alternative is to write your own button by extending the BasicButtonUI.

Hope this help.

Member Avatar for jaredleo999

Thanks for your help so far

I have: b1.setForeground(Color.BLUE);
the button labelled b1 turns blue and all is working,
However is it possible to use some function like:

String ButtonColor = "BLUE";
b1.setForeground(Color.(ButtonColor.getText()));

Any help appreciated thanks!

Sure, you can keep a variable for button color. Use Color though and not String

Color buttonColor = Color.BLUE;

and if you keep a list of the buttons, you can set it like

for (JButton button : buttonList){
  button.setForeground(buttonColor);
}

You can use a radio button group or combo box for color choice to update your variable.

Edit: oops, cross-post with Nick and it looks like I misunderstood your question.

That question was also asked here. Do not flood the forum by asking the same question more then once.

This code sniplet is an exemple of colorized JButton and it is nice enjoy.

import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
 
public class ColorizedButton extends JButton {
    private static ColorizeImageFilter imageFilter = new ColorizeImageFilter();
 
    private Color color;
 
    public ColorizedButton(String title, Color color) {
	super(title);
	this.color = color;
    }
 
    public void paintComponent(Graphics g) {
	boolean opaque = isOpaque();
	if (opaque) {
	    // Fill background
	    g.setColor(getBackground());
	    g.fillRect(0, 0, getWidth(), getHeight());
	}
 
	// Let the UI paint to offscreen image
	setOpaque(false);
	Image img = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
	Graphics g2 = img.getGraphics();
	g2.setFont(g.getFont());
	super.paintComponent(g2);
 
	// Colorize!
	img = imageFilter.colorize(this, img, color);
 
	// Paint offscreen image to button
	g.drawImage(img, 0, 0, null);
	setOpaque(opaque);
    }
 
    private static class ColorizeImageFilter extends RGBImageFilter {
	double cr, cg, cb;
	int bgRGB, fgRGB;
 
	public ColorizeImageFilter() {
	    canFilterIndexColorModel = true;
	}
 
	public Image colorize(JComponent comp, Image fromImage, Color c) {
	    cr = c.getRed()   / 255.0;
	    cg = c.getGreen() / 255.0;
	    cb = c.getBlue()  / 255.0;
	    bgRGB = comp.getBackground().getRGB();
	    fgRGB = comp.getForeground().getRGB();
	    ImageProducer producer = new FilteredImageSource(fromImage.getSource(), this);
	    return new ImageIcon(comp.createImage(producer)).getImage();
	}
 
	public int filterRGB(int x, int y, int rgb) {
	    int alpha = rgb & 0xff000000;
 
	    if (rgb == bgRGB || rgb == fgRGB || alpha < 0x80000000) {
		return rgb;
	    }
 
	    // Assume all rgb values are shades of gray
	    double grayLevel = 2 * (rgb & 0xff) / 255.0;
	    double r, g, b;
 
	    if (grayLevel <= 1.0) {
		r = cr * grayLevel;
		g = cg * grayLevel;
		b = cb * grayLevel;
            } else {
		grayLevel -= 1.0;
		r = cr + (1.0 - cr) * grayLevel;
		g = cg + (1.0 - cg) * grayLevel;
		b = cb + (1.0 - cb) * grayLevel;
            }
 
	    return (alpha +
		    (((int)(r * 255)) << 16) +
		    (((int)(g * 255)) << 8) +
		    (int)(b * 255));
	}
    }
 
    public static void main(String[] args) throws Exception {
	UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
	JFrame frame = new JFrame();
	Container cp = frame.getContentPane();
	cp.setLayout(new FlowLayout());
	cp.add(new ColorizedButton("Red",   Color.red));
	cp.add(new ColorizedButton("Green", Color.green));
	cp.add(new ColorizedButton("Blue",  Color.blue));
	frame.setBounds(100, 100, 200, 100);
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.setVisible(true);
    }
}
Hope it helps.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.