• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JTable

 
Ranch Hand
Posts: 389
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am able to populate the table with flight data but now I intend to add a checkbox in the last column for the user to select a particular flight.
Now my code doesn't work and I am getting ArrayIndexOutOfBoundsException. I am not able to resolve this bug for long.
String[] columnTitles = { "Flight #" , "Origin Airport" , "Destination Airport" , "Carrier #" , "Price" , "Day" , "Time" , "Duration" , "Available Seats" , "Select" };
DataInfo[] info = client.getInitialFlightInformation(count);
for ( int i = 0; i < info.length; i++ ) {
if ( info[i] != null ) {
dataEntries[i] = info[i].getValues();
for ( int j = 0; j < len-1; j++ ) {
table.setValueAt( dataEntries[i][j] , i, j );
} // end of for
table.setValueAt( new Boolean(false), i, len-1 );
} // end of if
} // end of for clause
Thanks
Ravi
 
Ranch Hand
Posts: 560
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is "len"?
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My wild guess, is that there is no data in that last field, as we should know because you are going to put a checkbox there. However I think that the JTable is using that columns ordinality in some "getValueAt" method to look at the DataInfo object, and finding that the ordinality is larger than the number of columns in the DataInfo object, which we already know is true.
I don't know how to solve it though.
Mark
 
ravi janap
Ranch Hand
Posts: 389
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sai
int len = columnTitles.length;
Could someone share there code with me for populating the JTable. I Have been stuck with this problem for long and not able to proceed with my assignment further.
Thanks
Ravi
 
Sai Prasad
Ranch Hand
Posts: 560
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If len = columnTitles.length(), then you will get ArrayIndexOutOfBoundException because of the line
 
ravi janap
Ranch Hand
Posts: 389
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mark
Please verify my code for JTable.
dataEntries = new Object[count][len];
DataInfo dataInfo = null;
TableModel model = new EditableTableModel(columnTitles, dataEntries);
JTable table = new JTable(model);
table.createDefaultColumnsFromModel();
Thanks
Ravi
 
ravi janap
Ranch Hand
Posts: 389
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sai
If len = columnTitles.length(), then you will get ArrayIndexOutOfBoundException because of the line
Please explain this :
According to me the i and j value are within the range only i.e i is total record count and j is ( len - 1 ).
Thanks
-- Ravi
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In DataInfo there are n number of fields, in your Object array for columns, with the CheckBox, you have n+1
Meaning say there 9 fields in DataInfo, then there are 10 Columns in the JTable, you have the 9 fields plus one with CheckBox, so in that line that Sai pointed out you are trying to get column 10 in the DataInfo class which does not exist, hence ArrayIndexOutOfBounds exception.

dataEntries = new Object[count][len];
DataInfo dataInfo = null;
TableModel model = new EditableTableModel(columnTitles, dataEntries);
JTable table = new JTable(model);
table.createDefaultColumnsFromModel();


Where is this code located, what method, what class, why are you calling createDefaultColumnsFromModel. If you read about AbstractTableModel you will see that by only overriding a couple of methods you shouldn't have to make such a call, the JTable and TableModel handles the calls for you.
I wish I could post my code to show how to do this, but I can't because it would be giving too much away.
Mark
 
ravi janap
Ranch Hand
Posts: 389
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mark
The code for 'EditableTableModel' class follows :
package suncertify.client;
import javax.swing.table.AbstractTableModel;
public class EditableTableModel extends AbstractTableModel {
String[] columnTitles;
Object[][] dataEntries;
int rowCount;
// Model Constructor.
public EditableTableModel(String[] columnTitles, Object[][] dataEntries) {
this.columnTitles = columnTitles;
this.dataEntries = dataEntries;
}
// Retrieve the number of rows for the table to be prepared.
public int getRowCount() {
return dataEntries.length;
}
// Retrieve the column count.
public int getColumnCount() {
return columnTitles.length;
}
// Retrieve each of the table values at the specified
// row and column
public Object getValueAt(int row , int column) {
return dataEntries[row][column];
}
// Retrieve the column names to be used in the table header.
public String getColumnName(int column) {
return columnTitles[column];
}
// Retrieve the class types of the entries in
// each of the table columns.
public Class getColumnClass(int column) {
return getValueAt(0, column).getClass();
}
// Allow all the table cell to be editable.
public boolean isCellEditable(int row, int column) {
return true;
}
// Assign the data entered to the data model.
public void setValueAt(Object value, int row, int column) {
dataEntries[row][column] = value;
}
}
I called table.createDefaultColumnsFromModel();
This method creates default columns for the table from the data model using the getColumnCount method defined in the TableModel interface.
Thanks
Ravi
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All
I have a peculiar problem. I am using a JTable where the client can enter Passenger Data.
Suppose there are 5 fields where data needs to be entered. After entering all the data. when the client clicks on a button 'book' - i am unable to retrieve the data using
String value = (String)clientTable.getValueAt(0, i);
This because I am able to retrieve the value only after that cell has lost focus.
But logically a client might not want to put in an additional tab or a click since he has already entered the data.
Is there any way to around this problem. Please advice urgently
Regards einstien
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I called table.createDefaultColumnsFromModel();
Why? I read what it does in your post, but I took that for granted without calling that method. What I mean is that I add my TableModel and the JTable will display it correctly without me doing anything else.
Einstein - Why have that in your assignment, there isn't a requirement to have them enter passenger information. So why spend the time pulling your hair out to have it in there. Just don't have that and that solves your problem.
Now to my problem. I know that there is a slight chance that Einstien Albert, but I doubt it. We have one main rule at JavaRanch, and that is that everyone uses their real name. Heck you know my name, and a bunch of other people's names here. So we should know yours.
You can change it here.
Thanks
Mark
 
reply
    Bookmark Topic Watch Topic
  • New Topic