Hi..., i have a problem. if anyone can please give me some suggestion then i could able to proceed further. My problem is that i need to show database values in JTable. i have two database table and i query to display data in a JTable.

I am just sending a part of my code. I am using MultipleColumnHeader in JTable. When i try to run then it gives me runtime error saying---
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 >= 0
at java.util.Vector.elementAt(Unknown Source)
at javax.swing.table.DefaultTableColumnModel.getColumn(Unknown Source)
at com.ws.ui.CompHoursPanel.getCompareDataPanel2(CompHoursPanel.java:207)
at com.ws.ui.CompHoursPanel.compHoursPanel(CompHoursPanel.java:254)

Please give me some suggestion. Any help from you will highly be appreciated. Thanks.

private JPanel getCompareDataPanel2(){
JPanel panel=new JPanel();

DaoWeatherImpl dao=new DaoWeatherImpl();

//Result set is taken from another class called DaoWeatherImpl
//and there is a method called getCompareDataStatus()

ResultSet rs=dao.getCompareDataStatus();	
DefaultTableModel model=new DefaultTableModel();

String[] a = {"Time","O","F","O","F","O","F","O","F"};
try
{
while (rs.next())
	{
  	a[0]=rs.getString("TimeStamp").substring(11,16);
  	a[1]=rs.getString("Temperature");
  	a[2]=rs.getString("Temperature");
  	a[3]=rs.getString("Rainfall");
  	a[4]=rs.getString("Rainfall");
  	a[5]=rs.getString("Wind");
  	a[6]=rs.getString("Wind");
  	a[7]=rs.getString("Rh");
  	a[8]=rs.getString("Rh");
   		
   model.addRow(a);                       
	}
}catch (Exception e){
JOptionPane.showMessageDialog(null, "Exception : "+e, "Error", JOptionPane.ERROR_MESSAGE);
}
	JTable jtab=new JTable(model){
	protected JTableHeader createDefaultTableHeader() {
		return new GroupableTableHeader(columnModel);
		}
	};

TableColumnModel cm = jtab.getColumnModel();
ColumnGroup g_temp = new ColumnGroup(null,"Temperature");
	g_temp.add(cm.getColumn(1));
	g_temp.add(cm.getColumn(2));
ColumnGroup g_rf = new ColumnGroup(null,"Rainfall");
	g_rf.add(cm.getColumn(3));
	g_rf.add(cm.getColumn(4));
ColumnGroup g_wind = new ColumnGroup(null,"Wind");
	g_wind.add(cm.getColumn(5));
	g_wind.add(cm.getColumn(6));
ColumnGroup g_rh = new ColumnGroup(null,"Rh");
	g_rh.add(cm.getColumn(7));
	g_rh.add(cm.getColumn(8));

GroupableTableHeader header = (GroupableTableHeader)jtab.getTableHeader();
	header.addColumnGroup(g_temp);
	header.addColumnGroup(g_rf);
	header.addColumnGroup(g_wind);
	header.addColumnGroup(g_rh);

JScrollPane scroll= new JScrollPane( jtab );
scroll.setBackground(Color.WHITE);

panel.add(scroll,BorderLayout.CENTER);
return panel;
}

Recommended Answers

All 9 Replies

Post the line where you get the exception.
If you read the stack trace you will find it. Also it is clear from the message what the problem is:
java.lang.ArrayIndexOutOfBoundsException: 1 >= 0
You are trying to access an array (or some thing like that) with invalid index.

Also this is wrong:
rs.getString("TimeStamp").substring(11,16)
How do you know that the string represantation of the Time stamp would have that length (16).

What kind of sql type is TimeStamp? If it is date try this:

java.sql.TimeStamp stamp = rs.getTimeStamp("TimeStamp");
if (stamp!=null) {
  java.util.Date timeStamp = new Date(stamp.getTime());
  // use the SimpleDateFormat class to format the java.util.Date timeStamp into the String that you want
}

Thank you for your reply. Sorry, i did not mentioned where i am getting the problem.
It is showing me error in----

g_temp.add(cm.getColumn(1));

my TimeStamp data type is datetime and it contain value like 2011-04-13 10:00:00. So that is why i am taking substring of TimeStamp. Actually i do not know why i am getting the error.

maybe about timing

1/ create for gui
TabelModel
TableHeader (I never use something similair, start with plain TableHeader )
JTable
JPanel
JFrame


2/ add data from db

3/ myFrame.setVisible(true)

Thank you for your reply. But actually i need to use multiple column header. i have found an example in google explaining about this.

http://www.esus.com/docs/GetQuestionPage.jsp?uid=1272

But now i could not able to use it. Actually in the link it is using hardcoded data and i need to take the data from database. If u can give me some suggestion then i would be very thankful to you....

It seems that the cm.getColumn(1) doesn't have any columns: java.lang.ArrayIndexOutOfBoundsException: 1 >= 0
Meaning that the jtab.getColumnModel() doesn't have a ColumnModel.

You could try to look the API for JTable and/or DefaultTableModel becasue I find strange the way you create your JTable instance.
I think that maybe you don't declare at the model the columns.

Also you should do this even if it is not part of your problem:

// String[] a = {"Time","O","F","O","F","O","F","O","F"};
while (rs.next())
	{
String [] a = new String[9];

        String tst = rs.getString("TimeStamp");
  	if (tst!=null) a[0]=tst.substring(11,16);

  	a[1]=rs.getString("Temperature");
  	a[2]=rs.getString("Temperature");
  	a[3]=rs.getString("Rainfall");
  	a[4]=rs.getString("Rainfall");
  	a[5]=rs.getString("Wind");
  	a[6]=rs.getString("Wind");
  	a[7]=rs.getString("Rh");
  	a[8]=rs.getString("Rh");
   		
   model.addRow(a);                       
	}

Hello,

I am new in this forum & am really liking this place.

I work for Digital Promoters, who specialize in web advertising & promotion.

Thank You,
WM

Thank you. Now i could able to know why i am doing wrong. Please guide me. I could not able to use database values. Would you please take me to the right way. I could not able to understand how i can use database values....

I am trying, but not able to find the right track. Please guide me....

Are you getting the same error?
Did my suggestion work?
What changes did you do?
What is the error now?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.