📄 jmimagecelleditor.java
字号:
package jm.framework.gui.module.editer ;
import java.awt.Color;
import java.awt.Component;
import java.util.EventObject;
import javax.swing.AbstractCellEditor;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.filechooser.FileSystemView;
import javax.swing.table.TableCellEditor;
import javax.swing.tree.TreeCellEditor;
import jm.framework.gui.module.JMFileChooser;
import jm.framework.gui.module.JMScrollPane;
import jm.framework.gui.module.SimpalFileFilter;
import jm.framework.gui.module.jmtable.JMTable;
import jm.util.JMVal;
/**
This editor pops up a color dialog to edit a cell value
*/
public class JMImageCellEditor
extends AbstractCellEditor implements TableCellEditor , TreeCellEditor
{
/**
*
*/
private static final long serialVersionUID = 3267490032928728750L;
public static final String CLASS_NAME = "ImageCellEditor" ;
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 JMScrollPane bkg = null;
private JMFileChooser fileChooser = null ;
private byte [] picBytes = null;
private JLabel lViewPic =null;
//private Color selbackColor = null;
public JMImageCellEditor ( Color selBackColor )
{
super () ;
try
{
//selbackColor = selBackColor;
lViewPic = new JLabel();
bkg = new JMScrollPane () ;
fileChooser = new JMFileChooser ( ) ;
SimpalFileFilter filter = new SimpalFileFilter();
FileSystemView poview = fileChooser.getFileSystemView();
filter.addExtension("jpg");
filter.addExtension("gif");
filter.setDescription("JPG and GIF Images");
fileChooser.setFileFilter(filter);
fileChooser.setFileSystemView(poview);
/////////////////////////////////////////////////////////
fileChooser.setAcceptAllFileFilterUsed(false);
/////////////////////////////////////////////////////////
fileChooser.setOpaque ( true ) ;
if(selBackColor!=null)
// fileChooser.setBackground ( selBackColor ) ;
jbInit () ;
}
catch ( Exception ex )
{
ex.printStackTrace () ;
}
}
public JMFileChooser getCellfileChooser()
{
return fileChooser;
}
public boolean shouldSelectCell(EventObject anEvent)
{
// start editing
bkg.setVisible(true);
// tell caller it is ok to select this cell
return true;
}
public void cancelCellEditing()
{
// editing is canceled--hide dialog
bkg.setVisible(false);
super.cancelCellEditing();
}
public boolean stopCellEditing()
{
// editing is complete--hide dialog
bkg.setVisible(false);
super.stopCellEditing();
// tell caller is is ok to use color value
return true;
}
public Component getTableCellEditorComponent (
JTable table ,
Object value ,
boolean isSelected ,
int row ,
int column )
{
try
{
JMVal cellval = (JMVal)value;
if(cellval ==null||cellval.isNull()||cellval.getDataType()!=JMVal.TYPE_BYTEARRAY)
{
cellval = null ;
}
int returnVal = fileChooser.showOpenDialog(table);
if (returnVal == JFileChooser.APPROVE_OPTION) {
//Get_filename
String filePathName = fileChooser.getSelectedFile().getPath();
picBytes = fileChooser.getFileBytes(filePathName);
cellval = new JMVal(picBytes) ;
lViewPic.setIcon ( new ImageIcon ( picBytes ) ) ;
stopCellEditing();
}
else
{
if ( cellval == null || cellval.isNull () || cellval.getDataType () != JMVal.TYPE_BYTEARRAY )
{
picBytes = null ;
lViewPic.setIcon ( null ) ;
}
else
{
picBytes = cellval.getByteArrayValue() ;
lViewPic.setIcon ( new ImageIcon ( picBytes ) ) ;
}
cancelCellEditing();
}
((JMTable)table).getModel().setValueAt(cellval,row,column);
return bkg ;
}
catch ( Exception e )
{
e.printStackTrace();
return bkg ;
}
}
/**
* 僄僨傿僞偺乽弶婜抣乿傪愝掕偟傑偡丅偙傟偵傛傝僄僨傿僞偱 stopEditing 偑屇傃弌偝傟丄偦偺偲偒偵僄僨傿僞偑曇廤拞偺応崌偼丄曇廤偝傟偨抣偑堦晹徚幐偡傞偙偲偑偁傝傑偡丅
* 僋儔僀傾儞僩偺 Component 奒憌偵捛壛偡傞昁梫偺偁傞僐儞億乕僱儞僩傪曉偟傑偡丅偙偺僐儞億乕僱儞僩偑僋儔僀傾儞僩偺奒憌偵慻傒崬傑傟傞偲丄儐乕僓擖椡傪昤夋偍傛傃庴怣偱偒傑偡丅
* @param tree JTree 僄僨傿僞偵曇廤傪梫媮偟偰偄傞 JTree丅偙偺僷儔儊乕僞偼 null 傕壜
* @param value Object 曇廤偝傟傞僙儖抣
* @param isSelected boolean 僙儖偑儗儞僟儕儞僌偲側傝慖戰晹偑僴僀儔僀僩偝傟偰偄傞応崌偼 true
* @param expanded boolean 僲乕僪偑揥奐偝傟偰偄傞応崌偼 true
* @param leaf boolean 僲乕僪偑梩僲乕僪偺応崌偼 true
* @param row int 曇廤拞偺僲乕僪偺峴僀儞僨僢僋僗
* @return Component 曇廤偺偨傔偺僐儞億乕僱儞僩
*/
public Component getTreeCellEditorComponent ( JTree tree ,
Object value ,
boolean isSelected ,
boolean expanded ,
boolean leaf ,
int row )
{
try
{
JMVal cellval = (JMVal)value;
if(cellval ==null||cellval.isNull()||cellval.getDataType()!=JMVal.TYPE_BYTEARRAY)
{
cellval = null ;
}
int returnVal = fileChooser.showOpenDialog(tree);
if (returnVal == JFileChooser.APPROVE_OPTION) {
//Get_filename
String filePathName = fileChooser.getSelectedFile().getPath();
picBytes = fileChooser.getFileBytes(filePathName);
cellval = new JMVal(picBytes) ;
lViewPic.setIcon ( new ImageIcon ( picBytes ) ) ;
stopCellEditing();
}
else
{
if ( cellval == null || cellval.isNull () || cellval.getDataType () != JMVal.TYPE_BYTEARRAY )
{
picBytes = null ;
lViewPic.setIcon ( null ) ;
}
else
{
picBytes = cellval.getByteArrayValue() ;
lViewPic.setIcon ( new ImageIcon ( picBytes ) ) ;
}
cancelCellEditing();
}
// ((JMTable)table).getModel().setValueAt(cellval,row,column);
return bkg ;
}
catch ( Exception e )
{
e.printStackTrace();
return bkg ;
}
}
/**
* Returns the value contained in the editor.
*
* @return the value contained in the editor
* @todo Implement this javax.swing.CellEditor method
*/
public Object getCellEditorValue ()
{
return new JMVal(picBytes);
}
public void setHorizontalScrollBarPolicy()
{
bkg.setHorizontalScrollBarPolicy ( JMScrollPane.HORIZONTAL_SCROLLBAR_NEVER ) ;
}
public void setVerticalScrollBarPolicy()
{
bkg.setVerticalScrollBarPolicy ( JMScrollPane.VERTICAL_SCROLLBAR_NEVER ) ;
}
private void jbInit ()
throws Exception
{
bkg.setBackground ( Color.white ) ;
bkg.getViewport ().add ( lViewPic ) ;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -