java - ComboBox as a CellRenderer of JTable -


i want draw several jtables, each of has combobox cell renderer. each cell in each table represents list of data items , want list displayed each time click on combobox arrow. problem combobox not react mouse clicks, i.e. drop-down list not appear.

how can activate drop boxes?

i added below picture of gui , code.

i new java.swing. highly appreciated.

my gui // code borrowed how add checkbox , combobox in table cell?

import java.awt.borderlayout; import java.awt.color; import java.awt.component; import java.awt.dimension; import java.awt.gridlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.event.mouseadapter; import java.awt.event.mouseevent; import java.util.eventobject;  import javax.swing.jcombobox; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.jscrollpane; import javax.swing.jtable; import javax.swing.border.lineborder; import javax.swing.event.celleditorlistener; import javax.swing.event.changeevent; import javax.swing.table.abstracttablemodel; import javax.swing.table.tablecelleditor; import javax.swing.table.tablecellrenderer;  public class mygui extends jpanel {     private int _ntables;     private int _nrows;      private int _ncols;     private string[] _tableinfo;     private string[][][][] _clusters;      private string[][][][] _clustersintersections;      public mygui(int ntables, int nrows, int ncols, string[] tableinfo,              string[][][][] clusters,              string[][][][] clustersintersections) {         super();         _ntables = ntables;         _nrows = nrows;         _ncols = ncols;         _clusters = clusters;         _clustersintersections = clustersintersections;          gridlayout layout = new gridlayout(0, 2);         layout.sethgap(30);         layout.setvgap(30);         setlayout(layout);           for(int t=0; t<_ntables; t++){             jpanel innerpanel = new jpanel(new borderlayout());             innerpanel.setborder(new lineborder(color.black));             jlabel label = new jlabel(tableinfo[t]);             label.setopaque(true);             label.setbackground(color.white);             innerpanel.add(label, borderlayout.page_start);              jtable table = new jtable(new mymodel(_nrows, _ncols,                                                _clusters[t],                                                _clustersintersections[t]));             table.setborder(new lineborder(color.black));             table.setminimumsize(new dimension(300,80));                 table.setmaximumsize(new dimension(300,80));                 table.setrowheight(36);                      table.setfillsviewportheight(true);             table.setcellselectionenabled(true);              for(int c=0; c<ncols; c++){                 table.getcolumnmodel().getcolumn(c).setcellrenderer(new comboboxcellrenderer());             }             innerpanel.add(table, borderlayout.page_end);              add(innerpanel);                     }            }      class mymodel extends abstracttablemodel{            int _nrows;         int _ncols;         string[][][] _clusters;          string[][][] _clustersintersections;              mymodel(int nrows, int ncols,                  string[][][] clusters,                  string[][][] clustersintersections){             _nrows = nrows;             _ncols = ncols;             _clusters = clusters;             _clustersintersections = clustersintersections;         }          public int getcolumncount() {             return _ncols;         }          public int getrowcount() {             return _nrows;         }          public object getvalueat(int row, int col) {             return _clusters[row][col];         }          public object getadditionalinfo(int row, int col) {             return _clustersintersections[row][col];         }          public class getcolumnclass(int c) {             return getvalueat(0, c).getclass();         }     }         class comboboxpanel extends jpanel {         protected jcombobox<string> combobox;          public comboboxpanel() {             super();             combobox = new jcombobox<string>() {             @override              public dimension getpreferredsize() {                 dimension d = super.getpreferredsize();                 return new dimension(80, d.height);             }         };         setopaque(true);         combobox.setenabled(true);         combobox.seteditable(false);         add(combobox);       }     }      class comboboxcellrenderer extends comboboxpanel                                                   implements tablecellrenderer {         public comboboxcellrenderer() {             super();             setname("table.cellrenderer");         }          @override          public component gettablecellrenderercomponent(jtable table, object value,                                                         boolean isselected,                                                        boolean hasfocus,                                                         int row, int column) {             setbackground(isselected?table.getselectionbackground()                                                         :table.getbackground());              if(value!=null) {                 string[] clusterslist = (string[]) value;                 combobox.removeallitems();                 for(string cluster:clusterslist){                     combobox.additem(cluster);                 }                                system.out.print("items count = "+combobox.getitemcount());                              system.out.print("    items: {");                 int count = combobox.getitemcount();                 for(int = 0; i<count; i++){                     system.out.print(combobox.getitemat(i)+" ");                 }                 system.out.print("}");                               system.out.println("");             }              if(value!=null) {                 combobox.setselectedindex(0);             }             return this;         }     }         public void draw(){      }         public static void main(string[] args) {         int ntables = 4;         int nrows = 2;         int ncols = 2;          string[] tableinfo = new string[ntables];         string[][][][] clusters = new string[ntables][nrows][ncols][];         string[][][][] clustersintersections = new string[ntables][nrows][ncols][];         for(int t=0; t<ntables; t++){             tableinfo[t] = new string("   info");             for(int row=0; row<nrows; row++){                 for(int col=0; col<ncols; col++){                     clusters[t][row][col] =                      new string[]{"   "+new integer(t).tostring(),                                    new integer(row).tostring(),                                    new integer(col).tostring()};                      clustersintersections[t][row][col] =                              new string[]{new integer(t).tostring(),                                            new integer(row).tostring(),                                            new integer(col).tostring()};                 }             }         }                //create , set window.         jframe frame = new jframe("mygui");         frame.setdefaultcloseoperation(jframe.exit_on_close);           //create , set content pane.                mygui gui = new mygui(ntables, nrows, ncols, tableinfo, clusters, clustersintersections);         gui.setopaque(true); //content panes must opaque          jscrollpane scrollpane = new jscrollpane(gui);         frame.setcontentpane(scrollpane);          //display window.         frame.pack();         frame.setvisible(true);     } } 

be default cells not editable.

you need override iscelleditable(...) method of custom model return true make cell editable.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -