I'm making a little program that when a jButton is clicked, it opens the downloads folder from the file explorer. I have the button set up, I'm just having troubles getting the folder open.
Here is my code.
private void downloadsActionPerformed(java.awt.event.ActionEvent evt) {
String download = System.getenv("downloads");
File downloadsDir = new File(download);
try {
Desktop.getDesktop().open(downloadsDir);
} catch (IOException ex) {
Logger.getLogger(GettingFile.class.getName()).log(Level.SEVERE, null, ex);
}
}
Related
I am trying to create a UI and run a batch file that will restart a service running in the background just by clicking a menu item. I am able to make the menu and add the menu item just like so:
JMenu menu = new("menu");
JMenuItem restart_service= new JMenuItem("Restart service");
menu .add(restart_service);
Then, I added a listener to the menu item to run the batch file:
restart_service.addActionListener (new ActionListener(){
#Override
public void actionPerformed(ActionEvent ae) {
openWebPage(
"file://path to bat file/batchfile.bat");
}
public void openWebPage(String url) {
try {
java.awt.Desktop.getDesktop().browse(java.net.URI.create(url));
} catch (java.io.IOException e) {
System.out.println(e.getMessage());
}
}
});
However, everytime I try this, the cmd window pops up and prints "Access Denied". Although i changed the premission for the file to run without being admin. Not sure how to fix this or if there is a way to excute batch files as an admin by clciking a menu item. Any help would be appreciated.
Try this.
try{
Process mp = Runtime.getRuntime().exec("BAT LOC");
mp.waitFor();
}catch( Exception procRunException ){
}
Desktop desktop = Desktop.getDesktop();
File sitetxt = new File(System.getProperty("user.dir") + File.separator + "site.txt");
MenuItem settings = new MenuItem("Settings");
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
desktop.open(sitetxt);
} catch (IOException e1) {
System.out.println("site.txt not found.");
e1.printStackTrace();
}
}
});
popup.add(settings);
trayIcon.setPopupMenu(popup);
I'm trying to open a text file when the user clicks "Settings" from the right click menu of the system tray icon. The default text editor is set to Notepad. There is another menu item called "Exit" but it works fine.
My JFileChooser works fine without the setLookAndFeel added, and it works with it added too, but doesn't automatically become the active window. However, if I make it the active window, then close the window, then press the button that opens JFileChooser again, it becomes the active window. Here is my relevant code:
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
...
JFileChooser chooser = new JFileChooser();
chooser.setVisible(true);
chooser.requestFocus(true);
chooser.setCurrentDirectory(new File("/home/me/Documents"));
int retrieval = chooser.showSaveDialog(chooser);
Thanks in advance
How to get the text in a Text Area to be saved to a text file when a button is clicked?
I have implemented the GUI and when the btnContinue button is clicked I want the content of txtOrder to be saved to a text file in notepad.
I'm not a pro programmer in Java so pls correct me if I'm wrong, but I would try this:
FileWriter fw = null;
try
{
fw = new FileWriter("Output.txt",true);
fw.write(txtOrder.getText())
//textArea.write(fw);
}
catch (IOException ex)
{
ex.printStackTrace();
}
finally
{
if (fw!=null)
{
fw.close();
}
}
I try to create a button that copy in the clipboard a String and then user can paste it.
I can do it when I do my test in local, but when I deploy the application on a server, nothing is saved in clipboard after an user click on button.
Here's my code :
public void clipboard() {
try {
StringSelection stringSelection = new StringSelection("test");
Clipboard clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard();
clpbrd.setContents(stringSelection, null);
} catch (Throwable e) {
e.printStackTrace();
}
}
How can I do it ?