0

i want to know how to set the color of a button (in example, but i will need to set every component color) with the apple look and feel.

I found an answer in stackoverflow that suggest to change to the standard look and feel, that works for me, but i prefer not to change because I like apple's one.

Is there any solution? I know there is because I saw many apps written in java that have colored buttons and also that use particular styles or images as background.

Can you tell me a solution?

Gianmarco
  • 2,536
  • 25
  • 57

1 Answers1

0

Extend the Jbutton class and in that override the repaint() method and call setBackground(COLOR.ORANGE), to change the button color.

Now use this class to create all your buttons. If you wish to change color of a specific button, call the setBackground(COLOR.ORANGE) method on that specific button. Hope this helps. Have a look at the code below

package solutions;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.InputVerifier;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class VerifierTest extends JFrame {

    private static final long serialVersionUID = 1L;

    public VerifierTest() {
        final JTextField tf = new JTextField("TextField1");

        getContentPane().add(tf, BorderLayout.NORTH);
        tf.setInputVerifier(new PassVerifier());

        final JTextField tf2 = new JTextField("TextField2");

        getContentPane().add(tf2, BorderLayout.SOUTH);
        tf2.setInputVerifier(new PassVerifier());

        final JButton b = new JButton("Button");
        b.setBackground(Color.ORANGE);
        b.setVerifyInputWhenFocusTarget(true);
        getContentPane().add(b, BorderLayout.EAST);
        b.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (!tf.getInputVerifier().verify(tf)) {
                    JOptionPane.showMessageDialog(tf.getParent(), "illegal value: " + tf.getText(), "Illegal Value",
                            JOptionPane.ERROR_MESSAGE);
                }
                if (b.isFocusOwner()) {
                    System.out.println("Button clicked");
                }
            }
        });
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        Frame frame = new VerifierTest();
        frame.setSize(400, 200);
        frame.setVisible(true);
    }

    class PassVerifier extends InputVerifier {

        @Override
        public boolean verify(JComponent input) {
            final JTextField tf = (JTextField) input;
            String pass = tf.getText();
            if (pass.equals("Manish")) {
                return true;
            } else {
                return false;
            }
        }
    }
}

Comment the line "b.setBackground(Color.ORANGE);" and see the difference.

dareurdream
  • 241
  • 4
  • 13
  • I tried, but this method is not working at all. I create the class button, I override the repaint method and put setBackgrount(Color.ORANGE) in it, then I override the constructor Button(String name) with super(name). But nothing change. Buttons are still standard light grey. I really don't know how to do, I do not want to use multi-platform Look and Feel, because my program will run on mac (and mac only) and the Aqua (of mac) is really beautiful. – Gianmarco Sep 27 '12 at 23:16
  • @Gianmarco - No need to override the repaint method. Just look at the code above. Post an [SSCCE](http://www.sscce.org) of your code to help you better. – dareurdream Sep 28 '12 at 11:08