MainClass.java Source code

Java tutorial

Introduction

Here is the source code for MainClass.java

Source

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.Icon;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.colorchooser.AbstractColorChooserPanel;
import javax.swing.colorchooser.ColorSelectionModel;

public class MainClass {

    public static void main(String[] a) {
        final JColorChooser colorChooser = new JColorChooser();
        SystemColorChooserPanel newChooser = new SystemColorChooserPanel();
        colorChooser.addChooserPanel(newChooser);

        ActionListener okActionListener = new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                System.out.println(colorChooser.getColor());
            }
        };

        ActionListener cancelActionListener = new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                System.out.println("Cancel");
            }
        };

        final JDialog dialog = JColorChooser.createDialog(null, "Change Button Background", true, colorChooser,
                okActionListener, cancelActionListener);
        dialog.setVisible(true);
    }
}

class SystemColorChooserPanel extends AbstractColorChooserPanel implements ItemListener {

    JComboBox comboBox;

    String labels[] = { "BLACK", "BLUE", "CYAN" };

    Color colors[] = { Color.BLACK, Color.BLUE, Color.CYAN };

    private void setColor(Color newColor) {
        int position = findColorPosition(newColor);
        comboBox.setSelectedIndex(position);
    }

    private int findColorLabel(Object label) {
        String stringLabel = label.toString();
        int position = -1;
        for (int i = 0, n = labels.length; i < n; i++) {
            if (stringLabel.equals(labels[i])) {
                position = i;
                break;
            }
        }
        return position;
    }

    private int findColorPosition(Color color) {
        int position = colors.length - 1;
        int colorRGB = color.getRGB();
        for (int i = 0, n = colors.length; i < n; i++) {
            if ((colors[i] != null) && (colorRGB == colors[i].getRGB())) {
                position = i;
                break;
            }
        }
        return position;
    }

    public void itemStateChanged(ItemEvent itemEvent) {
        int state = itemEvent.getStateChange();
        if (state == ItemEvent.SELECTED) {
            int position = findColorLabel(itemEvent.getItem());
            if ((position != -1) && (position != labels.length - 1)) {
                ColorSelectionModel selectionModel = getColorSelectionModel();
                selectionModel.setSelectedColor(colors[position]);
            }
        }
    }

    public String getDisplayName() {
        return "SystemColor";
    }

    public Icon getSmallDisplayIcon() {
        return new MyIcon(Color.BLUE);
    }

    public Icon getLargeDisplayIcon() {
        return new MyIcon(Color.GREEN);
    }

    protected void buildChooser() {
        comboBox = new JComboBox(labels);
        comboBox.addItemListener(this);
        add(comboBox);
    }

    public void updateChooser() {
        Color color = getColorFromModel();
        setColor(color);
    }
}

class MyIcon implements Icon {
    Color myColor;

    public MyIcon(Color myColor) {
        this.myColor = myColor;
    }

    public int getIconWidth() {
        return 16;
    }

    public int getIconHeight() {
        return 16;
    }

    public void paintIcon(Component c, Graphics g, int x, int y) {
        g.setColor(myColor);
        g.drawRect(0, 0, 16, 16);
    }
}