text in textfield - java

After selecting file using browse button into textfield and selecting checkbox that checkbox radio buttons and run should be enabled.How can i do this in swing using java code?

Lets start with selecting a file in Swing:
JFileChooser chooser = new JFileChooser();
int choice = chooser.showSaveDialog(MainFrame.this);
if (choice != JFileChooser.APPROVE_OPTION)
return;
File selectedFile = chooser.getSelectedFile();
That block of code will open a File dialog and wait for a user to select a file. Then the selected file will be stored into selectedFile.
You need to better clarify what you are asking for if you need more help.

JFileChooser, suggested by Justin Nelson, has an addActionListener() method. By implementing the ActionListener interface, your actionPerformed() method will be called to signal various user actions. You can enable buttons and set fields there. The tutorial How to Use File Choosers shows several examples.

Related

jFileChooser save and cancel buttons are changed (Save will cancel and Cancel will Save) Java

I'm making a simple Download manager in java.
When running the code in mac it works fine, but i went to test it in windows 7, and i got this problem:
If I click in save, it will leave the path to C:\Users\Gui
If I click to cancel, it will change it to C:\Users\Gui\Documents
Code that i'm using:
JFileChooser f = new JFileChooser();
f.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
f.showSaveDialog(null);
tfPath.setText(f.getCurrentDirectory().toString());
Anyone know a solution to this problem?
default directory of JFileChooser is user/documents for example
for me its
C:\Users\Adeel Ahmad\Documents
for you it will be your C:\Users\your_login_account_name\Documents
use setcurrent directory to set the default directory when jfilechooser is opened
f.setCurrentDirectory("C:\somwhere");
Your code calls f.getCurrentDirectory() , which will give you the parent directory of whichever directory you're in when selecting a directory in JFileChooser.DIRECTORIES_ONLY mode.
you might wanna use f.getSelectedFile() this is the reason you are getting C:\Users\Gui\Documents because when you save it,it gets its parent directory
and you can also know user selected save or cancel, JFileChooser.showSaveDialog returns an int which you can compare with constants of JFileChooser
you can take any action on the base of if user selected save or cancel by using
JFileChooser.CANCEL_OPTION or JFileChooser.APPROVE_OPTION(meaning save option)
JFileChooser f = new JFileChooser();
f.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
f.setCurrentDirectory(new File("D:\\"));
int result=f.showSaveDialog(null);
if(result==JFileChooser.APPROVE_OPTION)
tfPath.setText(f.getCurrentDirectory().toString());

Put an 'ok' or 'open' button in JFileChooser

I have used the JFileChooser option but by default, it shows save and cancel button. I want to put an OK button instead of save button. How do I do that?
Maybe this one help:
JFileChooser j = new JFileChooser();
j.showDialog(this, "ok");
When using one of the showDialog()-variants you can use
showDialog(Component parent, String approveButtonText)
with an approveButtonText of your choice.
If you instanciate your own JFileChooser use
setApproveButtonText(String approveButtonText)
to change the button's text.

JFileChooser.showSaveDialog: All files greyed out

I'm trying to use the JFileChooser to get files for loading and saving. The dialog that comes up with openFileDialog() works fine, but when I use the saveFileDialog() method, the dialog window has all the file names greyed out. This happens with or without a FileFilter (my example includes one to better show what I'm seeing).
Here's a minimal program to illustrate:
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.filechooser.FileNameExtensionFilter;
public class Temp extends JFrame {
public static void main(String[] args){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("Text File", "txt");
chooser.setFileFilter(filter);
frame.setVisible(true);
chooser.showOpenDialog(null);
chooser.showSaveDialog(null);
}
}
Here's what I see in the Open dialog:
Open Dialog
Here's what I see in the Save dialog:
Save Dialog
Despite being greyed out, all the files in the save dialog are selectable.
I'm on Mac/Mountain Lion and Java 7 if it matters.
Is this expected behavior? Is there a way to change this?
(Edit: per comments by MadProgrammer + trashgod below, this appears to be consistent with the look + feel of other (native) Mac apps)
I'm looking for the .txt files to be displayed in the "normal" color while in the save dialog.
That's controlled by the FileChooserUI delegate specific to a particular Look & Feel, e.g. AquaFileChooserUI on Mac OS X. You can use a different L&F, (laboriously) write your own FileChooserUI, or develop a custom File Browser GUI.
What I ended up doing was to use:
JFileChooser chooser = new JFileChooser(...);
chooser.showDialog(myFrame, "Save");
My save dialog looks like a save dialog, and the FileFilter greys out only files that fail its test.
Mmm... I think, that show dialogs the way you do is not the best way
chooser.showOpenDialog(null);
chooser.showSaveDialog(null);
I think that could be generating a conflict. Why don't you try to use a JFrame to help you? Try with this piece of code, just to know if the problem is the saveDialog. Myabe then you can adapt it to your programming requirements.
JFrame parentFrame = new JFrame();
JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle("Specify a file to save");
int userSelection = fileChooser.showSaveDialog(parentFrame);
if (userSelection == JFileChooser.APPROVE_OPTION) {
File fileToSave = fileChooser.getSelectedFile();
System.out.println("Save as file: " + fileToSave.getAbsolutePath());
}
As a matter of fact, you could try using the setLookAndFeel, I remember I had this issue working with my Macbook Pro.

Creating a file from a jFileChooser component

