All Downloads are FREE. Search and download functionalities are using the official Maven repository.

test.ComboBoxTest Maven / Gradle / Ivy

Go to download

A Mavenisation of the Quaqua Mac OSX Swing Look and Feel (Java library) Quaqua Look and Feel (C) 2003-2010, Werner Randelshofer. Mavenisation by Matt Gumbley, DevZendo.org - for problems with Mavenisation, see Matt; for issues with Quaqua, see the Quaqua home page. For full license details, see http://randelshofer.ch/quaqua/license.html

The newest version!
/*
 * @(#)ComboBoxTest.java  
 *
 * Copyright (c) 2004 Werner Randelshofer, Immensee, Switzerland.
 * All rights reserved.
 *
 * You may not use, copy or modify this file, except in compliance with the
 * license agreement you entered into with Werner Randelshofer.
 * For details see accompanying license terms.
 */

package test;

import ch.randelshofer.quaqua.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
/**
 * ComboBoxTest.
 *
 * @author  Werner Randelshofer
 * @version $Id: ComboBoxTest.java 363 2010-11-21 17:41:04Z wrandelshofer $
 */
public class ComboBoxTest extends javax.swing.JPanel {
    private static class ColorIcon implements Icon {
        private Color color = Color.white;
        
        public void setColor(Color newValue) {
            color = newValue;
        }
        
        public Color getColor() {
            return color;
        }
        
        
        public int getIconHeight() {
            return 12;
        }
        
        public int getIconWidth() {
            return 24;
        }
        
        public void paintIcon(Component c, Graphics g, int x, int y) {
            if (color != null) {
                g.setColor(color);
                g.fillRect(x, y, getIconWidth(), getIconHeight());
                g.setColor(color.darker());
                g.drawRect(x, y, getIconWidth() - 1, getIconHeight() - 1);
            }
        }
        
    }
    
