Reputation: 3
Good day to all.
I have a problem regarding JFileChooser's showSaveDialog.
This segment of code is used for exporting CSV files and converting it into a (.XLS) file. Here's a segment of my code:
During execution, whenever I click ExportButton, open the JFileChooser, I am able to choose on what directory I may save my converted file. Once selected, the JTextField beside the JFileChooser gets the desired location where my file would be saved.
EDIT: Code is fixed already! Thanks for helping me! :)
else if(e.getSource() == ExportButton){
JPanel panel = new JPanel(new GridLayout(0, 2));
fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
selection5 = new JTextField(20);
selection5.setDocument(new JTextFieldLimit(200));
selection5.setText("");
panel.add(SaveButtonCSVFile);
panel.add(selection5);
boolean panelChecker = false;
while(panelChecker == false){
int result = JOptionPane.showConfirmDialog(null, panel, "Quesion",JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
switch (result) {
case JOptionPane.OK_OPTION:
if(selection5.getText().trim().length() == 0){
JOptionPane.showMessageDialog(this, "Please input valid filename");
selection5.setText("");
}
else{
if(selection5.getText().trim().contains(".xls")){
panelChecker = true;
exportFilename = selection5.getText().trim();
....(other code)
exportFilename = null;
selection5.setText("");
}
else{
JOptionPane.showMessageDialog(this, "Please append .xls to your filename");
}
}
break;
case JOptionPane.CANCEL_OPTION:
panelChecker = true;
break;
}
}
}
else if (e.getSource() == SaveButtonCSVFile) {
int returnVal = fc.showSaveDialog(DataUI.this);
String temp = selection5.getText().trim();
if (returnVal == JFileChooser.APPROVE_OPTION) {
selection5.setText("");
File file = fc.getSelectedFile();
String suffix = ".xls";
if(file.getAbsolutePath().contains(".xls")){
suffix = "";
}
selection5.setText(file.getAbsolutePath() + suffix);
} else {
selection5.setText(temp);
}
}
My problem is, if i click again the ExportButton to export my data, it opens the JFileChooser again, and repeating this process, I was able to understand the pattern it makes:
1st click of Export result to 1 time open of JFileChooser, 2nd click of Export results to 2 times open of JFileChooser, 3rd click of Export results to3 times open of JFileChooser, and so on.
How can I make the JFileChooser to open only once?
Thank you in advance :)
Upvotes: 0
Views: 119
Reputation: 347334
You're adding the ActionListener
this
to the SaveButtonCSVFile
each time you press the ExportButton
SaveButtonCSVFile.addActionListener(this);
So, the first time your press the ExportButton
, SaveButtonCSVFile
will have one ActionListener
, the second time, it will have two, then three ... and soon on...
This is one of the reasons I prefer to create a custom inner class with all the components I need already added to it. Creating a new instance as required (or maintaining a single reference which can be re-used)
Upvotes: 1