I have 4 jButtons that execute each specific program (Browsers for the time being), I need to make a conditional that tells a user that if a path to that specific program doesn't exist it will open a webpage with that specific browser in mind.
This piece of code repeats itself and I need to optimise it. I tried making another method where I would use if statement but instead it just gave me a lot of issues while executing each button.
Warning_MSG(); is just another method with jOptionPane to confirm if a user wants to open an external application, etc etc.
private void chromeMouseClicked(java.awt.event.MouseEvent evt) {
File file = new File("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");
if(!file.exists()){
try {
URI url = new URI("https://google.com/chrome");
java.awt.Desktop.getDesktop().browse(url);
System.err.println("File Error, Google Chrome is not found!\n");
} catch (IOException | URISyntaxException e) {
System.err.println("Unknown Error, Exception found\n" + e);
}
}else {
Warning_MSG(file);
}
}
private void firefoxMouseClicked(java.awt.event.MouseEvent evt) {
File file = new File("C:\\Program Files\\Mozilla Firefox\\firefox.exe");
if(!file.exists()){
try {
System.err.println("File Error, Mozilla Firefox is not found!\n");
URI url = new URI("https://www.mozilla.org/en-GB/firefox/download/thanks/");
java.awt.Desktop.getDesktop().browse(url);
} catch (IOException | URISyntaxException e) {
System.err.println("Unknown Error, Exception found\n" + e);
}
}else {
Warning_MSG(file);
}
}
private void ieMouseClicked(java.awt.event.MouseEvent evt) {
File file = new File("C:\\Program Files (x86)\\Internet Explorer\\iexplore.exe");
if(!file.exists()){
System.err.println("File Error, iExplorer is not found! \n");
}else {
Warning_MSG(file);
}
}
private void edgeMouseClicked(java.awt.event.MouseEvent evt) {
File file = new File("C:\\Windows\\SystemApps\\Microsoft.MicrosoftEdge_8wekyb3d8bbwe\\MicrosoftEdge.exe");
if(!file.exists()){
System.err.println("File Error, Microsoft Edge is not Found! \n");
}else {
Warning_MSG(file);
}
}
The question is, how can I optimise these pieces of code so that I don't have to repeat the same thing all over again with other programs but instead run certain things only when conditions are met?
Aucun commentaire:
Enregistrer un commentaire