📄 jmbuttoncellrenderer.java
字号:
package jm.framework.gui.module.render ;
import java.io.* ;
import java.awt.* ;
import javax.swing.* ;
import javax.swing.table.* ;
import jm.framework.gui.module.*;
import jm.util.* ;
public class JMButtonCellRenderer
implements TableCellRenderer , Serializable
{
/**
*
*/
private static final long serialVersionUID = 8613768430683087938L;
public static final String CLASS_NAME = "buttonTableCellRenderer" ;
private JMScrollPane bkg = new JMScrollPane();
private Color unselectedForeground = null ;
private JMButton button = null;
private Color[] backcolor = null ;
public JMButtonCellRenderer ( Color[] color )
{
super () ;
try
{
backcolor = color ;
button = new JMButton () ;
bkg.setBorder(null);
bkg.setOpaque ( true ) ;
// button.setBackground(Color.white);
// button.setRenderer ( new RelListCellRenderer (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 ( button != null )
button.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 ( button != null )
button.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 ( button != null )
button.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)value;
if ( isSelected )
{
button.setForeground ( table.getSelectionForeground () ) ;
button.setBackground ( table.getSelectionBackground () ) ;
}
else
{
button.setForeground ( ( unselectedForeground != null ) ? unselectedForeground
: table.getForeground () ) ;
if ( backcolor != null && backcolor.length > 0 )
{
button.setBackground ( backcolor[ row % backcolor.length ] ) ;
}
}
button.setFont ( table.getFont () ) ;
button.setText ( ( 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 ( button ) ;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -