Results 1 to 7 of 7
- 02-11-2010, 01:07 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 20
- Rep Power
- 0
array Index out of Bounds exception== 0
Hi I cant figure why does the program telling me an ArrayIndexOutOfBounds when adding a row in a Table?
Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.*; public class DataBaseSample { private JTable mainTable; private JScrollPane sp ; private JViewport jv; private JTable rowHeaderTableColumn; private JPanel panel; private JButton rowRemover; private JFrame frame; private TableModel tableModel; private DefaultTableModel mainTableModel; private Integer[] rowHeaderData; public DataBaseSample() { frame = new JFrame(); rowRemover = new JButton("Add Row"); panel = new JPanel(); tableModel = new AbstractTableModel() { String[] header = {"Rows"}; public int getColumnCount() { return 1; } public int getRowCount() { return mainTable.getRowCount(); } @Override public String getColumnName(int col) { // this will set the row headers column name return header[0]; } public Object getValueAt(int row, int col) { // this will set the row headers row data return rowHeaderData[col] + row; } }; TableColumnModel rowHeaderModel = new DefaultTableColumnModel() { boolean first = true; @Override public void addColumn(TableColumn tableColumn) { if (first) { tableColumn.setMaxWidth(tableColumn.getPreferredWidth()); super.addColumn(tableColumn); tableColumn.setMaxWidth(100); first = false; } } }; Object[][] rows = new Object[0][3]; String[] cols = {"Column 1", "Column 2", "Column 3"}; mainTableModel = new DefaultTableModel(rows, cols); mainTable = new JTable(mainTableModel); rowHeaderTableColumn = new JTable(tableModel, rowHeaderModel); rowHeaderTableColumn.createDefaultColumnsFromModel(); rowHeaderTableColumn.setBackground(Color.lightGray); rowHeaderTableColumn.setColumnSelectionAllowed(false); rowHeaderTableColumn.setCellSelectionEnabled(false); jv = new JViewport(); jv.setView(rowHeaderTableColumn); jv.setPreferredSize(rowHeaderTableColumn.getMaximumSize()); mainTable.setSelectionModel(rowHeaderTableColumn.getSelectionModel()); mainTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); panel.setLayout(new BorderLayout()); rowHeaderData = new Integer[mainTable.getRowCount()]; for (int count = 0; count <= mainTable.getRowCount() - 1; count++) { rowHeaderData[count] = count + 1; } sp = new JScrollPane(mainTable); sp.setRowHeader(jv); sp.setCorner(ScrollPaneConstants.UPPER_LEFT_CORNER, rowHeaderTableColumn.getTableHeader()); rowRemover.addActionListener(new RowRemoverButtonActionListener()); panel.add(rowRemover, BorderLayout.NORTH); panel.add(sp, BorderLayout.SOUTH); frame.getContentPane().add(panel); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(500, 500); frame.setLocationRelativeTo(null); frame.setVisible(true); } private class RowRemoverButtonActionListener implements ActionListener { public void actionPerformed(ActionEvent e) { // set to zero to avoid array index out of bounds -1 mainTableModel.addRow(new Object[] {null,null,null}); sp.setRowHeader(jv); } } public static void main(String args[]) { new DataBaseSample(); } }
- 02-11-2010, 01:29 PM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
I just rushed through the code, so I'm not a 100% on this, but I'm pretty sure this is the problem:
Why are you trying to create a table with 0 elements? int[] array = new int[1]; will have only one element at index 0, is this perhaps what you wanted? Since multidimensional arrays are formed like this (this is what java does in the background, I'm not saying you should do this):Java Code:Object[][] rows = new Object[0][3];
Java Code:int[][] array = new int[1][3]; //the equivalent int[] array2 = new int[1]; array2[0] = new int[3];
- 02-11-2010, 01:35 PM #3
Member
- Join Date
- Feb 2010
- Location
- Dehradun
- Posts
- 5
- Rep Power
- 0
Resolved
The declaration of your rows is wrong. You are declaring it with 0 rows.
Object[][] rows = new Object[0][3];
Solution:
Declare your object as
Object[][] rows = new Object[1][3];
- 02-11-2010, 01:38 PM #4
Member
- Join Date
- Dec 2009
- Posts
- 20
- Rep Power
- 0
i was just trying to put my program or table in a situation like this. what if the default number of rows is zero? just in case and forJava Code:Why are you trying to create a table with 0 elements?
the compiler is pointing on this part >i tried to make an if-else statement that if rowHeaderData.length == 0, do something, or col == 0 then do something and row == 0 , then do something..Java Code:public Object getValueAt(int row, int col) { // this will set the row headers row data return rowHeaderData[col] + row; }
i tried to make them 1 if their values are 0, but still the compier throws me an ArrayOutOfBoundsLast edited by Allgorythm; 02-11-2010 at 01:41 PM.
- 02-11-2010, 01:53 PM #5
Member
- Join Date
- Feb 2010
- Location
- Dehradun
- Posts
- 5
- Rep Power
- 0
Then you check it first that it is zero.
If it is zero then add a row from your side only.
-
Perhaps you want your rowHeaderData to not be an array but instead an ArrayList, and then when you add a row, add to this list as well.
edit: Ignore the above. I've not used row headers before, but perhaps something as simple as changing the row header's table model's getValueAt(..) to
but again, this is "shooting from the hip". Let's see what the experts say...Java Code:public Object getValueAt(int row, int col) { return "Row " + row; } };Last edited by Fubarable; 02-11-2010 at 03:28 PM.
- 02-11-2010, 04:02 PM #7
Member
- Join Date
- Dec 2009
- Posts
- 20
- Rep Power
- 0
:eek:
are you not sure of this? but it helped me! it SOLVED the problem!edit: Ignore the above. I've not used row headers before, but perhaps something as simple as changing the row header's table model's getValueAt(..) to
Code:
public Object getValueAt(int row, int col) {
return "Row " + row;
}
};
but again, this is "shooting from the hip". Let's see what the experts say...
i never thought of that, instead of-relying to the data counts of the main table ,your code can generate my rowheadertable its own data counts.. wow thanks!Java Code:return rowHeaderData[col] + row;
i just did this one. >i just only need a number that will represents the count for each row, i hope i wont encounter any future problems. thanksJava Code:public Object getValueAt(int row, int col) { return 1 + row; }
!
Similar Threads
-
[SOLVED] Array index out of bounds exception
By sruthi_2009 in forum New To JavaReplies: 5Last Post: 11-24-2010, 11:46 AM -
Array Index Out Of Bounds and Problem in Assigning Values
By chronoz1300 in forum New To JavaReplies: 2Last Post: 12-28-2009, 07:14 PM -
Array Index Out of Bounds Exception
By kool001 in forum New To JavaReplies: 1Last Post: 12-03-2009, 07:42 AM -
Array out of bounds exception 20.
By dropt in forum New To JavaReplies: 4Last Post: 09-21-2009, 10:32 PM -
Array Index out of bounds
By leapinlizard in forum New To JavaReplies: 5Last Post: 04-29-2009, 05:11 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks