0

I'm newer to java as am a migrant from vb6 presnelty I'm shifting my staff and old equipments for last 15 years to Java now i would to load data isnide my Jtable relay on No. of records i mean i want to test where is the current records inside my Jtable and then upload my records behind that line my functions as per here under i used jtable number of row but it gave me error message

public void LoadLineInJtable(){ int RowNo= jTable1.getModel().getRowCount(); jTable1.setValueAt(jTjournal_submain_no.getText(), RowNo+1, 0); jTable1.setValueAt(jTjournal_submain_name.getText(), RowNo+1, 1); } 

when trying with above code gave me below error

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 9 >= 8 at java.util.Vector.elementAt(Vector.java:470) 

thank you for your input .

3
  • Please note changes to answer. Commented Jan 19, 2016 at 17:12
  • @Hovercraft what you mean , "Please note changes to answer"? Commented Jan 19, 2016 at 17:43
  • My answer has changed since I initially presented it. Commented Jan 19, 2016 at 17:58

2 Answers 2

3

Your JTable's row count is RowNum (which you should rename rowNum to comply with Java naming standards), and then you try to set values at a row that doesn't yet exist. Understand that JTable rows are 0 based, like Java arrays, and so you cannot manipulate the data of anything beyond the rowCount - 1.

If you want to add a row, you must use either the addRow(...) if your model is a DefaultTableModel or a child of this class, or else use your own add row method if you're using a custom table model.

So you might want something like this:

public void LoadLineInJtable(){ // get data and put into array or Vector String[] dataRow = { jTjournal_submain_no.getText(), jTjournal_submain_name.getText() }; // get table model and cast it to a DefaultTableModel DefaultTableModel model = (DefaultTableModel) jTable1.getModel(); // add row to model model.addRow(dataRow); } 
Sign up to request clarification or add additional context in comments.

Comments

2

use this to add a new row at the end of table:

((DefaultTableModel)jTable1.getModel()).addRow(new String[]{"column1","column2"}); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.