Following my previous post in this link I have another problem .
Given the following code :
public class GuiHandler extends javax.swing.JFrame {
// GuiHandler is the class that runs the entire project
// here are two private fields that I use in the code , I have more but they
// irrelevant for the moment
final JFileChooser openFiles = new JFileChooser();
private DataParser xmlParser = new DataParser();
// later on I have this method - XMLfilesBrowserActionPerformed
private void XMLfilesBrowserActionPerformed(java.awt.event.ActionEvent evt) {
//setting the file chooser
openFiles.setFileSelectionMode(JFileChooser.FILES_ONLY);
openFiles.setAcceptAllFileFilterUsed(false);
if (openFiles.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
//getting the selected file
File selected = openFiles.getSelectedFile();
// from here I parse the XML file that I opened with JFileChooser
// with the help of one of my methods getNodeListFromFile
xmlParser.getNodeListFromFile(selected);
The problem is that I cannot use the declaration of main
that was suggested before in the link above (and I must add that was a pretty nice one:)), the code that was posted is :
public class NativeFileChooser {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
} catch(Exception e) {
e.printStackTrace();
}
JFileChooser jfc = new JFileChooser();
jfc.showOpenDialog(null);
}
});
}
}
And the use of main
makes it very difficult for manipulating the XML
file later on ,with my
method getNodeListFromFile
.
What I need is to use the "simple" browser of JFileChooser
, and then use the chosen file , without involving the main
into that .
I'd appreciate if someone can explain how can I use the above code (or anything else) with my code .
Best regards
EDIT:
If I use the code like this ;
public void launchFileChooser() {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
} catch(Exception e) {
e.printStackTrace();
}
JFileChooser jfc = new JFileChooser();
jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
jfc.setAcceptAllFileFilterUsed(false);
if (jfc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
newFile = jfc.getSelectedFile();
}
});
}
here newFile
is a data member .
then after opening the file , the code crashes .
If I make jfc
a data member , the code opens the regular browser . Any idea how to fix it ?
thanks