Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable; Vectors & Boolean values

 
Fiona Healy
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,

I am having a couple of problems with my attempts to populate a JTable with 2 vectors. One for the data and one representing column names.

1)I am getting a ClassCastException when i try to pass the vector to my JTable

2)I am trying to incorporate a boolean value in my table even though it is not sourced from the database like the rest of the data. All i am trying to do is add a new element to the vector holding the data.

Can anyone help?

Code:
package com.rgs.client;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import java.awt.event.*;
import java.util.*;
import com.rgs.ejb.reportdefinition.*;

public class Definition extends JFrame//implements Listener
{
private JTable tblReportDefinitions;
private Vector definitionColumns;
private Vector definitionData;
private Vector reports;
private Vector schedules;

//private ReportTableModel reportModel;
//private DefaultTableModel dtm ;

public Definition(Vector rep)//Vector sched
{
super("Report Definition");
reports = rep;

definitionColumns = new Vector();
definitionColumns.add("Delete?");
definitionColumns.add("Report Name");
definitionColumns.add("Report Requester");
definitionColumns.add("Destination");
//definitionColumns.add("Generation Frequency");
//definitionColumns.add("Start Date");
//definitionColumns.add("End Date");
//definitionColumns.add("Generation Time");

definitionData = new Vector();
definitionData.add(new Boolean(true));

//loop through vector of report definition objects
ReportDefinitionPOJO pojo = new ReportDefinitionPOJO();
for(Enumeration e = reports.elements();e.hasMoreElements()
{
pojo = (ReportDefinitionPOJO)e.nextElement();
//holds data in an individual row

definitionData.add(0, new Boolean(false));
definitionData.add(pojo.getName());
definitionData.add(pojo.getRequester());
definitionData.add(pojo.getDestination());
}
//refrsh the vector with the data
System.out.println(definitionData);
System.out.println(definitionColumns);
//dtm.setDataVector(definitionData, definitionColumns);
tblReportDefinitions = new JTable(definitionData, definitionColumns);


getContentPane().setLayout (new BorderLayout());

Action deleteAction = new DeleteAction();
EnhancedJToolBar toolBar = new EnhancedJToolBar();

toolBar.showButtonText(true);
toolBar.setFloatable(true);
toolBar.add(deleteAction);

JScrollPane scrollPane = new JScrollPane (tblReportDefinitions);
Container con = getContentPane();
con.add(toolBar,BorderLayout.NORTH);
con.add(scrollPane, BorderLayout.CENTER);
setSize(500, 400);
setLocation(200, 200);
setVisible(true);
}
private void delete()
{
//deleteDefinition(pojo);
}
class DeleteAction extends AbstractAction
{
DeleteAction()
{
super("Delete");
putValue(Action.SMALL_ICON,IconUtil.getIcon("com/rgs/client/delete.gif"));
}

public void actionPerformed(ActionEvent evt){
delete();
}
}

}

Error:
[java] java.lang.ClassCastException
[java] at javax.swing.table.DefaultTableModel.justifyRows(DefaultTableM
odel.java:238)
[java] at javax.swing.table.DefaultTableModel.setDataVector(DefaultTabl
eModel.java:194)
[java] at javax.swing.table.DefaultTableModel.<init>(DefaultTableModel.
java:131)
[java] at javax.swing.JTable.<init>(JTable.java:403)
[java] at com.rgs.client.Definition.<init>(Definition.java:55)
[java] at com.rgs.client.ReportDefinitionGUI.delete(ReportDefinitionGUI
.java:118)
[java] at com.rgs.client.ReportDefinitionGUI.access$300(ReportDefinitio
nGUI.java:13)
[java] at com.rgs.client.ReportDefinitionGUI$DeleteAction.actionPerform
ed(ReportDefinitionGUI.java:188)
[java] at javax.swing.AbstractButton.fireActionPerformed(AbstractButton
.java:1786)
[java] at javax.swing.AbstractButton$ForwardActionEvents.actionPerforme
d(AbstractButton.java:1839)
[java] at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultBut
tonModel.java:420)
[java] at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.
java:258)
[java] at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Basi
cButtonListener.java:245)
[java] at java.awt.Component.processMouseEvent(Component.java:5100)
[java] at java.awt.Component.processEvent(Component.java:4897)
[java] at java.awt.Container.processEvent(Container.java:1569)
[java] at java.awt.Component.dispatchEventImpl(Component.java:3615)
[java] at java.awt.Container.dispatchEventImpl(Container.java:1627)
[java] at java.awt.Component.dispatchEvent(Component.java:3477)
[java] at java.awt.LightweightDispatcher.retargetMouseEvent(Container.j
ava:3483)
[java] at java.awt.LightweightDispatcher.processMouseEvent(Container.ja
va:3198)
[java] at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3
128)
[java] at java.awt.Container.dispatchEventImpl(Container.java:1613)
[java] at java.awt.Window.dispatchEventImpl(Window.java:1606)
[java] at java.awt.Component.dispatchEvent(Component.java:3477)
[java] at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
[java] at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDi
spatchThread.java:201)
[java] at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDisp
atchThread.java:151)
[java] at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.j
ava:145)
[java] at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.j
ava:137)
[java] at java.awt.EventDispatchThread.run(EventDispatchThread.java:100
)
 
jefff willis
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is just a quick evaluation of your code, but I believe I see yout create fours columns of data headers and you try to fill it with five columns of data.


(there are two Boolean elements at the beginning of your data Vector)
 
Fiona Healy
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it doesnt make a difference. This is just me messin with the code and experimenting with different options...still gettin the same silly error..but thanx anyway
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this post from a couple of days ago might be worth a look

http://www.coderanch.com/t/339953/GUI/java/checkbox-jTable
 
Fiona Healy
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is still some issue with passing vectors as opposed to String[] which is something i need to do. I read on a forum that you need to pass the model a Vector of Vectors??? How would you make a vector of vectors for the column names?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>I read on a forum that you need to pass the model a Vector of Vectors??? >How would you make a vector of vectors for the column names?

I think the vector of vectors is the row data, not the column names (single vector)

your row data 'vector of vectors' would probably be something like this


definitionData = new Vector();
//definitionData.add(new Boolean(true));//removed

//loop through vector of report definition objects
ReportDefinitionPOJO pojo = new ReportDefinitionPOJO();
for(Enumeration e = reports.elements();e.hasMoreElements()
{
pojo = (ReportDefinitionPOJO)e.nextElement();
//holds data in an individual row
Vector vec = new Vector();
vec.add(0, new Boolean(false));
vec.add(pojo.getName());
vec.add(pojo.getRequester());
vec.add(pojo.getDestination());
definitionData.add(vec);
}
 
jefff willis
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cut and paste this code to see an example that runs:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic