⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 imagedcomboboxcelleditorfactory.java

📁 一个用于排队系统仿真的开源软件,有非常形象的图象仿真过程!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                    setIcon(ImageLoader.loadImage(sd.getStationType(key)+"Combo"));
                }
                else if (key == null) {
                    // Null component
                    setText("");
                }
            }
            else if (!isString){
                // This is a class
                if (cd.getClassName(key) != null && !cd.getClassName(key).equals("")) {
                    setText(cd.getClassName(key));
                    switch(cd.getClassType(key)) {
                        case CommonConstants.CLASS_TYPE_CLOSED:
                            setIcon(ImageLoader.loadImage("ClosedCombo"));
                            break;
                        case CommonConstants.CLASS_TYPE_OPEN:
                            setIcon(ImageLoader.loadImage("OpenCombo"));
                            break;
                    }
                }
                else if (key == null) {
                    // Null component
                    setText(CommonConstants.ALL_CLASSES);
                }

            }
            else {
                // This is only a string
                setText((String)key);
                setIcon(ImageLoader.loadImage(key+"Combo"));
            }
        }

        /**
         * Gets search's key for rendered object (class or station)
         * @return search's key for rendered object (class or station)
         */
        public Object getKey() {
            return key;
        }
    }

    /**
     * Returns an instance of Imaged combobox renderer
     * @return an instance of Imaged combobox renderer
     */
    public TableCellRenderer getRenderer() {
        if (renderer == null)
            renderer = new ImagedComboRenderer();
        return renderer;
    }

    /**
     * This class is used to display a custom renderer into the comboBox with
     * Jlabels generated by getDrawComponent method.
     */
    protected class ComboImageRenderer implements ListCellRenderer {

        /**
         * Simply uses value param as a renderer (as we pass a JLabel)
         */
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            JLabel component = (JLabel) value;
            if (component == null)
                component = nullRenderer;
            if (isSelected) {
                component.setBackground(list.getSelectionBackground());
                component.setForeground(list.getSelectionForeground());
            }
            else {
                component.setBackground(list.getBackground());
                component.setForeground(list.getForeground());
            }
            return component;
        }
    }

    protected class ImagedComboRenderer implements TableCellRenderer {
        protected JComboBox combo = new JComboBox();

        /**
         * Creates a new ImagedComboRenderer and sets renderer for comboBox.
         */
        public ImagedComboRenderer() {
            combo.setRenderer(new ComboImageRenderer());
        }
        /**
         * Returns the component used for drawing the cell.  This method is
         * used to configure the renderer appropriately before drawing.
         *
         * @param    table        the <code>JTable</code> that is asking the
         * renderer to draw; can be <code>null</code>
         * @param    value        the value of the cell to be rendered.  It is
         * up to the specific renderer to interpret
         * and draw the value.  For example, if
         * <code>value</code>
         * is the string "true", it could be rendered as a
         * string or it could be rendered as a check
         * box that is checked.  <code>null</code> is a
         * valid value
         * @param    isSelected    true if the cell is to be rendered with the
         * selection highlighted; otherwise false
         * @param    hasFocus    if true, render cell appropriately.  For
         * example, put a special border on the cell, if
         * the cell can be edited, render in the color used
         * to indicate editing
         * @param    row     the row index of the cell being drawn.  When
         * drawing the header, the value of
         * <code>row</code> is -1
         * @param    column     the column index of the cell being drawn
         */
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            Component renderer = getDrawComponent(value);
            if (table.isCellEditable(row, column)) {
                // If the cell is editable, returns a comboBox
                combo.removeAllItems();
                if (value != null) {
                    combo.addItem(renderer);
                    combo.setSelectedItem(value);
                }
                if (!isSelected) {
                    combo.setBackground(table.getBackground());
                    combo.setForeground(table.getForeground());
                }
                else {
                    combo.setBackground(table.getSelectionBackground());
                    combo.setForeground(table.getSelectionForeground());
                }
                return combo;
            }
            else {
                // Otherwise returns the label only.
                if (!isSelected) {
                    renderer.setBackground(table.getBackground());
                    renderer.setForeground(table.getForeground());
                }
                else {
                    renderer.setBackground(table.getSelectionBackground());
                    renderer.setForeground(table.getSelectionForeground());
                }
                return renderer;
            }
        }
    }

    /**
     * This is a combobox editor. It will recycle the same combobox changing items
     */
    protected class ImagedComboEditor extends DefaultCellEditor {
        protected JComboBox combo;

        /**
         * Creates a new ImagedComboEditor and sets renderer for comboBox.
         */
        public ImagedComboEditor() {
            super(new JComboBox());
            combo = (JComboBox) super.getComponent();
            combo.setRenderer(new ComboImageRenderer());
        }

        /**
         * Changes data shown in this combobox
         * @param data array with LabelRenderers to be shown
         */
        public void setData(LabelRenderer[] data) {
            combo.removeAllItems();
            for (int i=0; i<data.length; i++)
                combo.addItem(data[i]);
        }

        /**
         * Sets an initial <code>value</code> for the editor.  This will cause
         * the editor to <code>stopEditing</code> and lose any partially
         * edited value if the editor is editing when this method is called. <p>
         * <p/>
         * Returns the component that should be added to the client's
         * <code>Component</code> hierarchy.  Once installed in the client's
         * hierarchy this component will then be able to draw and receive
         * user input.
         *
         * @param    table        the <code>JTable</code> that is asking the
         * editor to edit; can be <code>null</code>
         * @param    value        the value of the cell to be edited; it is
         * up to the specific editor to interpret
         * and draw the value.  For example, if value is
         * the string "true", it could be rendered as a
         * string or it could be rendered as a check
         * box that is checked.  <code>null</code>
         * is a valid value
         * @param    isSelected    true if the cell is to be rendered with
         * highlighting
         * @param    row the row of the cell being edited
         * @param    column the column of the cell being edited
         * @return the component for editing
         */
        public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
            combo.setBackground(table.getBackground());
            combo.setForeground(table.getForeground());
            combo.setSelectedItem(getDrawComponent(value));
            return combo;
        }

        /**
         * Returns the value contained in the editor.
         *
         * @return the value contained in the editor
         */
        public Object getCellEditorValue() {
            if (combo.getSelectedItem() != null)
                return ((LabelRenderer)combo.getSelectedItem()).getKey();
            else
                return null;
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -