Prezados, carrego dados de um txt em um Vector chamado de DadosTXT, entretanto ele e ele se comporta como um vetor redimensionavel.
Em seguida, quero transformar meu Vector para Vector[] (matriz redimensionavel) para eu poder transforma-lo para Object[][] e adiciona-lo em um JTable.
Aí está o problema, já tentei diversas coisas em minhas classes, e nada.
Caso haja alguma outra solução para carregar o txt já como uma matriz redimensionável, por favor me mostre.
Meu vcetor está assim por exemplo: [1 2 3 4 5 6 7 8 9]
Meu vector[] tem que estar assim: [1 2 3] [4 5 6] [7 8 9]
Desde já agradeço.
Abaixo seguem as classes:
import java.io.*;
import java.util.*;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Fabricio Farias
*/
public class Tokenizer3{
public static Vector DadosTXT = new Vector();
public static double aux = 0;
private JLabel lArquivo;
private JFileChooser fc = new JFileChooser(".");
private JTextArea taConteudo;
private JButton bSalvar;
//contador
public static int cont = 0;
final static int coluna = 3;
public static int quebraLinha = 0;
//número de quebras de linhas
public static int[] vetor = new int[0];
private static Vector[] soma_meses = new Vector[0];
private static Vector[] verifica = new Vector[0];
public static void main(String args[]) throws FileNotFoundException, IOException
{
AbrirArquivo();
}
public static void AbrirArquivo() throws FileNotFoundException,IOException
{
File file = new File("C:\Documents and Settings\Fabricio Farias\Meus documentos\LIVRO\TESTANO\1999\janeiro.txt");// +
FileReader reader = new FileReader(file);
BufferedReader leitor = new BufferedReader(reader);
String linha;
StringTokenizer separador;
try{
do
{
linha = leitor.readLine();
if( linha != null )
{
separador = new StringTokenizer(linha, " ");
//número de colunas da matriz
for(int i = 0; i < coluna; i++)
{
DadosTXT.add(Double.parseDouble(separador.nextToken()));
}
quebraLinha++;
}
}while ( linha != null );
}catch(NullPointerException e){System.out.println("DEU PAU " + e);}
vetor = new int[quebraLinha];
//setando a primeira quebra de linha, na coluna 5.
//contando de 0 a 4
vetor[0] = 2;
//setando a posição das outras quebras de linha
for(int i = 1; i < quebraLinha; i++)
{
vetor[i] = coluna + vetor[i-1];
}
// for(int i = 0; i < quebraLinha; i++)
// System.out.println(vetor[i]);
String endereco = "C:\Documents and Settings\Fabricio Farias\Meus documentos\LIVRO\SALVATESTANO\janeiro1999"; //+
PrintWriter entTXT = new PrintWriter(new FileWriter(endereco));
GravarArquivo(endereco, DadosTXT);
entTXT.close();
leitor.close();
reader.close();
}
public static void GravarArquivo(String endereco, Vector result) throws IOException
{
try{
soma_meses = new Vector[coluna];
PrintWriter entradaTXT = new PrintWriter(new FileWriter(endereco+".txt"));
for(int i = 0; i < DadosTXT.size(); i++)
{
aux = (Double) result.elementAt(i);
//System.out.println(aux);
entradaTXT.print(aux + " ");
soma_meses[cont] = (Vector) result;//.elementAt(i);
if(i == vetor[cont])
{
entradaTXT.print("\r\n");
cont++;
}
}
CopyElement3.Copia(quebraLinha, coluna, soma_meses);
// for(int j = 0; j < 9; j++)
// {
//// System.out.print(" " + soma_meses[0].elementAt(j) );
// }System.out.println(" ");
// System.out.println(DadosTXT);
//Limpando as variáveis static
DadosTXT.clear();
aux = 0;
cont = 0;
quebraLinha = 0;
//vetor = new int[0];
//Fim do Limpando variáveis static
//JOptionPane.showMessageDialog(null,"Arquivo Salvo com Sucesso");
entradaTXT.close();
}catch(NullPointerException e){System.out.println(" ERRO ExemploTokenizer3" + e);
}catch(ArrayIndexOutOfBoundsException e){System.out.println(" PAU ExemploTokenizer3"+ e);
}catch(ClassCastException e){System.out.println(" ERRO ExemploTokenizer3"+ e);}
}}
import java.util.Vector;
import javax.swing.table.AbstractTableModel;
public class CopyElement3 extends AbstractTableModel{
private String[] colunas = {"Coluna 1","Coluna 2","Coluna 3"};
static Object[][] objArray = new Object[0][0];
public static void Copia(int quebraLinha, int coluna, Vector[] Meses)
{
for(int j = 0; j < quebraLinha; j++)
{
for(int i = 0; i < coluna; i++)
{
System.out.print(""+Meses[j].elementAt(i));
}
}
//declare an array to hold elements of Vector
objArray = new Object[quebraLinha][coluna];
/*
To copy all elements of java vector object into array use
void copyInTo(Ojbect[] obj) method. Here obj is an array into which
elements will get copied.
Please note that the array should be big enough to hold all elements of
java vector object. If not, ArrayIndexOutOfBoundException would be thrown.
*/
for(int i = 0; i < quebraLinha; i++)
Meses[i].toArray(objArray [i]);//copyInto(objArray);
//display contents of Object array
System.out.println("Vector elements are copied into an Array.Now Array Contains..");
for(int i = 0; i < quebraLinha; i++)
{
for(int j = 0; j < coluna ; j++)
{
System.out.print(objArray[i][j] + " ");
}
}
}
//métodos de implementação obrigatória
public int getColumnCount(){return colunas.length;}
public int getRowCount(){return objArray.length;}
public Object getValueAt(int lin, int col){return objArray[lin][col];}
//métodos de implementação opcional
public String getColumnName(int col){return colunas[col];}
public boolean isCellEditable(int lin, int col){return false;}
public Class getColumnClass(int col){return getValueAt(0, col).getClass();}
}
// public static void main(String[] args) {
// CopyElement3 cp = new CopyElement3();
//
//}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Fabricio Farias
*/
import java.util.Vector;
import javax.swing.*;
public class ChamaTabela extends JFrame{
private JTable tabela;
private static Vector Dados = new Vector();
//Construtor
public ChamaTabela(){
setTitle("Dados");
int Linha = 8;
int Coluna = 8;
Dados.add(1);
Dados.add(2);
Dados.add(3);
Dados.add(10);
Dados.add(20);
Dados.add(30);
Dados.add(100);
Dados.add(200);
Dados.add(300);
//Tabela.repasse(Linha,Coluna,Dados);
tabela = new JTable(new Tabela());
getContentPane().add(new JScrollPane(tabela), "Center");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400,150);
}
//main
public static void main(String args[])
{
new ChamaTabela().setVisible(true);
}
}