图片的辅助录入完成,其实不难的

图片的辅助录入完成,其实不难的

package publictool.cellEditor;

import java.awt.Component;
import java.awt.Frame;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.EventObject;

import javax.swing.AbstractCellEditor;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;

import rootwindow.ValueMap;

import com.borland.jb.io.InputStreamToByteArray;

public class ImageCellEditor extends AbstractCellEditor implements
  TableCellEditor, ActionListener {
 // 鼠标点击次数
 public int CLICKCOUNT = 2;

 JButton button;

 // button 命令的名称
 protected static final String EDIT = "CELLEDITOR";

 // 对话框中确定的命令要是"OK"
 protected static final String DIALOG_OK = "OK";

 // 最终的返回一个object
 Object curobject;

 // 所有的弹出式窗口都必须是Dialog的子类
 // 而且必须有getSelectInPut
 // CellEditorDialog dialog1;

 JDialog dialog1;

 ValueMap valuemap;

 Frame frame;

 JFileChooserx jfc;

 public ImageCellEditor(Frame frame, ValueMap valuemap) {
  // super(frame, valuemap);
  init();
 }

 public void init() {
  System.out.println("sss1");
  button = new JButton();
  button.setActionCommand(EDIT);
  button.addActionListener(this);
  button.setBorderPainted(false);
  /*
   * dialog1 = new CellEditorDialog(frame, "", true, valuemap, "select
   * bcode,bname from bcode", "bname", this, null);
   */

  jfc = new JFileChooserx();
  dialog1 = jfc.createDialog(frame);
  jfc.addActionListener(this);

 }

 public Object getCellEditorValue() {

  System.out.println("3");
  return curobject;

 }

 public Component getTableCellEditorComponent(JTable table, Object value,
   boolean isSelected, int row, int column) {
  System.out.println("4");
  // curobject =jfc .getSelectedFile();
/*
  File file = jfc.getSelectedFile();
  InputStream is = null;
  InputStreamToByteArray imputs = null;
  byte[] b = new byte[102400];
  if (file != null) {
   try {
    System.out.println("5");
    is = new FileInputStream(file);
    is.read(b);

    imputs = new InputStreamToByteArray(b);
    // imputs = (InputStreamToByteArray)is;

   } catch (Exception e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
   }
    curobject = imputs;
   //curobject = null;
  }*/
  curobject = value;
  System.out.println("value = "+value);
  System.out.println("getTableCellEditorComponent"+curobject);
  return button;

 }

 // 默认鼠标双击才可以编辑
 public boolean isCellEditable(EventObject anEvent) {
  if (anEvent instanceof MouseEvent) {
   return ((MouseEvent) anEvent).getClickCount() >= CLICKCOUNT;
  }
  return true;
 }

 /**
  * Handles events from the editor button and from the dialog's OK button.
  */
 public void actionPerformed(ActionEvent e) {
  System.out.println("command=" + e.getActionCommand());
  if (EDIT.equals(e.getActionCommand())) {
   // The user has clicked the cell, so
   // bring up the dialog.

   dialog1.setVisible(true);

   // Make the renderer reappear.
   fireEditingStopped();
   System.out.println("1");
  } else if ("ApproveSelection".equals(e.getActionCommand())) { // User
   // pressed
   // dialog's
   // "OK"
   // button.
   File file = jfc.getSelectedFile();
   if (file != null) {
    InputStream is = null;
    InputStreamToByteArray imputs = null;
    byte[] b = new byte[102400];
    try {
     System.out.println("6");
     is = new FileInputStream(file);
     // InputStreamToByteArray imputs = new
     // InputStreamToByteArray(null);
     is.read(b);
     imputs = new InputStreamToByteArray(b);
     curobject = imputs;
     // imputs = (InputStreamToByteArray)is;
     // b = new byte[1024];
     // is.read(b);
    } catch (Exception e1) {
     // TODO Auto-generated catch block
     e1.printStackTrace();
    }
   }
   dialog1.dispose();
   

  }
   else if ("CancelSelection".equals(e.getActionCommand())) {
  
   dialog1.dispose();
  
   }
 }

 class JFileChooserx extends JFileChooser implements ActionListener {
  JDialog dialog = null;

  public JFileChooserx() {
   super();
  }

  public JDialog createDialog(Component parent) throws HeadlessException {

   return dialog = super.createDialog(parent);
  }

  public void actionPerformed(ActionEvent e) {
   
   if ("CancelSelection".equals(e.getActionCommand())) {
    dialog.dispose();
   }
  }
 }
}

有几个地方要注意JFileChooser的createDialog(Component parent)是protected的,我要覆盖它
jdbtable里存图片是用InputStreamToByteArray 来存储的,这里我还有个问题如何把读出来的图片,存到数据库中。
我现在是读取图片--放在byte[]中--数据库,这样一来我不知道byte要设置多大的长度,在控制台老报“Premature end of JPEG file”
但是这不是一个异常。

你可能感兴趣的:(图片的辅助录入完成,其实不难的)