java - JList render invisible "selection marker" -
the jlist supports multiple selection when holding control key: press ctrl+up/down move invisible marker (nimbus laf). if no press space, element gets selected.
example: jlist has 3 elements, first 1 selected. know press ctrl + down, ctrl + down , space. last element selected.
the question is: how can render invisible marker move ctrl+up/down?
for example windows file explorer renders marker dotted border , render similar. thing ctrl + up/down don't change selection change element selected/deselected if press space.
defaultlistcellrenderer
automatically using special border. if want change border, can change appropriate setting of l&f in uimanager
.
import java.awt.basicstroke; import java.awt.color; import java.awt.component; import javax.swing.jframe; import javax.swing.jlist; import javax.swing.jscrollpane; import javax.swing.swingutilities; import javax.swing.uimanager; import javax.swing.border.strokeborder; public class listtryout { public static void main(string[] args) { swingutilities.invokelater(new runnable() { @override public void run() { uimanager.put("list.focuscellhighlightborder", borderfactory.createdashedborder(color.gray)); final jframe f = new jframe(); f.setdefaultcloseoperation(jframe.dispose_on_close); f.add(new jscrollpane(new jlist<string>(new string[] {"one", "two", "three"}))); f.pack(); f.setvisible(true); } }); } }
if want else can write own renderer.
import java.awt.color; import java.awt.component; import javax.swing.defaultlistcellrenderer; import javax.swing.jcomponent; import javax.swing.jframe; import javax.swing.jlist; import javax.swing.jscrollpane; import javax.swing.swingutilities; public class listtryout { public static void main(string[] args) { swingutilities.invokelater(new runnable() { @override public void run() { // uimanager.put("list.focuscellhighlightborder", new strokeborder(new basicstroke(2f))); final jframe f = new jframe(); f.setdefaultcloseoperation(jframe.dispose_on_close); jlist<string> l = new jlist<string>(new string[] {"one", "two", "three"}); l.setcellrenderer(new myrenderer()); f.add(new jscrollpane(l)); f.pack(); f.setvisible(true); } }); } private static class myrenderer extends defaultlistcellrenderer { /** * {@inheritdoc} */ @override public component getlistcellrenderercomponent(jlist<?> list, object value, int index, boolean isselected, boolean cellhasfocus) { component result = super.getlistcellrenderercomponent(list, value, index, isselected, cellhasfocus); if (!isselected && cellhasfocus) { // custom highlight of focused not-selected cell result.setbackground(color.light_gray); ((jcomponent) result).setborder(null); } return result; } } }
Comments
Post a Comment