java - JTable focus on entire row instead of cell -
is there way force jtable focus on entire rows instead of individual cells? i'm not talking row selection highlight, focus border i'd include cells on focused row.
update:
table non-editable cells , delete-row-functionality:
public class tabletest { private static void createandshowgui() { int rows = 20; int cols = 2; string[] headers = { "column 1", "column 2" }; string[][] data = new string[rows][cols]; (int j = 0; j < rows; j++) (int k = 0; k < cols; k++) data[j][k] = "item " + (j * cols + k + 1); jtable table = new jtable(); defaulttablemodel tablemodel = new defaulttablemodel(data, headers) { // disable editing of cells @override public boolean iscelleditable(int row, int column) { return false; } }; table.setmodel(tablemodel); table.setshowgrid(false); table.setintercellspacing(new dimension(0,0)); // key binding remove rows inputmap inputmap = table.getinputmap(jcomponent.when_ancestor_of_focused_component); actionmap actionmap = table.getactionmap(); inputmap.put(keystroke.getkeystroke(keyevent.vk_delete, 0), "remove"); actionmap.put("remove", new abstractaction() { @override public void actionperformed(actionevent e) { int[] rows = table.getselectedrows(); (int = rows.length - 1; >= 0; i--) { tablemodel.removerow(rows[i]); } } }); jframe f = new jframe(); f.setdefaultcloseoperation(jframe.exit_on_close); f.getcontentpane().add(new jscrollpane(table)); f.setsize(500, 500); f.setvisible(true); } public static void main(string[] args) { javax.swing.swingutilities.invokelater(new runnable() { public void run() { try { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); //uimanager.setlookandfeel(uimanager.getcrossplatformlookandfeelclassname()); } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception e) {} createandshowgui(); } }); } }
"focus border" on selected row:
"focus border" no rows selected:
"focus border" in windows laf:
i want "focus border" include cells of row (when visible).
below same code i've added part of @camickr 's code:
public class tabletest { private static void createandshowgui() { int rows = 20; int cols = 2; string[] headers = { "column 1", "column 2" }; string[][] data = new string[rows][cols]; (int j = 0; j < rows; j++) (int k = 0; k < cols; k++) data[j][k] = "item " + (j * cols + k + 1); // added code jtable table = new jtable() { private border outside = new matteborder(1, 0, 1, 0, color.black); private border inside = new emptyborder(0, 1, 0, 1); private border border = new compoundborder(outside, inside); @override public component preparerenderer(tablecellrenderer renderer, int row, int column) { component c = super.preparerenderer(renderer, row, column); jcomponent jc = (jcomponent) c; // add border selected row if (isrowselected(row)) jc.setborder(border); return c; } }; defaulttablemodel tablemodel = new defaulttablemodel(data, headers) { // disable editing of cells @override public boolean iscelleditable(int row, int column) { return false; } }; table.setmodel(tablemodel); table.setshowgrid(false); table.setintercellspacing(new dimension(0,0)); // key binding remove rows inputmap inputmap = table.getinputmap(jcomponent.when_ancestor_of_focused_component); actionmap actionmap = table.getactionmap(); inputmap.put(keystroke.getkeystroke(keyevent.vk_delete, 0), "remove"); actionmap.put("remove", new abstractaction() { @override public void actionperformed(actionevent e) { int[] rows = table.getselectedrows(); (int = rows.length - 1; >= 0; i--) { tablemodel.removerow(rows[i]); } } }); jframe f = new jframe(); f.setdefaultcloseoperation(jframe.exit_on_close); f.getcontentpane().add(new jscrollpane(table)); f.setsize(500, 500); f.setvisible(true); } public static void main(string[] args) { javax.swing.swingutilities.invokelater(new runnable() { public void run() { try { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); //uimanager.setlookandfeel(uimanager.getcrossplatformlookandfeelclassname()); } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception e) {} createandshowgui(); } }); } }
this adds border around entire row (but no left/right inset include cells). not "focus border/rectangle", border appears around selected row only. when delete row selected row cleared , focus border reappears.
note not want hide focus border (i know how it), want keep functioning include cells of row instead of 1 cell when becomes visible.
i want display focusborder around entire row.
take @ table row rendering.
it shows 1 way put border on row instead of individual cells.
Comments
Post a Comment