I have this button, which opens the file directory on the computer and from where you can choose the file and execute the commands. I have each of them in a class, but only one of them is running. How can I make it execute first one class and then the other? The codes is at follows:
JButton btnFindMe = new JButton("Find theta");
btnFindMe.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser fctheta = new JFileChooser();
fctheta.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int returnVal = fctheta.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
final File filetheta = fctheta.getSelectedFile();
//counts the thetas
class Inputcontatheta {
public int main (String [] arg) throws Exception{
BufferedReader CSVFile = new BufferedReader (new FileReader (filetheta));
String dataRow = CSVFile.readLine();
int ii;
ii=0;
while (dataRow != null && !dataRow.isEmpty()){
dataRow = CSVFile.readLine ();
ii++;
}
CSVFile.close ();
return ii;
}
}
// exectues gaussians
class Inputtheta {
public void main (String [] arg) throws Exception{
Inputcontatheta dimensao= new Inputcontatheta();
int x=dimensao.main(arg);
BufferedReader CSVFile = new BufferedReader (new FileReader (filetheta));
String dataRow = CSVFile.readLine();
double [][] gaussiana = new double [x][6];
int j;
j=0;
while (dataRow != null && !dataRow.isEmpty()){
String [] dataArray = dataRow.split (",");
double[] doubleArray =new double[dataArray.length];
int i=0;
while (i< dataArray.length ) {
doubleArray[i]= Double.parseDouble(dataArray[i]);
i++;
}
gaussiana[j][0]=1.0;
gaussiana[j][1]=0.25;
gaussiana[j][2]=doubleArray[0];
gaussiana[j][3]=-doubleArray[0];
gaussiana[j][4]=doubleArray[1];
gaussiana[j][5]=doubleArray[2];
j++;
System.out.println(Arrays.toString(doubleArray));
dataRow = CSVFile.readLine();
}
CSVFile.close ();
System.out.println(gaussiana[1][5]);
//return gaussiana [][] double;
}}
}
}});
Try transforming your Inputcontatheta
and Inputtheta
classes into methods of the ActionListener
like so:
btnFindMe.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser fctheta = new JFileChooser();
fctheta.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int returnVal = fctheta.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File fileTheta = fctheta.getSelectedFile();
int x = countThetas(fileTheta);
executeGaussians(fileTheta, x);
}
}
private int countThetas(File fileTheta) {
// stuff from Inputcontatheta here
}
private void executeGaussians(File fileTheta, int x) {
// stuff from Inputtheta here
}
});
That being said, maybe instead of just counting the number of rows in your csv file the countThetas
method could actually parse the csv file into a double[][]
array and you could pass that array to executeGaussians
. (You can use java.util.ArrayList
if you don't know the length of your data beforehand.)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.