Questa è la classe dove ho il main
Mentre questa è la classe dove ho i metodicodice:package mySplitProva; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; import java.io.File; import javax.swing.JFileChooser; import javax.swing.filechooser.FileFilter; import java.io.*; public class SplitJFrame extends javax.swing.JFrame { /** Creates new form SplitJFrame */ public SplitJFrame() { initComponents(); jSplitPane1.setOneTouchExpandable(true); } @SuppressWarnings("unchecked") private void jFileReaderActionPerformed(java.awt.event.ActionEvent evt) { JFileChooser fileChooser = new JFileChooser(); fileChooser.setFileFilter(new TxtFileFilter()); int returnVal = fileChooser.showOpenDialog(this); if(returnVal == JFileChooser.APPROVE_OPTION){ File myFile = fileChooser.getSelectedFile(); FileImport obj1 = new FileImport(myFile); jTotalRowsTextField.setText(String.valueOf(obj1.numberOfLines())); System.out.println(obj1.checkIsFile()); System.out.println(obj1.numberOfLines()); obj1.importToArray(); obj1.printArray(); System.out.println("--------------------------------------"); obj1.buildDataArray(obj1.getArray()); obj1.printDataArray(); } } private class TxtFileFilter extends FileFilter{ public boolean accept(File file){ if(file.isDirectory()) return true; String fname = file.getName(); return fname.endsWith("txt"); } public String getDescription(){ return "txt file"; } } public static void main(String args[]) { try{ UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch(UnsupportedLookAndFeelException e){ //handle exception } catch(ClassNotFoundException e){ //handle exception } catch(InstantiationException e){ //handle exception } catch(IllegalAccessException e){ //handle exception } java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new SplitJFrame().setVisible(true); } }); }
Potrebbe essere una soluzione quella di abbinare un listener differente ad ogni RadioButton?codice:package mySplitProva; import java.io.*; //import java.util.StringTokenizer; import java.util.*; import java.text.SimpleDateFormat; public class FileImport { private File fileToImport; private Date[] dateArray; private double[][] dataArray; public FileImport(File myFile) { fileToImport = myFile; dateArray = new Date[numberOfLines()];//Array for date(calendar) dataArray = new double[numberOfLines()][6];//Array for double data }//constructor for fileToImport field int lines = 0; String[][]bin; public boolean checkIsFile(){ return fileToImport.isFile(); } public int numberOfLines(){ lines = 0; if(checkIsFile()){ try{ FileReader fr = new FileReader(fileToImport); BufferedReader br = new BufferedReader(fr); while((br.readLine()!=null)){ lines++; }//end while loop br.close(); }catch(Exception e){ System.out.println(e); } } else{ System.out.println("There is no file to import"); } return lines; }//returns number of lines in a txt file public void importToArray(){ int rows = 0; bin = new String[numberOfLines()][6]; try { FileReader fr = new FileReader(fileToImport); BufferedReader br = new BufferedReader(fr); String line = null; while((line = br.readLine())!= null){ StringTokenizer stk = new StringTokenizer(line, ","); while(stk.hasMoreTokens()){ for (int cls = 0;cls<6; cls++){ bin[rows][cls]= stk.nextToken(); } rows++; }//end inner while loop }//end outer while loop br.close(); }//end try catch(Exception e){ System.out.println(e); } }//import data to bin array public void printArray(){ for(int i =0;i<bin.length; i++){ System.out.printf("%s ", i); for(int j =0;j<bin[i].length; j++){ System.out.printf("%s ", bin[i][j]); } System.out.println(""); }//end for loop }//print contents of bin array public String[][] getArray(){ return bin; }//return bin array public void buildDateArray(String[][]d) { SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");//set date format here; for(int i=0;i<d.length; i++){ for(int j = 0;j<d[i].length; j++){ if(j==0){ try{ Date newDate = (Date)sdf.parse(d[i][0]);//parse first column to Date dateArray[i] = newDate; }//end try catch(Exception e){ System.out.println(e); }//end catch } } }//end for loops } public void buildDataArray(String[][]d){ for(int i=0;i<d.length;i++){ for(int j=0;j<d[i].length; j++){ switch(j){ case 0: dataArray[i][j]=0; break; case 1: dataArray[i][j]=new Double(d[i][j]); break; case 2: dataArray[i][j]=new Double(d[i][j]); break; case 3: dataArray[i][j]=new Double(d[i][j]); break; case 4: dataArray[i][j]=new Double(d[i][j]); break; case 5: dataArray[i][j]=new Double(d[i][j]); break; }//end switch } }//end for loops } public void printDataArray(){ for(int i=0;i<dataArray.length;i++){ for(int j=0;j<dataArray[i].length;j++){ System.out.printf("%s ", dataArray[i][j]); } System.out.println(""); } } public void printDateArray(){ for(int i=0;i<dateArray.length;i++){ System.out.println(dateArray[i]); } } }
Grazie
Susanna
PS spero che l'indentazione sia venuta correttamente questa volta...
PS2 evidentemente sbaglio qualcosa perchè nonostante uso CODE per inserire il codice e chiudo il tag, mi stampa tutto su una riga.

Rispondi quotando