I am using JFileChooser as part of an export feature. I would like for the user to be able to either select a file from JFileChooser's file viewer or enter the name of a file in the filename text box. From what I've read it's possible to get that value using the getSelectedFile() method, so I have some listeners that call getSelectedFile() and attempt to do some checks before executing the export.
The problem I'm encountering is that the getSelectedFile() method is returning null when I enter the name into the filename text box manually. To add more confusion, the getSelectedFile() method does work in three different situations:
I populate it via setSelectedFile() (a user has clicked a value from a table and I use setSelectedFile())
I click an existing file in the file viewer
I hit ENTER after populating the filename text box
I have three file filters but have had the same behavior regardless of if they are enabled or not.
Listeners that call getSelectedFile() are as follows:
Event Listener for keyReleased
Event Listener for mousePressed.
PropertyChangeEvent listener on my jFileChooser
Action Listener on my jFileChooser
Is there a better way to listen to my jFileChooser to get the user input? I feel like I'm missing something very obvious ... any help is appreciated!
edit
A little more info ...
I have a JFileChooser component in a JSplitPane, which is in a JFrame. I'm not calling showOpenDialog to get input from the user. The component is accessible as part of the form.
What I would like to do is listen to the user input as he/she types. I have a 'start export' button that I would like to leave disabled until the user has entered a valid filename in the filename textbox in the JFileChooser component. To accomplish this I have a KeyEvent listener that I would like to use to get the filename as the user types it in.
further edit
Here is the action listener code:
jFileChooserExport.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jFileChooserExportActionPerformed(evt);
}
});
I also have a property change listener here:
jFileChooserExport.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
public void propertyChange(java.beans.PropertyChangeEvent evt) {
jFileChooserExportPropertyChange(evt);
}
});
Inside of both jFileChooserExportPropertyChange and jFileChooserExportActionPerformed I am trying to get the file the user has selected by invoking getSelectedFile(). In both cases, however, it remains null until the user does one of the three methods described above
Read the section from the Swing tutorial on How to Use File Choosers. The demo code there works fine for me.
Since none of below seems to work, you might want to try to add a PropertyChangeListener to your JFileChooser, listening for the SELECTED_FILE_CHANGED_PROPERTY
What might be possibly happening is that your file chooser may have multi selection enabled, in which case getSelectedFile will return null, but getSelectedFiles will return an array containing the selected file(s). You may either want to disable multi selection, or use the array (If you want the user to only select one file, set multiSelectionEnabled to false).
Another possibility, though, is if you try to get the selected file but fileChooser.showOpenDialog or fileChooser.showSaveDialog were neither called yet or did not return JFileChooser.APPROVE_OPTION
Also, I believe JFileChooser is case-sensitive, so if the file name is "Foo.bar" and you enter "FoO.bar", it will think you want something else.

JFileChooser: Cannot select Desktop when Selection Mode is File and Directories

I ran into an issue with JFileChooser and wanted to see if there is a workaround.
If the JFileChooser is created and the setFileSelectionMode is FILES_AND_DIRECTORIES, when a user clicks a shortcut button on the left (in XP) such as Desktop or My Documents or drop down to Desktop, the field is not placed in the File Name JTextPane. And when clicking the "Select/Accept" button, nothing happens (because isDirectorySelected() returns false for some reason).
Overriding the approveSeletion does not work because the Event Handler function in BasicFileChooser does not call it.
How would I make it so the Desktop can be selected without having to navigate to it manually, but by clicking the shortcut on the left?
Thanks
In Windows, the desktop is not backed by any file in the file system - it's a shell namespace. So there really isn't anything that JFileChooser could return to you. Yes, I know that there is a folder that contains the desktop for the user - but remember that the desktop actually displays as a composite of the user's desktop and the All Users desktop folder - plus other things that are added by the shell but not part of any folder (like the trash bin). So returning a File object that represents the 'desktop' is pretty much a non-starter.
Long and short: Ask yourself why you need to do this - chances are that you are going to wind up deep into native code territory, dealing with namespace PIDLs and all sorts of nastiness that you may not want to get into (for the life of me, I cannot understand why M$ had to make this stuff so amazingly difficult to use)...
Here's an intro to Windows shell namespaces so you'll have a feel for what's involved:
http://msdn.microsoft.com/en-us/library/cc144090%28v=vs.85%29.aspx
Found the following code in the BasicFileChooserUI:
if (fc.getFileSelectionMode() == JFileChooser.FILES_AND_DIRECTORIES
&& fc.getFileSystemView().isFileSystem(dir)) {
setFileName(dir.getAbsolutePath());
}
So it looks like "special folders" are purposely ignored. The code is in a private method so it would be hard to create you own UI.
As a hack you might be able to add a PropertyChangeListener to the file chooser:
public void propertyChange(final PropertyChangeEvent e)
{
String prop = e.getPropertyName();
if (JFileChooser.DIRECTORY_CHANGED_PROPERTY.equals(prop))
{
JFileChooser fileChooser = (JFileChooser)e.getSource();
File currentDirectory = (File)e.getNewValue();
String directory = currentDirectory.toString();
if (directory.endsWith("Desktop")
|| directory.endsWith("My Documents"))
{
File selectedFile = fileChooser.getSelectedFile();
if (selectedFile == null || ! selectedFile.equals(currentDirectory))
{
fileChooser.removePropertyChangeListener( this );
fileChooser.setSelectedFile( currentDirectory );
fileChooser.addPropertyChangeListener( this );
}
}
}
}

Categories