java - Jtable Select row and columns -
i want select row , column of jtable's cell , print value, in public void changevalue()
,as shown below, got error:
exception in thread "main" java.lang.arrayindexoutofboundsexception: -1 @ java.util.vector.elementdata(unknown source) @ java.util.vector.elementat(unknown source) @ javax.swing.table.defaulttablemodel.getvalueat(unknown source) @ javax.swing.jtable.getvalueat(unknown source) @ shopmanagement.showemployee.changevalue(showemployee.java:81) @ shopmanagement.showemployee.<init>(showemployee.java:74) @ shopmanagement.showemployee.main(showemployee.java:87)
maybe,have implement mouselistener used check current position of cursor in jtable?
if so,how can change code?
import java.awt.*; import java.sql.*; import java.util.*; import javax.swing.*; import databaseconnectionsingleton.connection; import databaseconnectionsingleton.creationstatement; public class showemployee extends jframe { public jtable table = new jtable(); public showemployee() { vector<string> columnnames = new vector<string>(); vector<vector<object>> data = new vector<vector<object>>(); try { connection.getconnectioninstance(); statement st = creationstatement.getcreationstatementinstance(); resultset rs = st.executequery("select * employee"); resultsetmetadata md = rs.getmetadata(); int columns = md.getcolumncount(); // column names (int = 1; <= columns; i++) { columnnames.addelement(md.getcolumnname(i)); } // row data while (rs.next()) { vector<object> row = new vector<object>(columns); (int = 1; <= columns; i++) { row.addelement(rs.getobject(i)); } data.addelement(row); } } catch (exception e) { system.out.println(e); } // create table database data table = new jtable(data, columnnames) { /** * */ private static final long serialversionuid = 1l; public class<?> getcolumnclass(int column) { (int row = 0; row < getrowcount(); row++) { object o = getvalueat(row, column); if (o != null) { return o.getclass(); } } return object.class; } }; jscrollpane scrollpane = new jscrollpane(table); getcontentpane().add(scrollpane); jpanel buttonpanel = new jpanel(); getcontentpane().add(buttonpanel, borderlayout.south); changevalue(); } public void changevalue() { int rowindex = table.getselectedrow(); int colindex = table.getselectedcolumn(); string s=(string) table.getvalueat(rowindex,colindex); system.out.println("innertable:" + rowindex + "*" + colindex+" "+s); } public static void main(string[] args) { showemployee frame = new showemployee(); frame.setdefaultcloseoperation(exit_on_close); frame.pack(); frame.setvisible(true); } }
thanks in advance.
as calling changevalue()
on form load , gives rowindx , columnindex -1 because nothing selected on form load.
you can using jbutton's actionlistener
method. want print selected data add button
called print
.you can use actionlistener on print
button.
look @ example
public class tablevalueprint extends jframe implements actionlistener{ private final jbutton print; private final jtable table; public tablevalueprint() { string[] columnnames = {"a", "b", "c"}; object[][] data = { {"moni", "adsad", "pass"}, {"jhon", "ewrewr", "fail"}, {"max", "zxczxc", "pass"} }; table = new jtable(data, columnnames); jscrollpane tablesp = new jscrollpane(table); jpanel tablepanel = new jpanel(); tablepanel.add(tablesp); tablepanel.setbackground(color.red); add(tablepanel); settitle("result"); setsize(1000,700); print=new jbutton("print"); jpanel jpi1 = new jpanel(); jpi1.add(print); tablepanel.add(jpi1,borderlayout.south); print.addactionlistener(this); setlocationrelativeto(null); setdefaultcloseoperation(exit_on_close); } public static void main(string[] args) { swingutilities.invokelater(new runnable() { public void run() { tablevalueprint ex = new tablevalueprint(); ex.setvisible(true); } }); } @override public void actionperformed(actionevent ae) { if(ae.getsource()==print){ int rowindex = table.getselectedrow(); int colindex = table.getselectedcolumn(); string s=(string) table.getvalueat(rowindex,colindex); system.out.println("innertable:" + rowindex + "*" + colindex+" "+s); } } }
output :
innertable:0*1 adsad innertable:2*2 pass innertable:1*1 ewrewr innertable:2*0 max
Comments
Post a Comment