posts - 39,  comments - 263,  trackbacks - 0

          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”
          但是這不是一個異常。

          posted on 2005-10-23 23:34 nake 閱讀(970) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2005年10月>
          2526272829301
          2345678
          9101112131415
          16171819202122
          23242526272829
          303112345

          常用鏈接

          留言簿(18)

          我參與的團隊

          隨筆檔案(39)

          收藏夾(1)

          搜索

          •  

          積分與排名

          • 積分 - 451388
          • 排名 - 119

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 中方县| 内丘县| 马尔康县| 基隆市| 普安县| 余姚市| 吴旗县| 祁连县| 正安县| 英山县| 仙桃市| 张家口市| 岱山县| 土默特左旗| 晋中市| 佛学| 青海省| 罗田县| 阿荣旗| 丰顺县| 鄂托克前旗| 南江县| 通榆县| 江门市| 武强县| 哈尔滨市| 澎湖县| 偃师市| 鄂温| 伊吾县| 五大连池市| 临夏市| 务川| 绥中县| 新乡县| 公主岭市| 广昌县| 福鼎市| 四会市| 天台县| 冀州市|