open local folder with a jButton

Thread Solved
Reply

Join Date: Jun 2009
Posts: 27
Reputation: JohnPhilipps is an unknown quantity at this point 
Solved Threads: 0
JohnPhilipps JohnPhilipps is offline Offline
Light Poster

open local folder with a jButton

 
0
  #1
Jul 4th, 2009
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:
  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. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 196
Reputation: llemes4011 is an unknown quantity at this point 
Solved Threads: 13
llemes4011 llemes4011 is offline Offline
Junior Poster

Re: open local folder with a jButton

 
0
  #2
Jul 5th, 2009
I suggest taking a look at the JFileViewer class.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,421
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 433
adatapost's Avatar
adatapost adatapost is offline Offline
Nearly a Posting Maven

Re: open local folder with a jButton

 
0
  #3
Jul 5th, 2009
Use following code
  1. String a="C:\\csnet";
  2. Runtime.getRuntime().exec("cmd /c start "+a);
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 27
Reputation: JohnPhilipps is an unknown quantity at this point 
Solved Threads: 0
JohnPhilipps JohnPhilipps is offline Offline
Light Poster

Re: open local folder with a jButton

 
0
  #4
Jul 5th, 2009
Thank you so much, it works great.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC