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

📄 jmcomboboxcellrenderer.java

📁 梦界家园程序开发基底框架
💻 JAVA
字号:
package jm.framework.gui.module.render ;

import java.io.* ;
import java.util.* ;

import java.awt.* ;
import javax.swing.* ;
import javax.swing.table.* ;

import jm.framework.gui.module.*;
import jm.util.* ;
import javax.swing.tree.TreeCellRenderer;
public class JMComboBoxCellRenderer
    implements TreeCellRenderer,TableCellRenderer , Serializable
{

    /**
	 * 
	 */
	private static final long serialVersionUID = 4590234187584385890L;
	public static final String CLASS_NAME = "ComboBoxTableCellRenderer" ;
//    protected static Border noFocusBorder = new EmptyBorder ( 0 , 0 , 0 , 0 ) ;
    private JMScrollPane bkg = new JMScrollPane();
    private Color unselectedForeground = null ;
    private Vector oval = null ;
    private DefaultComboBoxModel oModel = null ;
    private JMComboBox comboBox = null ;
    private Color[] backcolor = null ;
    public JMComboBoxCellRenderer ( Vector<String> val , Color[] color )
    {
        super () ;
        try
        {
            backcolor = color ;
            oval = ( val != null ) ? val : new Vector () ;
            oModel = new DefaultComboBoxModel ( oval ) ;
            comboBox = new JMComboBox ( oModel ) ;
            comboBox.setBorder(null);
            bkg.setBorder(null);
            bkg.setOpaque ( true ) ;
            comboBox.setBackground(Color.white);
            comboBox.setRenderer ( new JMListCellRenderer (Color.BLUE)) ;
            jbInit () ;
        }
        catch ( Exception ex )
        {
            ex.printStackTrace () ;
        }
    }

    /**
     * Overrides <code>JComponent.setForeground</code> to assign
     * the unselected-foreground color to the specified color.
     *
     * @param c set the foreground color to this value
     */
    public void setForeground ( Color c )
    {
        if ( comboBox != null )
            comboBox.setForeground ( c ) ;
        unselectedForeground = c ;
    }

    /**
     * Overrides <code>JComponent.setBackground</code> to assign
     * the unselected-background color to the specified color.
     *
     * @param c set the background color to this value
     */
    public void setBackground ( Color c )
    {
        if ( comboBox != null )
            comboBox.setBackground ( c ) ;
        bkg.setBackground ( c ) ;
    }

    /**
     * Notification from the <code>UIManager</code> that the look and feel
     * [L&F] has changed.
     * Replaces the current UI object with the latest version from the
     * <code>UIManager</code>.
     *
     * @see JComponent#updateUI
     */
    public void updateUI ()
    {
        if ( comboBox != null )
            comboBox.updateUI () ;
        bkg.updateUI () ;
        setForeground ( null ) ;
        setBackground ( null ) ;
    }

    /**
     * Overridden for performance reasons.
     * See the <a href="#override">Implementation Note</a>
     * for more information.
     */
    public boolean isOpaque ()
    {
        Color back = bkg.getBackground () ;
        Component p = bkg.getParent () ;
        if ( p != null )
        {
            p = p.getParent () ;
        }
        // p should now be the JTable.
        boolean colorMatch = ( back != null ) && ( p != null ) &&
            back.equals ( p.getBackground () ) &&
            p.isOpaque () ;
        return!colorMatch && bkg.isOpaque () ;
    }

    /**
     * Overridden for performance reasons.
     * See the <a href="#override">Implementation Note</a>
     * for more information.
     */
//    protected void firePropertyChange ( String propertyName , Object oldValue , Object newValue )
//    {
//        // Strings get interned...
//        if ( propertyName == "text" )
//        {
//            bkg.firePropertyChange ( propertyName , oldValue , newValue ) ;
//        }
//    }

    /**
     * Returns the component used for drawing the cell.
     *
     * @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
     * @return Component
     * @todo Implement this javax.swing.table.TableCellRenderer method
     */
    public Component getTableCellRendererComponent (
        JTable table ,
        Object value ,
        boolean isSelected ,
        boolean hasFocus ,
        int row ,
        int column )
    {
        if ( isSelected )
        {
            comboBox.setForeground ( table.getSelectionForeground () ) ;
            comboBox.setBackground ( table.getSelectionBackground () ) ;
        }
        else
        {
            comboBox.setForeground ( ( unselectedForeground != null ) ? unselectedForeground
                                     : table.getForeground () ) ;
            if ( backcolor != null && backcolor.length > 0 )
            {
                comboBox.setBackground ( backcolor[ row % backcolor.length ] ) ;
            }

        }

        comboBox.setFont ( table.getFont () ) ;

        JMVal cellval = (JMVal)value;
        comboBox.setSelectedItem ( ( cellval == null ) ? "" :
                                   cellval.getStringValue().toString () ) ;
        return bkg ;
    }

    private void jbInit ()
        throws Exception
    {
        bkg.setHorizontalScrollBarPolicy ( JMScrollPane.HORIZONTAL_SCROLLBAR_NEVER ) ;
        bkg.setVerticalScrollBarPolicy ( JMScrollPane.VERTICAL_SCROLLBAR_NEVER ) ;

        bkg.getViewport ().add ( comboBox ) ;
    }

    public Component getTreeCellRendererComponent ( JTree tree , Object value , boolean selected , boolean expanded ,
        boolean leaf , int row , boolean hasFocus )
    {
        return null ;
    }

}

⌨️ 快捷键说明

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