Results 1 to 10 of 10
Thread: Automatically set the path
- 01-26-2010, 03:55 AM #1
Member
- Join Date
- Sep 2009
- Posts
- 44
- Rep Power
- 0
Automatically set the path
I am creating a GUI using Netbeans 6.7.1 and this GUI contains a TextFields, JButtons.
all i want to do is.. when i put a drive in TextField for example i put drive "c:\" in the TextField and when i click the JButton is automatically the path is in the drive "c:\" also.
is it possible?
Thanks in advance
-
- 01-26-2010, 04:05 AM #3
Member
- Join Date
- Sep 2009
- Posts
- 44
- Rep Power
- 0
Oh..! sorry for not clearing my Explanation...
my gui contains TextFields and JButtons right? and the plow of my program is to browse files
and to browse this i use JFileChooser and i implement this with my JButtons... i want to know is? is it possible when i put "C:\" in my TextField and then, when i click my JButton the JFileChooser is set into Drive "C:\" also because i put in "C:\" in my TextField..
-
Yes, it's possible. First of all, you are using JTextFields and not TextFields, right?
Next of all, look up the Sun tutorial on JFileChooser and it will show you how to take use a directory to initialize the file chooser: How to use File Choosers
Much luck!
- 01-26-2010, 04:35 AM #5
Member
- Join Date
- Sep 2009
- Posts
- 44
- Rep Power
- 0
Here's my code and i didn't got an error on this but the problem is i didn't make it my question on this forum about the JTexField... it still not working :-(
although they can browse files but when i put the path in the JTextField the path is not set to specific path which i put into JTextField,
Java Code:private void BrowseS1ActionPerformed(java.awt.event.ActionEvent evt) { JFileChooser JFileChooseS1 = new JFileChooser(); JFileChooseS1.setMultiSelectionEnabled(true); JFileChooseS1.setFileSelectionMode(JFileChooser.FILES_ONLY); int result = JFileChooseS1.showDialog(this,"Open/Save"); if(result == JFileChooser.APPROVE_OPTION){ File S1 = JFileChooseS1.getSelectedFile(); TxtFldS1.setText(S1.toString()); } } private void BrowseS2ActionPerformed(java.awt.event.ActionEvent evt) { JFileChooser JFileChooseS2 = new JFileChooser(); JFileChooseS2.setMultiSelectionEnabled(true); JFileChooseS2.setFileSelectionMode(JFileChooser.FILES_ONLY); int result = JFileChooseS2.showDialog(this,"Open/Save"); if(result == JFileChooser.APPROVE_OPTION){ File S2 = JFileChooseS2.getSelectedFile(); TxtFldS2.setText(S2.toString()); } }
-
Did you read the tutorial that I linked to above? As I mentioned in my post above, it tells you how to set the directory. To quote:
By default, a file chooser that has not been shown before displays all files in the user's home directory. You can specify the file chooser's initial directory by using one of JFileChooser's other constructors, or you can set the directory with the setCurrentDirectory method.
- 01-26-2010, 05:06 AM #7
Member
- Join Date
- Sep 2009
- Posts
- 44
- Rep Power
- 0
yes i already read it. and i found another example
here's the code:
Java Code:private void BrowseS1ActionPerformed(java.awt.event.ActionEvent evt) { JFileChooser JFileChooseS1 = new JFileChooser(new File("C:\\")); JFileChooseS1.setMultiSelectionEnabled(true); JFileChooseS1.setFileSelectionMode(JFileChooser.FILES_ONLY); int result = JFileChooseS1.showDialog(this,"Open/Save"); if(result == JFileChooser.APPROVE_OPTION){ File S1 = JFileChooseS1.getSelectedFile(); TxtFldS1.setText(S1.toString()); } }
-
Of course because I don't see you getting the text from the JTextField and using it in the JFileChooser constructor anywhere, but instead are using the String literal "C:\\". Have you tried to use the JTextField text in your code but not posted this attempt here yet? If so, let's see this attempt, otherwise we can't tell what you're doing wrong.
- 01-26-2010, 06:33 AM #9
Member
- Join Date
- Sep 2009
- Posts
- 44
- Rep Power
- 0
Thanks. Finally i got your point...
Thanks Fubarable.
- 01-26-2010, 09:18 AM #10
Also, not to step on anyone's toes, but if you are using the netbeans gui builder, then default text for a field can be set by double clicking on the text field (slowly) in the gui builder view and simply typing the default text in. As far as setting text with a button click, I think fubarable has it covered in what he said -- you can always just use the setText() method for the textfield you are trying to set.
Similar Threads
-
Automatically Generate an XML file
By svpriyan in forum XMLReplies: 2Last Post: 06-25-2009, 06:46 AM -
ActionListener run automatically problem
By cassysumandak in forum New To JavaReplies: 1Last Post: 03-23-2009, 10:42 PM -
Application automatically terminates
By PaulT in forum EclipseReplies: 4Last Post: 03-18-2009, 06:02 PM -
Automatically add method to all classes in package
By skaspersen in forum EclipseReplies: 2Last Post: 07-04-2008, 01:33 PM -
Call a Method Automatically
By rhm54 in forum New To JavaReplies: 4Last Post: 02-07-2008, 09:51 AM
Bookmarks