943,603 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1630
  • Java RSS
Jul 4th, 2009
0

open local folder with a jButton

Expand Post »
Good evening,

I am trying to simply open a folder located on my local C:\ drive, to be exact just for testing purposes I am trying to open C:\temp folder.
What I have is a JFrame with a single Jbutton and some code, however when I execute the button, nothing absolutely nothing happens, it doesn't crash, it doesn't open a folder, it doesn't give me a message error.

line 65 to 85 is where my code resides.
under the "private void jButton1ActionPerformed" I call a "OpenMain();" method which I define right under that starting on line 69.

I would definitly appreciate some help,

John

My code:
java Syntax (Toggle Plain Text)
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. /*
  7.  * NewJFrame.java
  8.  *
  9.  * Created on Jul 4, 2009, 6:57:23 PM
  10.  */
  11.  
  12. package systems;
  13.  
  14. /**
  15.  *
  16.  * @author olivier
  17.  */
  18. public class NewJFrame extends javax.swing.JFrame {
  19.  
  20. /** Creates new form NewJFrame */
  21. public NewJFrame() {
  22. initComponents();
  23. }
  24.  
  25. /** This method is called from within the constructor to
  26.   * initialize the form.
  27.   * WARNING: Do NOT modify this code. The content of this method is
  28.   * always regenerated by the Form Editor.
  29.   */
  30. @SuppressWarnings("unchecked")
  31. // <editor-fold defaultstate="collapsed" desc="Generated Code">
  32. private void initComponents() {
  33.  
  34. jButton1 = new javax.swing.JButton();
  35.  
  36. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  37.  
  38. jButton1.setText("jButton1");
  39. jButton1.addActionListener(new java.awt.event.ActionListener() {
  40. public void actionPerformed(java.awt.event.ActionEvent evt) {
  41. jButton1ActionPerformed(evt);
  42. }
  43. });
  44.  
  45. org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
  46. getContentPane().setLayout(layout);
  47. layout.setHorizontalGroup(
  48. layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
  49. .add(layout.createSequentialGroup()
  50. .add(154, 154, 154)
  51. .add(jButton1)
  52. .addContainerGap(173, Short.MAX_VALUE))
  53. );
  54. layout.setVerticalGroup(
  55. layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
  56. .add(layout.createSequentialGroup()
  57. .add(86, 86, 86)
  58. .add(jButton1)
  59. .addContainerGap(191, Short.MAX_VALUE))
  60. );
  61.  
  62. pack();
  63. }// </editor-fold>
  64.  
  65. private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
  66. OpenMain();
  67. }
  68.  
  69. private void OpenMain()
  70. { try
  71. {
  72. //Path for folder or file that you want to open
  73. //In my case i will open C:\ drive
  74. //So create a String like below
  75. //Don't forget to change \ to \\
  76. String a="C:\\temp";
  77.  
  78. //Execute command
  79. //Command that you must know : rundll32 SHELL32.DLL,ShellExec_RunDLL File_Or_Folder_To_Open
  80. Runtime.getRuntime().exec("rundll32 SHELL32.DLL,ShellExec_RunDLL \""+a+"\"");
  81. }
  82. catch(Exception exception)
  83. {
  84. exception.printStackTrace();
  85. } }
  86.  
  87.  
  88. public static void main(String args[]) {
  89. java.awt.EventQueue.invokeLater(new Runnable() {
  90. public void run() {
  91. new NewJFrame().setVisible(true);
  92. }
  93. });
  94. }
  95.  
  96. // Variables declaration - do not modify
  97. private javax.swing.JButton jButton1;
  98. // End of variables declaration
  99.  
  100. }
Similar Threads
Reputation Points: 26
Solved Threads: 0
Junior Poster in Training
JohnPhilipps is offline Offline
65 posts
since Jun 2009
Jul 5th, 2009
0

Re: open local folder with a jButton

I suggest taking a look at the JFileViewer class.
Reputation Points: 41
Solved Threads: 13
Posting Whiz in Training
llemes4011 is offline Offline
224 posts
since Aug 2008
Jul 5th, 2009
0

Re: open local folder with a jButton

Use following code
Java Syntax (Toggle Plain Text)
  1. String a="C:\\csnet";
  2. Runtime.getRuntime().exec("cmd /c start "+a);
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Jul 5th, 2009
0

Re: open local folder with a jButton

Thank you so much, it works great.
Reputation Points: 26
Solved Threads: 0
Junior Poster in Training
JohnPhilipps is offline Offline
65 posts
since Jun 2009
Sep 15th, 2010
0
Re: open local folder with a jButton
Here is another way to open folder or directory
Java Syntax (Toggle Plain Text)
  1. String path = "C:\\"; // path to the directory to be opened
  2. File file = new File(path);
  3. Desktop desktop = Desktop.getDesktop();
  4. try {
  5. desktop.open(file);
  6. } catch (IOException e){ }
to use Desktop you must import java.awt.Desktop.

This method can be little-bit complex. But it works well. You can use any.
Last edited by ankurgecr; Sep 15th, 2010 at 3:22 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ankurgecr is offline Offline
5 posts
since Sep 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Drawing a centered rectangle.
Next Thread in Java Forum Timeline: problem in opening a file with application using java





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC