This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Swing / AWT / SWT and the fly likes JTable; Vectors &  Boolean values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable; Vectors &  Boolean values" Watch "JTable; Vectors &  Boolean values" New topic
Author

JTable; Vectors & Boolean values

Fiona Healy
Greenhorn

Joined: Jan 27, 2005
Posts: 21
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

Joined: Sep 29, 2004
Posts: 113
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

Joined: Jan 27, 2005
Posts: 21
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

Joined: Jun 09, 2003
Posts: 4632
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

Joined: Jan 27, 2005
Posts: 21
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

Joined: Jun 09, 2003
Posts: 4632
>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

Joined: Sep 29, 2004
Posts: 113
cut and paste this code to see an example that runs:

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTable; Vectors & Boolean values