java - Specific comboBox on a specific column number in a table -
i have jtable 17 columns. of these columns want comboboxes, others, don't. code:
public final jtable table; void setcelleditors(){ setbooleancelleditor (table); // combobox boolean values setintcelleditor (table); // combobox int values settypecelleditor (table); setanothertypecelleditor (table); // .. , on, types need comboboxes }
the celleditor function types this:
private void settypecelleditor (jtable jt) { defaultcelleditor dce = new defaultcelleditor (type.buildcombobox ()); jt.setdefaulteditor (type.class, dce); }
and works fine, because type unique table, in other words, have 1 column type boolean, 1 int, 1 anothertype etc. problem 2 columns have string values, need different comboboxes. meaning, code above doesn't work, because both string.class.
naturally, tried solving saying "on column 10 want combobox":
private void setyetanothertypecelleditor (jtable jt) { defaultcelleditor dce = new defaultcelleditor (yetanothertype.buildcombobox ()); if (jt.getcolumnmodel ().getcolumncount() > 0) { jt.getcolumnmodel ().getcolumn (9).setcelleditor (dce); } }
this, however, doesn't seem work , not know why. tried this guide doesn't help. basically, think setcelleditor isn't setting cell editor reason.
it difficult more specific because there lot of code behind this.
it difficult more specific because there lot of code behind this.
using combo box editor illustrates setting cell editor column. minimal, complete, , verifiable example shown below allow study problem in isolation.
import java.awt.eventqueue; import javax.swing.defaultcelleditor; import javax.swing.defaultcomboboxmodel; import javax.swing.jcombobox; import javax.swing.jframe; import javax.swing.jscrollpane; import javax.swing.jtable; import javax.swing.table.tablecelleditor; /** * @see https://stackoverflow.com/a/37435196/230513 */ public class test { private void display() { jframe f = new jframe("test"); f.setdefaultcloseoperation(jframe.exit_on_close); jtable table = new jtable(1, 2); table.getcolumnmodel().getcolumn(0).setcelleditor(type1.buildcombobox()); table.getcolumnmodel().getcolumn(1).setcelleditor(type2.buildcombobox()); f.add(new jscrollpane(table)); f.pack(); f.setlocationrelativeto(null); f.setvisible(true); } private static class type1 { private static tablecelleditor buildcombobox() { return new defaultcelleditor(new jcombobox( new defaultcomboboxmodel<>(new string[]{"a", "b", "c"}))); } } private static class type2 { private static tablecelleditor buildcombobox() { return new defaultcelleditor(new jcombobox( new defaultcomboboxmodel<>(new string[]{"x", "y", "z"}))); } } public static void main(string[] args) { eventqueue.invokelater(new test()::display); } }
Comments
Post a Comment