java - Make cell editable with JButton -
i'm stuck 2 days on piece of code, cells of table non-editable default , want editable when click on edit button, can not run code. thank in advance help.
table = new jtable(model) { public boolean iscelleditable(int row, int column) { return false; } }; table.setselectionmode(listselectionmodel.single_selection); sorter = new tablerowsorter < tablemodel > (model); table.setpreferredscrollableviewportsize(new dimension(560, 200)); jscrollpane scrollpane = new jscrollpane((table)); table.setrowsorter(sorter); add(scrollpane); scrollpane.setbounds(10, 180, 560, 200); table.setrowheight(28); table.setbackground(color.dark_gray); table.setforeground(color.white); modifier.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { if ((jbutton) e.getsource() == change) { boolean iscelleditable(int row, int column) { return true; } } } });
put enabled cell (row, col) set , make table use set check if cell enabled.
package javaapplication33; import java.awt.borderlayout; import java.awt.dimension; import javax.swing.jframe; import javax.swing.jbutton; import javax.swing.jpanel; import javax.swing.jtable; import javax.swing.jlabel; import javax.swing.listselectionmodel; import javax.swing.jscrollpane; import javax.swing.action; import javax.swing.abstractaction; import javax.swing.jtextfield; import java.awt.point; import java.awt.event.actionevent; import java.util.set; import java.util.hashset; import java.util.collection; import java.awt.eventqueue; import javax.swing.box; import java.awt.color; import javax.swing.boxlayout; import javax.swing.table.abstracttablemodel; public class javaapplication33 { public static void main(string[] args) { runnable r = new runnable() { @override public void run() { jframe f = new jframe(); f.setdefaultcloseoperation(jframe.exit_on_close); jpanel p = new jpanel(new borderlayout()); p.setpreferredsize(new dimension(300,300)); final set<point> erc = new hashset<>(); //this set contains (row,cell) pairs of editable cells //put data table final jtable table = new jtable(new string[][] {{"a", "aa"}, {"b", "bb"}, {"c", "cc"}}, new string[]{"col1", "col2"}) //name columns { collection<point> editablerowscells = erc; collection<point> geteditablerowscells() { return editablerowscells; } public boolean iscelleditable(int row, int column) { return geteditablerowscells() .contains(new point(row, column)); } }; table.setselectionmode(listselectionmodel.single_selection); jscrollpane scrollpane = new jscrollpane(table); p.add(scrollpane, borderlayout.center); table.setrowheight(28); table.setbackground(color.dark_gray); table.setforeground(color.white); final jtextfield xf = new jtextfield(); final jtextfield yf = new jtextfield(); action enableaction = new abstractaction("enable") { @override public void actionperformed(actionevent e) { int row = integer.valueof(xf.gettext()); int col = integer.valueof(yf.gettext()); erc.add(new point(row,col)); ((abstracttablemodel) table.getmodel()) .firetablecellupdated(row, col); } }; jbutton enablebutton = new jbutton(); enablebutton.setaction(enableaction); box b = new box(boxlayout.x_axis); b.add(new jlabel("row")); b.add(xf); b.add(new jlabel("col")); b.add(yf); b.add(enablebutton); p.add(b, borderlayout.north); f.add(p); f.pack(); f.setvisible(true); } }; eventqueue.invokelater(r); } }
this output:
update: to make cells editable
package javaapplication33; import java.awt.borderlayout; import java.awt.dimension; import javax.swing.jframe; import javax.swing.jbutton; import javax.swing.jpanel; import javax.swing.jtable; import javax.swing.listselectionmodel; import javax.swing.jscrollpane; import javax.swing.action; import javax.swing.abstractaction; import java.awt.event.actionevent; import java.awt.eventqueue; import java.awt.color; import javax.swing.table.abstracttablemodel; public class javaapplication33 { public static class toggle { boolean val; public toggle(boolean val) { this.val = val; } public void setval(boolean val) { this.val = val; } public boolean getval() { return val; } public void toggle() { setval(!getval()); } } public static void main(string[] args) { runnable r = new runnable() { @override public void run() { jframe f = new jframe(); f.setdefaultcloseoperation(jframe.exit_on_close); jpanel p = new jpanel(new borderlayout()); p.setpreferredsize(new dimension(300, 300)); final toggle editable = new toggle(false); final jtable table = new jtable( new string[][]{ {"a", "aa"}, {"b", "bb"}, {"c", "cc"}}, new string[] {"col1", "col2"}) //name columns { public boolean iscelleditable(int row, int column) { return editable.getval(); //request current value } }; table.setselectionmode(listselectionmodel.single_selection); jscrollpane scrollpane = new jscrollpane(table); p.add(scrollpane, borderlayout.center); table.setrowheight(28); table.setbackground(color.dark_gray); table.setforeground(color.white); action enableaction = new abstractaction("toggle editable") { @override public void actionperformed(actionevent e) { editable.toggle(); //toggle current value ((abstracttablemodel) table.getmodel()).firetablestructurechanged(); } }; jbutton enablebutton = new jbutton(); enablebutton.setaction(enableaction); p.add(enablebutton, borderlayout.north); f.add(p); f.pack(); f.setvisible(true); } }; eventqueue.invokelater(r); } }
output:
Comments
Post a Comment