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

📄 jmimagecellrenderer.java

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

import java.awt.* ;
import javax.swing.* ;
import javax.swing.table.* ;
import java.io.Serializable;
import jm.util.*;
import jm.framework.gui.module.*;
import jm.framework.gui.module.jmtable.JMTable;

import javax.swing.tree.TreeCellRenderer;
public class JMImageCellRenderer
    implements TreeCellRenderer,TableCellRenderer , Serializable
{
    /**
	 * 
	 */
	private static final long serialVersionUID = -4790042313221822672L;
	public static final String CLASS_NAME = "oimageTableCellRenderer" ;
    private JMScrollPane bkg = new JMScrollPane();
    private JLabel lab = new JLabel();
    public static final int VERTICAL_SCROLLBAR_ALWAYS      = JMScrollPane.VERTICAL_SCROLLBAR_ALWAYS;
    public static final int VERTICAL_SCROLLBAR_AS_NEEDED   = JMScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED;
    public static final int VERTICAL_SCROLLBAR_NEVER       = JMScrollPane.VERTICAL_SCROLLBAR_NEVER;

    public static final int HORIZONTAL_SCROLLBAR_ALWAYS    = JMScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS;
    public static final int HORIZONTAL_SCROLLBAR_AS_NEEDED = JMScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED;
    public static final int HORIZONTAL_SCROLLBAR_NEVER     = JMScrollPane.HORIZONTAL_SCROLLBAR_NEVER;
    private Color[] backcolor = null ;
    public JMImageCellRenderer ( Color[] color )
    {
        super () ;
        try
        {
            backcolor = color ;
            lab = new JLabel (  ) ;
            bkg.setBorder(null);
            bkg.setOpaque ( true ) ;
            jbInit () ;
        }
        catch ( Exception ex )
        {
            ex.printStackTrace () ;
        }
    }

    public JMImageCellRenderer ( Color[] color ,ImageIcon image)
    {
        super () ;
        try
        {
            backcolor = color ;
            if(image!=null)
            {
                lab = new JLabel ( image ) ;
            }
            else
            {
                lab = new JLabel (  ) ;
            }
            bkg.setBorder(null);
            bkg.setOpaque ( true ) ;
            jbInit () ;
        }
        catch ( Exception ex )
        {
            ex.printStackTrace () ;
        }
    }

    public JMImageCellRenderer ( Color[] color , byte[] image )
    {
        super () ;
        try
        {
            backcolor = color ;

            if(image!=null)
            {
                lab = new JLabel(new ImageIcon ( image ));
            }
            else
            {
                lab = new JLabel (  ) ;
            }

            bkg.setBorder ( null ) ;
            bkg.setOpaque ( true ) ;
            jbInit () ;
        }
        catch ( Exception ex )
        {
            ex.printStackTrace () ;
        }
    }

    /**
     * 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 () ;
    }

    /**
     * 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 )
    {
        try
        {
            JMVal cellval = (JMVal)value;
            if(cellval ==null||cellval.isNull()||cellval.getDataType()!=JMVal.TYPE_BYTEARRAY)
            {
                cellval = null ;
                lab.setIcon ( null );
            }
            else
            {
                lab.setIcon ( new ImageIcon ( cellval.getByteArrayValue () ) ) ;
            }
            ((JMTable)table).getModel().setValueAt(cellval,row,column);
            if ( isSelected )
            {
                bkg.setForeground ( table.getSelectionForeground () ) ;
            }
            else
            {
                if ( backcolor != null && backcolor.length > 0 )
                {
                    bkg.setBackground ( backcolor[ row % backcolor.length ] ) ;
                }
            }
            return bkg ;
        }
        catch ( Exception ex )
        {
            return bkg ;
        }
    }

    public void setHorizontalScrollBarPolicy()
    {
        bkg.setHorizontalScrollBarPolicy ( JMScrollPane.HORIZONTAL_SCROLLBAR_NEVER ) ;
    }

    public void setVerticalScrollBarPolicy()
    {
        bkg.setVerticalScrollBarPolicy ( JMScrollPane.VERTICAL_SCROLLBAR_NEVER ) ;
    }

    private void jbInit ()
        throws Exception
    {
        bkg.getViewport ().add ( lab ) ;
    }

    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 + -