    private static class ColorComboCellRenderer extends DefaultListCellRenderer {
        private ColorIcon colorIcon = new ColorIcon();
        public ColorComboCellRenderer() {
            setIconTextGap(6);
        }
        @Override
        public Component getListCellRendererComponent(
                JList list,
                Object value,
                int index,
                boolean isSelected,
                boolean cellHasFocus) {
            JLabel l = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            if (value instanceof Color) {
                Color color = (Color) value;
                colorIcon.setColor(color);
                l.setIcon(colorIcon);
                l.setText(color.getRed()+","+color.getGreen()+","+color.getBlue());
            } else {
                l.setIcon(null);
            }
            return l;
        }
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            /*
            g.setColor(new Color(0xafff00f0,true));
           // g.fillRect(0,0,getWidth(),getHeight());
            Insets i = getInsets();
            System.out.println("ColorComboCellRenderer insets:"+i);
            g.fillRect(i.left,i.top,getWidth()-i.left-i.right,getHeight()-i.top-i.bottom);
             **/
        }
    }
    
    
    
    /** Creates new form. */
    public ComboBoxTest() {
        initComponents();
        
        String[] items = new String[100];
        for (int i=0; i < items.length; i++) {
            items[i] = "Item "+(i+1);
        }
        comboBox2.setModel(new javax.swing.DefaultComboBoxModel(items));

        // Setting properties for Apple's AquaLookAndFeel
        smallComboBox1.putClientProperty("JComponent.sizeVariant", "small");
        smallComboBox2.putClientProperty("JComponent.sizeVariant", "small");
        smallComboBox3.putClientProperty("JComponent.sizeVariant", "small");
        
        iconComboBox.setRenderer(new ColorComboCellRenderer());
        DefaultComboBoxModel cbm = new DefaultComboBoxModel();
        cbm.addElement(Color.red);
        cbm.addElement(Color.green);
        cbm.addElement(Color.blue);
        iconComboBox.setModel(cbm);
       
        //         iconComboBox.setFont(UIManager.getFont("SmallSystemFont"));
        
        tableComboBox.putClientProperty("JComboBox.isTableCellEditor",Boolean.TRUE);
    }
    public static void main(String args[]) {
        try {
            UIManager.setLookAndFeel(QuaquaManager.getLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        JFrame f = new JFrame("Quaqua ComboBox Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(new ComboBoxTest());
        ((JComponent) f.getContentPane()).setBorder(new EmptyBorder(9,17,17,17));
        f.pack();
        f.setVisible(true);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // //GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        comboBox1 = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        comboBox2 = new javax.swing.JComboBox();
        comboBox3 = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel4 = new javax.swing.JLabel();
        smallComboBox1 = new javax.swing.JComboBox();
        smallComboBox2 = new javax.swing.JComboBox();
        smallComboBox3 = new javax.swing.JComboBox();
        jSeparator2 = new javax.swing.JSeparator();
        largeComboBox = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        iconComboBox = new javax.swing.JComboBox();
        iconLabel = new javax.swing.JLabel();
        tableComboBox = new javax.swing.JComboBox();

        setBorder(javax.swing.BorderFactory.createEmptyBorder(16, 17, 17, 17));
        setLayout(new java.awt.GridBagLayout());

        comboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6" }));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(8, 0, 0, 0);
        add(comboBox1, gridBagConstraints);

        jLabel2.setText("Non-Editable");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(8, 20, 0, 0);
        add(jLabel2, gridBagConstraints);

        comboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6", "Item 7", "Item 8", "Item 9", "Item 10" }));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(8, 0, 0, 0);
        add(comboBox2, gridBagConstraints);

        comboBox3.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6" }));
        comboBox3.setEnabled(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(8, 0, 0, 0);
        add(comboBox3, gridBagConstraints);

        jLabel3.setText("Disabled");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(8, 20, 0, 0);
        add(jLabel3, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(12, 0, 12, 0);
        add(jSeparator1, gridBagConstraints);

        jLabel4.setText("Small Size");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 20, 0, 0);
        add(jLabel4, gridBagConstraints);

        smallComboBox1.setFont(new java.awt.Font("Lucida Grande", 0, 11));
        smallComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6" }));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(8, 0, 0, 0);
        add(smallComboBox1, gridBagConstraints);

        smallComboBox2.setFont(new java.awt.Font("Lucida Grande", 0, 11));
        smallComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6", "Item 7", "Item 8", "Item 9", "Item 10" }));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridy = 9;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(8, 0, 0, 0);
        add(smallComboBox2, gridBagConstraints);

        smallComboBox3.setFont(new java.awt.Font("Lucida Grande", 0, 11));
        smallComboBox3.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6" }));
        smallComboBox3.setEnabled(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridy = 10;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(8, 0, 0, 0);
        add(smallComboBox3, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(12, 0, 12, 0);
        add(jSeparator2, gridBagConstraints);

        largeComboBox.setFont(new java.awt.Font("Lucida Grande", 0, 18));
        largeComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6" }));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(8, 0, 0, 0);
        add(largeComboBox, gridBagConstraints);

        jLabel5.setText("Large Font");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 20, 0, 0);
        add(jLabel5, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(8, 0, 0, 0);
        add(iconComboBox, gridBagConstraints);

        iconLabel.setText("Icon");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 20, 0, 0);
        add(iconLabel, gridBagConstraints);

        tableComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(8, 0, 0, 0);
        add(tableComboBox, gridBagConstraints);
    }// //GEN-END:initComponents
        
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox comboBox1;
    private javax.swing.JComboBox comboBox2;
    private javax.swing.JComboBox comboBox3;
    private javax.swing.JComboBox iconComboBox;
    private javax.swing.JLabel iconLabel;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JComboBox largeComboBox;
    private javax.swing.JComboBox smallComboBox1;
    private javax.swing.JComboBox smallComboBox2;
    private javax.swing.JComboBox smallComboBox3;
    private javax.swing.JComboBox tableComboBox;
    // End of variables declaration//GEN-END:variables
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy