Reputation: 10784
i doing a program to calculate the critical path method from a DAG, program logic is perfect, but I have a problem trying to integrate a graphical user interface. The interface lets me through a JFileChooser to choose the input file for the program, but I do not know how to pass that parameter to the function "readfile", located in the main action.
Herés the code of the void main:
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
JFrameConFondo jf = new EjemploJFrameConFondo();
jf.setLocationRelativeTo(null);
jf.setTitle("CPM");
jf.setVisible(true);
readfile(route);
////I need to pass a filename to the program which calculate the critical path,
}
});
}
}
here's the code of the function "readfile":
public static void leer_archivo(String fileName){
try{
File archivo=new File(fileName);
FileReader fr= new FileReader(archivo);
BufferedReader br= new BufferedReader(fr);
String linea;
linea=br.readLine();
c=Integer.parseInt(linea);
for(int i=0;i<c;i++){
CrearCaso(br, i+1);
}
}catch(Exception e){
}
}
I'm doing this action for the button on the interface for the file selection, and I want the name of that file in some way can be sent to void main to be able to use the function "readfile":
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
JFileChooser filechooser = new JFileChooser();
int option = filechooser.showOpenDialog (this);
if (option==JFileChooser.APPROVE_OPTION){
cajaTexto.setText(filechooser.getSelectedFile().getPath());
}
I hope someone can give me a hand with this, I'm stuck with this for a few days and I'm really new in the world Java. I did not put the whole code because there are several classes and several lines of code.
Upvotes: 1
Views: 143
Reputation:
Ok, let me check if I understand your question properly...
You have a function readfile(String filename)
that you want to call inside your main
function.
You can get the path to the file using filechooser.getSelectedFile().getPath()
.
In this case you can export the selected path into a static variable of a public class.
So make a new class:
public class Globals
{
private static String FilePath;
public static String GetFilePath()
{
return FilePath;
}
public static void SetFilePath(String NewPath)
{
FilePath = NewPath;
}
}
Set it with button using:
Globals.SetFilePath(filechooser.getSelectedFile().getPath());
And then use it so:
readfile(Globals.GetFilePath());
Notes*
This isn't necessarily the best solution available. It involves least changes to already exising source code though.
Also, please excuse my Pascal notation, habit from C#.
Upvotes: 1
Reputation: 13057
You need to change your code in two places. I assume, that the button action is actually working. I assume, that your "main" class is called "MyProgram".
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
JFileChooser filechooser = new JFileChooser();
int option = filechooser.showOpenDialog (this);
if (option==JFileChooser.APPROVE_OPTION){
String filename = filechooser.getSelectedFile().getPath();
cajaTexto.setText(filename);
// call main():
MyProgram.main(new String[] { filename });
}
public static void main(String args[]) {
// assign file name
final String route = args[0];
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
JFrameConFondo jf = new EjemploJFrameConFondo();
jf.setLocationRelativeTo(null);
jf.setTitle("CPM");
jf.setVisible(true);
readfile(route);
}
});
}
}
Upvotes: 1