java - Usability features for JComboBox within JTable -
i'm trying build table includes jcomboboxes both renderer , editor components. works fine, there 2 things can't seem solve.
- tabbing between cells should make jcombobox active
- clicking drop-down arrow should open option list
regarding 1, editable combo should place focus within embedded text field, fixed combo should allow down arrow open list of options.
regarding 2, find works depending on other cell active, other times have double click. cannot make behaviour consistent.
for convenience have included clear example (i believe) uses recommended approach embedding jcomboboxes within jtables.
thank constructive advice.
import java.awt.component; import java.util.*; import javax.swing.*; import javax.swing.table.*; public class tablecombos { public static void main(string[] args) { jframe frame = new jframe("frame"); frame.setdefaultcloseoperation(windowconstants.exit_on_close); abstracttablemodel model = new defaulttablemodel() { string[] columnheaders = new string[]{"label", "combo-edit", "combo-fixed"}; class<?>[] columnclasses = new class<?>[]{string.class, double.class, double.class}; list<object[]> data = new arraylist<>(); { data.add(new object[]{"row 1", 1.0d, 2.0d}); data.add(new object[]{"row 2", 2.0d, 3.0d}); } @override public int getcolumncount() { return columnheaders.length; } @override public boolean iscelleditable(int row, int column) { return column != 0; } @override public int getrowcount() { if (data == null) // race condition return 0; return data.size(); } @override public object getvalueat(int row, int column) { return data.get(row)[column]; } @override public void setvalueat(object avalue, int row, int column) { data.get(row)[column] = avalue; } @override public class<?> getcolumnclass(int column) { return columnclasses[column]; } @override public string getcolumnname(int column) { return columnheaders[column]; } }; jtable table = new jtable(model); table.setsurrendersfocusonkeystroke(true); tablecolumn c1 = table.getcolumnmodel().getcolumn(1); tablecolumn c2 = table.getcolumnmodel().getcolumn(2); jcombobox<double> editorcomboeditable = new jcombobox<>(new double[]{1.0d, 2.0d, 3.0d}); editorcomboeditable.seteditable(true); c1.setcelleditor(new defaultcelleditor(editorcomboeditable)); c2.setcelleditor(new defaultcelleditor(new jcombobox<>(new double[]{1.0d, 2.0d, 3.0d}))); final jcombobox<double> renderercomboeditable = new jcombobox<>(new double[]{1.0d, 2.0d, 3.0d}); renderercomboeditable.seteditable(true); final jcombobox<double> renderercombofixed = new jcombobox<>(new double[]{1.0d, 2.0d, 3.0d}); c1.setcellrenderer(new tablecellrenderer() { @override public component gettablecellrenderercomponent(jtable t, object value, boolean isselected, boolean hasfocus, int row, int column) { renderercomboeditable.setselecteditem(value); return renderercomboeditable; } }); c2.setcellrenderer(new tablecellrenderer() { @override public component gettablecellrenderercomponent(jtable t, object value, boolean isselected, boolean hasfocus, int row, int column) { renderercombofixed.setselecteditem(value); return renderercombofixed; } }); frame.getcontentpane().add(new jscrollpane(table)); frame.pack(); frame.setvisible(true); } }
both of issues resolved overriding defaultcelleditor#iscelleditable
method , returning true
you may need set jtable#setsurrendersfocusonkeystroke
true well
update
the short answer "it's messy". long answer "it's messy"
i did "continuos editing" process time back. basically, overrode enter , tab key bindings.
what i'd did stop active cell editor, take note of current cell , tried finding next editable cell, looping around start (cell 0x0) if required.
when found editable cell, called jtable#esitcellat
start editing cell.
to popup visible when cell starts editing, need override addnotify
method of jcombobox
and, using swingutilities#invokelater
, set popup visible
Comments
Post a Comment