/** Eine_AusSechsFarben.java*/import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Eine_AusSechsFarben extends JFrame { public Eine_AusSechsFarben() { super("DEMO: Eine aus sechs Farben/Symbolen"); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setSize(400,300); setLocationRelativeTo(null); //JComponents erzeugen: toolbar = new JToolBar(); paletteBtn = new JButton("Farbe wählen"); paletteBtn2 = new JButton("Symbol wählen"); mainPanel = new JPanel(); btn = new JButton(); //Layout: toolbar.setFloatable(false); toolbar.add(paletteBtn); toolbar.add(paletteBtn2); getContentPane().add(toolbar, BorderLayout.NORTH); mainPanel.add(btn); getContentPane().add(mainPanel,BorderLayout.CENTER); //Listeners: paletteBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { paletteBtnActionPerformed(evt); } }); paletteBtn2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { paletteBtn2ActionPerformed(evt); } }); } private void paletteBtnActionPerformed(java.awt.event.ActionEvent evt) { chooser = new ColorChooser(this,true,false); Color color = chooser.getSelectedColor(); btn.setBackground(color); validate(); } private void paletteBtn2ActionPerformed(java.awt.event.ActionEvent evt) { chooser = new ColorChooser(this,true,true); Icon icon = chooser.getSelectedIcon(); btn.setIcon(icon); validate(); } public static void main(String args[]) { new Eine_AusSechsFarben().setVisible(true); } private ColorChooser chooser; private JButton paletteBtn,paletteBtn2, btn; private JToolBar toolbar; private JPanel mainPanel ;}class ColorChooser extends JDialog implements ActionListener{ public ColorChooser(Frame parent, boolean modal, boolean isIconChooser) { super(parent, modal); this.isIconChooser=isIconChooser; setSize(380,170); setLocationRelativeTo(parent); setUndecorated(true); //JComponents erzeugen: colorChooser = new JPanel(); chooser = new JPanel(); preview = new JPanel(); preview1 = new JButton(); okB = new JButton("OK"); //Layout: chooser.setLayout(new GridLayout(6, 1, 1, 1)); chooser.setPreferredSize(new Dimension(50, 120)); preview.setPreferredSize(new Dimension(240, 40)); preview.add(preview1); if(!isIconChooser){ initColors(); for( int i =0; i<colors.length; i++ ){ addButton(colors[i]); } } else{ initIcons(); for( int i =0; i<icons.length; i++ ){ addButton(icons[i]); } } colorChooser.add(chooser); colorChooser.add(preview); colorChooser.add(okB); getContentPane().add(colorChooser, BorderLayout.CENTER); //Listener: okB.addActionListener(this); okB.setEnabled(false); setVisible(true); } private JButton addButton(Object obj) { JButton button = new JButton(); if(!isIconChooser){ button.setBackground((Color) obj); } else{ button.setIcon((Icon) obj); } button.setRolloverEnabled(false); chooser.add(button); button.addActionListener(this); return(button); } public void actionPerformed(ActionEvent e) { if( e.getSource() == okB ){ setVisible(false); } else{ if(!isIconChooser){ JButton button = (JButton)e.getSource(); Color bg = button.getBackground(); preview1.setBackground(bg); okB.setEnabled(true); } else{ JButton button = (JButton)e.getSource(); Icon bg = button.getIcon(); preview1.setIcon(bg); okB.setEnabled(true); } } } public Color getSelectedColor(){ Color selectedColor = null; selectedColor = preview1.getBackground(); return selectedColor; } public Icon getSelectedIcon(){ Icon selectedIcon = null; selectedIcon = preview1.getIcon(); return selectedIcon; } private void initColors() { int numColors = colorValues.length / 3; colors = new Color[numColors]; for (int i = 0; i < numColors ; i++) { colors[i] = new Color( colorValues[(i*3)], colorValues[(i*3)+1], colorValues[(i*3)+2] ); } } private void initIcons() { int numIcons = iconValues.length; icons = new Icon[numIcons]; for (int i = 0; i < numIcons; i++) { icons[i] = new ImageIcon( iconValues[i] ); } } private final Color DEFAULT_COLOR = (new JButton()).getBackground(); private JPanel colorChooser, preview; private JPanel chooser; private JButton preview1, okB; private Color[] colors; private boolean isIconChooser; private int[] colorValues = { 255, 255, 255, 0, 255, 255, 0, 0, 255, 255, 0 , 0, 0, 255, 0, 0, 0, 0, }; private Icon[] icons; private String[] iconValues = { "toolbarButtonGraphics\\general\\About16.gif", "toolbarButtonGraphics\\general\\Add16.gif", "toolbarButtonGraphics\\general\\AlignBottom16.gif", "toolbarButtonGraphics\\general\\Bookmarks16.gif", "toolbarButtonGraphics\\general\\ComposeMail16.gif", "toolbarButtonGraphics\\general\\ContextualHelp16.gif", };}