java - rows added selected by default after selecting two rows -


taking example mkorbel example:

i have achieved creating desired behavior using mechanisms suggested mkorbel. works long have 1 row painted green (unselected). if add 1 row, select , add new row, works fine, new unselected row. if add second row , selected painted red correctly, upon adding more rows after second row (two red rows) selected default, , not want. want rows in green until click on them (double click). know why happening? why work long have 1 cell unselected? why if, have more 2 or the, rows selected, keeps adding new rows in selected mode? thnx

my code mouse event follows:

m_list = new jlist<string>(m_listmodel)  {     private mycellrenderer cellrenderer = new mycellrenderer();      // emulate control down multiple non contiguous selection on     // list.     @override     // todo fix here     public void processmouseevent(mouseevent event) {     int modifiers = event.getmodifiers() | inputevent.ctrl_mask;      m_myme = new mouseevent((component) event.getsource(),         event.getid(), event.getwhen(), modifiers,         event.getx(), event.gety(), event.getxonscreen(),         event.getyonscreen(), event.getclickcount(),         event.ispopuptrigger(), event.getbutton());      //if clicked twice     if (event.getclickcount() == 2) {         //if flag set true consume event         if ((mycellrenderer.getflag() == true)) {         m_urlname = mycellrenderer.getvalue();         m_myme.consume();         //initiate parsing         initiateparsing();         }else{         m_urlname = mycellrenderer.getvalue();         }         //if not consume emulate ctrl_mask         if (!m_myme.isconsumed()) {         super.processmouseevent(m_myme);         m_urlname = mycellrenderer.getvalue();         //initiate parsing process         initiateparsing();         }      }     } }; 

the code in cellrenderer follows:

 public static class mycellrenderer extends jlabel implements         listcellrenderer {      private static final long serialversionuid = 1l;     private static boolean myflag = false;     private static string thisvalue;      public mycellrenderer() {         setopaque(true);      }      public component getlistcellrenderercomponent(jlist list, object value,         int index, boolean isselected, boolean cellhasfocus) {         settext(value.tostring());          color background = null;         color foreground = null;          if (isselected == true) {          background = color.red;         foreground = color.white;         myflag = true;         } else {          background = color.green;         foreground = color.black;         myflag = false;         }         setbackground(background);         setforeground(foreground);      public static class mycellrenderer extends jlabel implements         listcellrenderer {      private static final long serialversionuid = 1l;     private static boolean myflag = false;     private static string thisvalue;      public mycellrenderer() {         setopaque(true);      }      public component getlistcellrenderercomponent(jlist list, object value,         int index, boolean isselected, boolean cellhasfocus) {         settext(value.tostring());          color background = null;         color foreground = null;          if (isselected == true) {          background = color.red;         foreground = color.white;         myflag = true;         } else {          background = color.green;         foreground = color.black;         myflag = false;         }         setbackground(background);         setforeground(foreground);          // string pointing @         thisvalue = value.tostring();         m_index = index;         return this;     }      public static boolean getflag() {         return myflag;     }      public static string getvalue() {         return thisvalue;     }  } 

  • as commneted in previous question, can use mouseevent.consume() not in xxxrenderer

  • (i don't want comment something) selection must stored in xxxmodel, otherwise attemps wrong or in better case caused side effects

  • for example, using model_to_view, .... defaultlistselectionmodel

enter image description here

import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*;  public class jlistdisableditemdemo implements itemlistener, runnable {      private jframe f = new jframe("colors");     private static final string items[] = {" black ", " blue ", " green ",         " orange ", " purple ", " red ", " white ", " yellow "};     private jlist jlist;     private jcheckbox[] checkboxes;     private boolean[] enabledflags;      @override     public void run() {         jpanel pnlenablers = new jpanel(new gridlayout(0, 1));         pnlenablers.setborder(borderfactory.createtitledborder("enabled items"));         checkboxes = new jcheckbox[items.length];         enabledflags = new boolean[items.length];         (int = 0; < items.length; i++) {             checkboxes[i] = new jcheckbox(items[i]);             checkboxes[i].setselected(true);             checkboxes[i].additemlistener(this);             enabledflags[i] = true;             pnlenablers.add(checkboxes[i]);         }         jlist = new jlist(items);         jlist.setselectionmode(listselectionmodel.single_selection);         jlist.setselectionmodel(new disableditemselectionmodel());         jlist.setcellrenderer(new disableditemlistcellrenderer());         jlist.addlistselectionlistener(new listselectionlistener() {             @override             public void valuechanged(listselectionevent e) {                 if (!e.getvalueisadjusting()) {                     system.out.println("selection");                 }             }         });         jscrollpane scroll = new jscrollpane(jlist);         scroll.sethorizontalscrollbarpolicy(scrollpaneconstants.horizontal_scrollbar_never);         scroll.setverticalscrollbarpolicy(scrollpaneconstants.vertical_scrollbar_always);          container contentpane = f.getcontentpane();         contentpane.setlayout(new gridlayout(1, 2));         contentpane.add(pnlenablers);         contentpane.add(scroll);         f.setdefaultcloseoperation(jframe.exit_on_close);         f.setlocation(240, 280);         uimanager.put("list.background", color.lightgray);         uimanager.put("list.selectionbackground", color.orange);         uimanager.put("list.selectionforeground", color.blue);         uimanager.put("label.disabledforeground", color.magenta);         swingutilities.updatecomponenttreeui(f);         f.pack();         javax.swing.swingutilities.invokelater(new runnable() {             @override             public void run() {                 f.setvisible(true);             }         });     }      @override     public void itemstatechanged(itemevent event) {         jcheckbox checkbox = (jcheckbox) event.getsource();         int index = -1;         (int = 0; < items.length; i++) {             if (items[i].equals(checkbox.gettext())) {                 index = i;                 break;             }         }         if (index != -1) {             enabledflags[index] = checkbox.isselected();             jlist.repaint();         }     }      public static void main(string args[]) {         swingutilities.invokelater(new jlistdisableditemdemo());     }      private class disableditemlistcellrenderer extends defaultlistcellrenderer {          private static final long serialversionuid = 1l;          @override         public component getlistcellrenderercomponent(jlist list, object value, int index, boolean isselected, boolean cellhasfocus) {             component comp = super.getlistcellrenderercomponent(list, value, index, false, false);             jcomponent jc = (jcomponent) comp;             if (enabledflags[index]) {                 if (isselected & cellhasfocus) {                     comp.setforeground(color.black);                     comp.setbackground(color.red);                 } else {                     comp.setforeground(color.blue);                 }                 if (!isselected) {                     if ((value.tostring()).trim().equals("yellow")) {                         comp.setforeground(color.orange);                         comp.setbackground(color.magenta);                     }                 }                 return comp;             }             comp.setenabled(false);             return comp;         }     }      private class disableditemselectionmodel extends defaultlistselectionmodel {          private static final long serialversionuid = 1l;          @override         public void setselectioninterval(int index0, int index1) {             if (enabledflags[index0]) {                 super.setselectioninterval(index0, index0);             } else {                 /*                  * selected index before one,                  * walk forward find next selectable item.                  */                 if (getanchorselectionindex() < index0) {                     (int = index0; < enabledflags.length; i++) {                         if (enabledflags[i]) {                             super.setselectioninterval(i, i);                             return;                         }                     }                 } /*                  * otherwise, walk backward find next selectable item.                  */ else {                     (int = index0; >= 0; i--) {                         if (enabledflags[i]) {                             super.setselectioninterval(i, i);                             return;                         }                     }                 }             }         }     } } 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -