📄 jmcheckboxcellrenderer.java
字号:
package jm.framework.gui.module.render ;
import java.io.* ;
import java.awt.* ;
import javax.swing.* ;
import javax.swing.table.* ;
import jm.util.*;
import jm.framework.gui.module.JMCheckBox;
import jm.framework.gui.module.jmtable.JMTable;
import javax.swing.tree.TreeCellRenderer;
public class JMCheckBoxCellRenderer
implements TreeCellRenderer,TableCellRenderer , Serializable
{
/**
*
*/
private static final long serialVersionUID = 2188575870255053169L;
public static final String CLASS_NAME = "CheckBoxTableCellRenderer" ;
private JPanel bkg = new JPanel () ;
private Color unselectedForeground = null ;
private JMCheckBox checkbox = null ;
private Color[] backcolor = null ;
private boolean bshowtext = false ;
private JMVList oval = null ;
/**
* CheckBoxTableCellRenderer
* @param val JMList val[0]:check,val[1]:uncheck
* @param selBackColor Color
* @param showtext boolean
*/
public JMCheckBoxCellRenderer ( JMVList val , Color[] color , boolean showtext )
{
super () ;
try
{
oval = val ;
backcolor = color ;
bshowtext = showtext ;
checkbox = new JMCheckBox () ;
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 ( checkbox != null )
checkbox.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 ( checkbox != null )
checkbox.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 ( checkbox != null )
checkbox.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 () ;
}
/**
* 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 )
{
JMVal cellval = (JMVal)( ( JMTable ) table ).getCellData ( row , column ) ;
if ( oval != null && oval.size () == 2 )
{
if ( cellval.equals ( oval.get ( 1 ) ) )
{
checkbox.setSelected ( true ) ;
if ( bshowtext ) checkbox.setText ( (oval.get ( 1 )).getStringValue () ) ;
}
else
{
checkbox.setSelected ( false ) ;
if ( bshowtext ) checkbox.setText ( (oval.get ( 0 )).getStringValue () ) ;
}
}
else
{
checkbox.setSelected ( false ) ;
if ( bshowtext ) checkbox.setText ( cellval.getStringValue().equals("")
? "" : cellval.getStringValue() ) ;
}
if ( isSelected )
{
checkbox.setForeground ( table.getSelectionForeground () ) ;
checkbox.setBackground ( table.getSelectionBackground () ) ;
bkg.setBackground ( table.getSelectionBackground () ) ;
}
else
{
checkbox.setForeground ( ( unselectedForeground != null ) ? unselectedForeground
: table.getForeground () ) ;
if ( backcolor != null && backcolor.length > 0 )
{
checkbox.setBackground ( backcolor[ row % backcolor.length ] ) ;
bkg.setBackground ( backcolor[ row % backcolor.length ] ) ;
}
}
checkbox.setFont ( table.getFont () ) ;
// if ( hasFocus )
// {
// Border noFocusBorder = new EmptyBorder ( 1 , 1 , 1 , 1 ) ;
//
// checkbox.setBorder ( UIManager.getBorder ( "Table.focusCellHighlightBorder" ) ) ;
// if ( !isSelected && table.isCellEditable ( row , column ) )
// {
// Color col ;
// col = UIManager.getColor ( "Table.focusCellForeground" ) ;
// if ( col != null )
// {
// checkbox.setForeground ( col ) ;
// }
// col = UIManager.getColor ( "Table.focusCellBackground" ) ;
// if ( col != null )
// {
// checkbox.setBackground ( col ) ;
// }
// }
// }
// else
// {
// checkbox.setBorder ( null ) ;
// }
return bkg ;
}
private void jbInit ()
throws Exception
{
checkbox.setBorder ( null ) ;
bkg.setBorder ( null ) ;
bkg.setOpaque ( true ) ;
// checkbox.setBackground(Color.white);
// checkbox.setRenderer ( new RelListCellRenderer (Color.BLUE)) ;
bkg.add ( checkbox ) ;
}
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 + -