Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

JTable zand Vector

1 view
Skip to first unread message

MasterAnakan

unread,
Jul 31, 2001, 5:02:35 PM7/31/01
to
Hello everybody
I have a problem which will make me crazy

For those who like challenge here is the exception thrown at execution:
/*
C:\JAVA>java Modificator
Exception in thread "main" java.lang.ClassCastException: java.lang.String
at javax.swing.table.DefaultTableModel.justifyRows(Unknown Source)
at javax.swing.table.DefaultTableModel.setDataVector(Unknown Source)
at javax.swing.table.DefaultTableModel.<init>(Unknown Source)
at javax.swing.JTable.<init>(Unknown Source)
at Modificator.<init>(Modificator.java:49)
at Modificator.main(Modificator.java:124)

C:\JAVA>
*/

Here is the source I'd like to understand if i program that bad of it is
a standard bug


/**
* Modificator.java
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.*;
import java.util.*;

public class Modificator extends JFrame{

private String[] itemFileNames={"Open", "New", "Save", "Save as...", "Quit"};
private String[] itemEditNames={"Copy", "Cut", "Paste"};
private String[] menuNames={"File", "Edit"};

private Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
private Container pane=getContentPane();

private Vector header;
private Vector data;
private FileWatcher watchDog;


private JPanel centerPanel;

private JMenuItem[] item;
private JMenu[] menu;
private JMenuBar menuBar;
private JTable table;
private JScrollPane tableScroller;
private boolean debug;


public Modificator(FileWatcher watchDog){
this.watchDog=watchDog;
setBounds(screenSize.width/3,50, screenSize.width/2,screenSize.height/2);
setDefaultCloseOperation(EXIT_ON_CLOSE);
menuBar=initMenu();
centerPanel=new JPanel();
header=watchDog.loadTableHeader("header.dat");
data=watchDog.loadTableData(watchDog.getDataFiles());

table=new JTable(data, header);


tableScroller=new JScrollPane(table);
centerPanel.add(tableScroller);
pane.add(centerPanel, "Center");

show();


}

public void setDebugMode(boolean state){
this.debug=state;
}


private JMenuBar initMenu(){

int arraySize=itemFileNames.length+itemEditNames.length;
int itemLast=0;
item=new JMenuItem[arraySize];
menu=new JMenu[menuNames.length];
menuBar=new JMenuBar();


for(int a=0; a<menu.length; a++){
menu[a]=new JMenu(menuNames[a]);
menu[a].getAccessibleContext().setAccessibleName(menuNames[a]);
menu[a].setActionCommand(menuNames[a]);
}
for(int a=0; a<itemFileNames.length; a++){


item[a]=new JMenuItem(itemFileNames[a]);
item[a].getAccessibleContext().setAccessibleName(itemFileNames[a]);
item[a].setActionCommand(menuNames[0]);
itemLast=a+1;
if(debug){
System.out.println("items="+itemLast+"\n a="+a);
System.out.println("Accessible
name"+item[a].getAccessibleContext().getAccessibleName());
}
}


for(int a=itemLast, b=0; b<itemEditNames.length ;b++, a++){

item[a]=new JMenuItem(itemEditNames[b]);
item[a].getAccessibleContext().setAccessibleName(itemEditNames[b]);

item[a].setActionCommand(menuNames[1]);
if(debug){
System.out.println("itemLast="+itemLast+"\n a="+a+" b="+b);
System.out.println("Accessible
name"+item[a].getAccessibleContext().getAccessibleName());
}

}
for(int a=0;a<menu.length; a++){
for(int b=0; b<item.length; b++){
if(item[b].getActionCommand().equalsIgnoreCase(menuNames[a])){
menu[a].add(item[b]);
}
menuBar.add(menu[a]);
}

}

setJMenuBar(menuBar);
return menuBar;
}


public static void main(String[] arg){
Modificator m=new Modificator(new FileWatcher());
m.setDebugMode(true);
}


}

/**
*And the Second file
*FileWatcher.java
*/

import java.io.*;
import java.util.*;
import javax.swing.*;


public class FileWatcher implements Runnable{

public static final int MAX_LINES=100;
public int line;

/**
* loads the table header
*/

public Vector loadTableHeader(String fileName){
File file =new File(fileName);
Vector header=null;
String str=null;
try{
BufferedReader in=new BufferedReader(new InputStreamReader(new
FileInputStream(file)));
str=in.readLine();
in.close();
}catch(IOException ioe){
ioe.printStackTrace();
System.out.println("I/O Error While Loading or Table Header");
}
header=new Vector();
StringTokenizer token=new StringTokenizer(str, "|");
while(token.hasMoreTokens()){
header.addElement(token.nextToken());
}
return header;
}


/**
* Loads the data handled in every file
* when loading if the file contains more than MAX_LINES
* a new lile is created
*/


public Vector loadTableData(String[] fileName){
Vector data=null;
for(int a=0;a<fileName.length; a++){
File file =new File("./data/"+fileName[a]);
String str="";
try{
data=new Vector(1,1);
BufferedReader in=new BufferedReader(new InputStreamReader(new
FileInputStream(file)));
while((str=in.readLine()) !=null){
line++;
if(line==MAX_LINES){
new File(String.valueOf(a+1));
}

StringTokenizer token=new StringTokenizer(str, "|");

while(token.hasMoreTokens()){
String buf=token.nextToken();
System.out.println(buf);
data.addElement((String)buf);
}
}
in.close();
}catch(IOException ioe){
ioe.printStackTrace();
System.out.println("I/O Error While Loading or Table Header");
}

}


return data;
}

/**
* gets data Files in "data" directory
*/
public static String[] getDataFiles(){
String[] list=null;
File dir=new File("data");
if(dir.isDirectory()){
list=dir.list();

}
for(int a=0;a<list.length; a++){
System.out.println(list[a]);
}
return list;
}

/**
* updates a row given its number captured in "Number" column
* As the files contain MAX_LINES the dataNumber/MAX_LINES=file
* getting the file number if BufferedReader reads and finds a
* line ending with datanumber replaces it with newData
*
*/

public synchronized void update(String dataNumber, String[] newData){
String file=null;
String str=null;
BufferedReader in;
BufferedWriter out;
File temp=new File("temp.dat");
try{
int number=(int)(Integer.parseInt(dataNumber))/MAX_LINES;
file=String.valueOf(number+".dat");
}catch(NumberFormatException nfe){
System.out.println("Data Number is not Valid!!!!");
}
File f=new File(file);
try{
in=new BufferedReader(new InputStreamReader(new FileInputStream(f)));
out=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(temp)));
while((str=in.readLine()) !=null){
if(str.endsWith(dataNumber)){
for(int a=0;a<newData.length; a++){
out.write(newData[a]+"|");
}
continue;
}
out.write(str);
out.flush();
}
in.close();
out.close();
if(f.exists()){
if(f.delete()){
in=new BufferedReader(new InputStreamReader(new FileInputStream(temp)));
out=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f)));
while((str=in.readLine()) !=null){
out.write(str);
out.newLine();
out.flush();
}
in.close();
out.close();
}
}

}catch(IOException e){
e.printStackTrace();
}


}

public void run(){
}

}

/*
*the data file is like
A|X|B
and the header

Compagny|User|Role
*
*/

Thanx for your attention

Arman

Gregory A. Swarthout

unread,
Jul 31, 2001, 6:02:39 PM7/31/01
to
Answered in the other newsgroup you posted this to.
0 new messages