java - Custom CellEditor with JScrollPane - start editing issue -


i have jtable custom celleditor using jtextarea inside jscrollpane. works when enter edit mode via mouse clic. however, when try type letter while cell focused, nothing happens. cell gets "edit mode style" (the background changes), stays empty...

any idea ?

public class multilinecelleditor extends defaultcelleditor {      jtextarea textarea;     jscrollpane scrollpane;      public multilinecelleditor( final jtable table ) {         super( new jtextfield() );          getcomponent().setname( "table.editor" );         setclickcounttostart( 2 );          textarea = new jtextarea();          scrollpane = new jscrollpane();         scrollpane.setviewportview( textarea );         editorcomponent = scrollpane;      }//end multilinecelleditor       public component gettablecelleditorcomponent( jtable table, object value, boolean isselected,                                                   int row, int column ) {         this.setvalue( value );         scrollpane.setborder( new lineborder( color.black ) );         return scrollpane;     }       public void setvalue( object value ) {         textarea.settext( ( value != null ) ? value.tostring() : "" );     }       public object getcelleditorvalue() {         return textarea.gettext();     }  }//end class 

here testing code:

import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.table.*;  public class multilinecelleditortest {   public jcomponent makeui() {     string[] columnnames = {"jtextfield", "jtextarea"};     object[][] data = {       {"aaa", "jtextarea+jscrollpane\nctrl-enter: stopcellediting"},       {"bbb", "ggg"}, {"ccccddd", "hhh\njjj\nkkk"}     };     tablemodel model = new defaulttablemodel(data, columnnames) {       @override public class<?> getcolumnclass(int column) {         return string.class;       }     };     jtable table = new jtable(model) {       @override public void updateui() {         super.updateui();         tablecolumn col = getcolumnmodel().getcolumn(1);         col.setcelleditor(new textareacelleditor());         col.setcellrenderer(new textareacellrenderer());       }     };     table.setautocreaterowsorter(true);     table.setsurrendersfocusonkeystroke(true);     table.setrowheight(64);     return new jscrollpane(table);   }   public static void main(string... args) {     eventqueue.invokelater(new runnable() {       @override public void run() {         createandshowgui();       }     });   }   public static void createandshowgui() {     jframe f = new jframe();     f.setdefaultcloseoperation(windowconstants.exit_on_close);     f.getcontentpane().add(new multilinecelleditortest().makeui());     f.setsize(320, 240);     f.setlocationrelativeto(null);     f.setvisible(true);   } } class textareacellrenderer implements tablecellrenderer {   private final jtextarea textarea = new jtextarea();   public textareacellrenderer() {     textarea.setlinewrap(true);     textarea.setborder(borderfactory.createemptyborder(1, 5, 1, 5));   }   @override public component gettablecellrenderercomponent(       jtable table, object value, boolean isselected,       boolean hasfocus, int row, int column) {     if (isselected) {       textarea.setforeground(table.getselectionforeground());       textarea.setbackground(table.getselectionbackground());     } else {       textarea.setforeground(table.getforeground());       textarea.setbackground(table.getbackground());     }     textarea.setfont(table.getfont());     textarea.settext(objects.tostring(value, ""));     return textarea;   } } //class textareacelleditor extends abstractcelleditor implements tablecelleditor { class textareacelleditor implements tablecelleditor {   private static final string key = "stop-cell-editing";   private final jscrollpane scroll;   private final jtextarea textarea = new jtextarea();   public textareacelleditor() {     //super();     scroll = new jscrollpane(textarea);     scroll.setborder(borderfactory.createemptyborder());     textarea.setlinewrap(true);     textarea.setborder(borderfactory.createemptyborder(1, 5, 1, 5));     keystroke enter = keystroke.getkeystroke(         keyevent.vk_enter, inputevent.ctrl_mask);     textarea.getinputmap(jcomponent.when_focused).put(enter, key);     textarea.getactionmap().put(key, new abstractaction() {       @override public void actionperformed(actionevent e) {         stopcellediting();       }     });   }   @override public object getcelleditorvalue() {     return textarea.gettext();   }   @override public component gettablecelleditorcomponent(       jtable table, object value, boolean isselected, int row, int column) {     system.out.println("2. gettablecelleditorcomponent");     textarea.setfont(table.getfont());     textarea.settext(objects.tostring(value, ""));     eventqueue.invokelater(new runnable() {       @override public void run() {         textarea.setcaretposition(textarea.gettext().length());         textarea.requestfocusinwindow();         system.out.println("4. invokelater: gettablecelleditorcomponent");       }     });     return scroll;   }   @override public boolean iscelleditable(final eventobject e) {     if (e instanceof mouseevent) {       return ((mouseevent) e).getclickcount() >= 2;     }     system.out.println("1. iscelleditable");     eventqueue.invokelater(new runnable() {       @override public void run() {         if (e instanceof keyevent) {           keyevent ke = (keyevent) e;           char kc = ke.getkeychar();           if (character.isunicodeidentifierstart(kc)) {             textarea.settext(textarea.gettext() + kc);             system.out.println("3. invokelater: iscelleditable");           }         }       }     });     return true;   }    //copid abstractcelleditor   protected eventlistenerlist listenerlist = new eventlistenerlist();   transient protected changeevent changeevent = null;   @override public boolean shouldselectcell(eventobject e) {     return true;   }   @override public boolean stopcellediting() {     fireeditingstopped();     return true;   }   @override public void cancelcellediting() {     fireeditingcanceled();   }   @override public void addcelleditorlistener(celleditorlistener l) {     listenerlist.add(celleditorlistener.class, l);   }   @override public void removecelleditorlistener(celleditorlistener l) {     listenerlist.remove(celleditorlistener.class, l);   }   public celleditorlistener[] getcelleditorlisteners() {     return listenerlist.getlisteners(celleditorlistener.class);   }   protected void fireeditingstopped() {     // guaranteed return non-null array     object[] listeners = listenerlist.getlistenerlist();     // process listeners last first, notifying     // interested in event     for(int = listeners.length-2; i>=0; i-=2) {       if(listeners[i]==celleditorlistener.class) {         // lazily create event:         if(changeevent == null) changeevent = new changeevent(this);         ((celleditorlistener)listeners[i+1]).editingstopped(changeevent);       }     }   }   protected void fireeditingcanceled() {     // guaranteed return non-null array     object[] listeners = listenerlist.getlistenerlist();     // process listeners last first, notifying     // interested in event     for(int = listeners.length-2; i>=0; i-=2) {       if(listeners[i]==celleditorlistener.class) {         // lazily create event:         if(changeevent == null) changeevent = new changeevent(this);         ((celleditorlistener)listeners[i+1]).editingcanceled(changeevent);       }     }   } } 

edit: replace inheritance delegation


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -