Question To JFileChooser - OpenDialog
I use a JFileChooser to open a dialog for choosing a file:
JFileChooser fc = new JFileChooser();
int retVal = fc.showOpenDialog(m_main);
In these dialog is a button for creating a new folder. But the new folder is always named "New Folder" and I cannot rename it in this dialog. Can I set any property to the JFileChooser so that I can rename the new folder ? Or if this is not possible, can I set that the button for creating a new folder is not visible ?
Thanks for help, Nadine.
Hi.
The name "New Folder" is hardcoded in the FileSystemView, so theres no way to change it easily. Also no reference is saved for the new folder button in the ui.
You can write your own ui (you can copy most things, just don't add the create button) or you search through the components in the FileChooser until you find the right button and remove it...
Hope this helps
Similar Messages
-
2 questions of JFileChooser,please !
Dear all,
I have have 2 questions about using JFileChooser:
1. How to disable " Create a new folder"?
2. How NOT to display the hidding files? It seems simply "chooser.setHiddingFilesEnabled(true)" dosn't take effect.
Thanks a lot.
Kevin1. How to disable " Create a new folder"?This I don't know, but I know that in your filter, you can filter out directories as an option
2. How NOT to display the hidding files? It seems
simply "chooser.setHiddingFilesEnabled(true)" dosn't
take effect. what about chooser.setHidingFilesEnabled(false)?
>
Thanks a lot.
Kevin -
Question regarding JFileChooser
A user calls up a JFileChooser, navaigates through the file system and selects File "file" in Directory "directory."
How do I set "directory" such tat the next time the the FileChooser is called up, opens up in "directory?"
Many thanks in advance.JFileChooser(File currentDirectory)
Constructs a JFileChooser using the given File as the path.
Read the API before asking next time. -
Hi,
I want to implement "Save As" functionality in my App where user should be able to save as -->
CSV
CSV with Headers
DBase 2
DBase 3
DIF
Excel
Excel with Headers
Excel 5
Excel 5 with Headers
HTML Table
PowerSoft report
SQL
SYLK
SYLK with Headers
Text
Text With Headers
WKS
WKS with Headers
WK1
WK1 with Headers
Windows MetaFile
Is there any example for this anywhere?
Thankshttp://java.sun.com/docs/books/tutorial/uiswing/components/filechooser.html
-
JFileChooser and JTextArea question
Hello NG !!!
I Have to questions about the GUI with Swing:
1.) JFileChooser: I have a JFrame with my JFileChooser (OpenDialog). How can I realize, that my OpenDialog shows me just the ".txt" files ?
2.) JTextArea: I want use my JTextArea fro infromation the user about the result. How can I realize, that the JTextArea is deeper or has a frame ?
Lot of thanks for your help !!!
schaf77
PS: Happy new Year !!!!1.) JFileChooser: I have a JFrame with my JFileChooser
(OpenDialog). How can I realize, that my OpenDialog
shows me just the ".txt" files ?Set a filefiler on your filechooser like this:
myJFileChooser.setFileFilter(new MyFileFilter() );
import java.io.File;
import javax.swing.*;
import javax.swing.filechooser.*;
public class MyFileFilter extends FileFilter {
// Accept all directories and all txt files.
public boolean accept(File f) {
if (f.isDirectory()) {
return true;
String extension = getExtension(f);
if (extension != null) {
if (extension.equals("txt") ) {
return true;
} else {
return false;
return false;
private static String getExtension(File f) {
String ext = null;
String s = f.getName();
int i = s.lastIndexOf('.');
if (i > 0 && i < s.length() - 1) {
ext = s.substring(i+1).toLowerCase();
return ext;
// The description of this filter
public String getDescription() {
return "Textfile (*.txt)";
2.) JTextArea: I want use my JTextArea fro infromation
the user about the result. How can I realize, that the
JTextArea is deeper or has a frame ?What do you mean? -
Newbie Question: Getting the file name from a JFileChooser
Hi,
I have a simple problem.
I'd like to get the filename that a user types into a JFileChooser Save dialog before they save a file.
How do I retrieve this name?
Cheers,
Chris ShareHey it's ok, you're new. But one thing you've got to learn is to RTFM (Read the .... Manual). You'll answer own your questions much faster that way.
Take a look at the example at the top of:
http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JFileChooser.html -
JFileChooser and date format questions
I have a JFileChooser that currently works - I set the default directory and allow the user to select a file or enter a file name.
I have been asked to modify this to prepopulate the file name field on the JFileChooser.
How do I do that? It is not obvious which method to call to set the filename field.
Also, the file name needs to be a timestamp "*yyyyMMddHHmmss*.dat"
I tried creating the timestamp part with the following code, but it does not format as expected.
String s = "";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddddHHmmss");
try
java.util.Date now = new java.util.Date();
java.util.Date dat = dateFormat.parse( now.toString() );
s = dat.toString();
}What is wrong with this?
Thanks.I don't do Swing and this is also not the subforum for Swing questions. It has its own subforum.
-
Hey Guys,
My First post on the sun forums, so a big hello to you guys. I have a question in relation to best practice in querying something in one user interface, of another user interface. Suppose i have a class that consists on a TextField,JButton, and a JButton which opens a JFileChooser; i would like to return the value of the JTextField from the second GUI class to a calling gui class (gui1), once the OK button has been pressed. The second gui class consists of the following.
package UserInterface;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
Class that allows for a local file to be chosen
@author pthug
public class ImageChooser extends JFrame {
private JButton okButton;
private JButton chooseFileButton;
private JTextField locationTextField;
private JPanel mainPanel;
private JPanel locationPanel;
private JFileChooser fc;
private String address;
//default constructor
public ImageChooser() {
super("Image Chooser");
//set default size
setSize(new Dimension(450,250));
setResizable(false);
//create components
initComponents();
//set layout - top to bottom
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
locationPanel.setLayout(new FlowLayout());
//add components to frame
addComponents();
//pak components
pack();
//centre frame on screen
setLocationRelativeTo(null);
//close frame as default operation
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
//add action listeners to components
createActionListeners();
//show frame
setVisible(true);
//*adds all components*
*private void addComponents() {*
*locationPanel.add(locationTextField);*
*locationPanel.add(chooseFileButton);*
*mainPanel.add(locationPanel);*
*mainPanel.add(okButton);*
*this.add(mainPanel);*
*//*create the components for interface
private void initComponents() {
okButton = new JButton("OK");
chooseFileButton = new JButton("...");
locationTextField = new JTextField(50);
mainPanel = new JPanel();
locationPanel = new JPanel();
fc = new JFileChooser();
address = new String("");
//create action listeners for components
private void createActionListeners() {
//open File Choose Dialog
chooseFileButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == chooseFileButton) {
int returnVal = fc.showOpenDialog(ImageChooser.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
locationTextField.setText(file.getPath());
} else {
System.out.println("Open command cancelled by user." + "\n");
//ok button action listener
okButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
address = locationTextField.getText();
//some more methods that determine if a web address has been typed in the JTextField or whether a
//local file has been chosen using the JFileChooser which has not been written yet
//@param args
public static void main(String[] args) {
ImageChooser co = new ImageChooser();
}and a call the class from a the first gui class as such
GUI 1
addImageItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
imageChooser = new ImageChooser();
... remaining source of class remove as its not necessary to the questionhow can i query when the ok button has been pressed on the second gui class, so that the first gui knows when to take the value from the JTextField. Would i just put a method in the second gui which is a boolean that holds true or false whether its ok to take the value from the JTextField and query it like such...
// while(imageChooser.isStillOpen()) {
// }Hope that makes sense.
Regards,
KevJ
Edited by: KevJ on 10-Nov-2009 17:05Thank you for your reply bharath.bravo, that has solved my question. Which i did as such, i provided a method in the second GUI class to enable me to add an ActionListener to the OK button. As such
* this method allows other classes to add their action listeners to
* the OK Button
* @param e Action Listener from another class
public void addActionToOKButton(ActionListener e) {
okButton.addActionListener(e);
} I then added a accessor method, to get the value from the textfield; which i've implemented it as such:
imageChooser.addActionToOKButton(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
//use imageChooser accessor method to get the value from the text field
imageLocation = imageChooser.getLocationTextField();
//DEBUG statement
System.out.println("gui one recieved: " + imageLocation);
//get rid of imageChooser window
imageChooser.dispose();
}For the sake of clarity, in your first suggestion. You mentioned that i could implement the WindowListener Interface in the first gui, and add it as a Listener to the second gui. Would i do that like this, and what would be a prefferable way of doing this out of the two methods you suggested.
public class GUI extends JFrame implements WindowListener
@Override
public void windowClosed(WindowEvent arg0) {
// TODO Auto-generated method stub
//get value of jTextField using its public accessor method
add window listener to second gui, from the first GUI
imageChooser.addWindowListener(this);Regards,
KevinJ
Edited by: KevJ on 11-Nov-2009 14:38 -
Hi,
I have 2 questions concerning the use of JFileChooser:
1. When you use "Details" option to see the details of the files; the "Modified" column seems to show the time in "United States" format even if the local regional setting is something else. Is this the default behavior of the JFileChooser or is there something that I can do to change that?
2. It seems that you cannot just click on the column headers such as "Modified" to have the files sorted by that column like real Window's file chooser... Is that also the default behavior or is there something that I can do to change that?
Thanks....you can read this
http://java.sun.com/docs/books/tutorial/uiswing/components/filechooser.html
to know all the subtilities fo the JFileChooser -
JFileChooser Question .... wo hoo!
Hey there,
I have a question regarding the JFileChooser class. I have managed to instantiate a file chooser and specify that it display directories only, I have also worked out how to get information when the "Attach" button (my custom button) or the "cancel" button is pressed. When the "Attach" button is pressed I return a file object that selects the user selected directory. I then print the directory to the standard output. Unfortunately the entire selection is not being printed, only the top level directory relevant to the selection is being printed. If you are familiar with JFileChooser then you will probably know what it looks like when it is instantiated. To refresh a couple of areas of significance to my program are the "Look in:" section and the "File Name:" section.
I need my program to print the inforamtion in the "File Name:" section not the "Look in:" section as it is doing now. My code is as follows:
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class TestChooser extends JFrame
public TestChooser()
final JFileChooser fC = new JFileChooser(); //Final modifier needed
//because this object is used in an inner class.
fC.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
JButton button1 = new JButton();
button1.setText("Activate File Chooser");
button1.setBounds(50, 50, 50, 50);
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int returnVal = fC.showDialog(TestChooser.this, "Attach");
if(returnVal == JFileChooser.APPROVE_OPTION)
File curDirectY = fC.getCurrentDirectory();
/* The "Look In:" Label information is being returned,
instead of the complete directory & folder details
contained in "File Name:". Note that File Name has the
keyMnemonic of underscore N.
String str = curDirectY.toString();
System.out.println("Of course the selected Directory is "+str);
this.getContentPane().add(button1);
public static void main(String args[])
JFrame frame = new TestChooser();
frame.setBounds(100, 100, 300, 200);
frame.setVisible(true);
}I have noticed that the "File Name:" section has a Key Mnemonic of N so maybe that can be used in the actionListener, I'm just not sure how. If anybody can help me with retrieving the information from the "File Name:" section as opposed to the "Look in:" section it will be greatly appreciated.
Regards
Davo<snip>
File curDirectY = fC.getCurrentDirectory();The above method is the wrong method to call. It returns the directory that the JFileChooser itself is pointing at. It knows nothing about what file or directory may have been selected. Use the getSelectedFile() method instead.
Jim S. -
JFileChooser question regarding the order of the files in a folder
I was hoping someone had a way around this issue I am having with the JFileChooser. I have a folder that has filenames in it like text1.txt, text2.txt, and so forth all the way up to text300.txt. When the filechooser shows these files it shows the order of them as:
text1.txt
text10.txt
text11.txt
text2.txt
text20.txt
text21.txt
and so forth.
How come it doesn't list them like they would be in a folder window like:
text1.txt
text2.txt
text3.txt
text150.txt
text151.txt
text299.txt
text300.txt
Actually I know why JFileChooser does it this way, it is because it sorts them as Strings without taking into account the number at the end of a similar string.
Anyway, I was wondering if anyone had some ideas of how I could alter the JFileChooser to list the contents of a folder like this in the "right" order. Hopefully that made sense. :) Thanks.Thank u for ur reply.... actually the thing is mine's one of the pre-ordered iphones n it's reaching me by 2moro i think. so are the pre-ordered iphone's already activated? or m i in trouble if it's not activated out of the box (if i have to activate it myself). ?
-
Hello everyone ...
I have a problem that has many answers on this site, but i cannot understand them (damn newbie me ! :)) ).
I use a JFileChooser to save a file and I want the extension of the file to be automatically appended to the filename. I use a filter for viewing files but i don't know how can I use a filter to save the files.
Thanks in advance,
Danclass MyExtensionFilter extends FileFilter {
String myExtension = "";
public MyExtensionFilter(String myExtension) {
this.myExtension = myExtension;
public boolean accept(File file) {
return file.getName().endWith(myExtension);
public string getDescription() {
return myExtension;
File chosenFile = myFileChooser.getSelectedFile();
chosenFile = newFile(chosenFile.getAbsolutePath() + myFileFilter.getDescription(); -
JFileChooser or FileDialog.... so many questions
hi all,
i have searched the forum to no avail so i need your help.
i am wanting to use jfilechooser to get the directory listing and files over a tcp/ip connection. i send the getdirectory message to the server and it returns the directory. next, i send the getfiles message to the server and it returns the files in the directory. i can get the directory by overriding the setcurrentdirectory()method but how do i set the files in the filechooser. can it even be done this way. if so, what methods do i need to override to accomplish this task. any help will be greatly appreciated
thanksI believe JFileChooser is only for file systems that the current JVM has access to (in other words, not remote file systems).
-
Quick question: JFileChooser mode
Hello All,
I have a bit of a problem regarding JFileChooser. My application has 1 main window, and subwindows which are created when certain actions are performed on the main window. I want to create a file chooser when I perform certain actions on one of the subwindows, and make that chooser modal with respect to the subwindow only. I want to be able to perform other operations on the main window, even if the file chooser is shown. Is this possible?
Currently, I have a JFileChooser sublclass, and I tried to override the createDialog() method so that I can manipulate the shown dialog's modal property. So far this approach of changing the modal property does not get me close to the behavior I want. [ The dialog does not block when modal is set to false, and setting it to true makes the other windows inoperable. ]
Does anyone have any idea regarding this problem? Please let me know.
Thanks.
Regards,
CocohI am pretty sure that the modality (is that a word?) of a Dialog is by definition application wide. To do what you want you will probably have to manually enforce the single window modality. You could probably do this by adding a WindowFocusListener to your disabled window that will force focus to your quasi-modal window.
Hope this helps,
Josh Castagno
http://www.jdc-software.com -
We all know that the JFileChooser is incredibly sloooooooow in sdk1.4 (especially across networks.)
Maybe someone can help me with this (sort of) workaround:
I selected a directory in the file chooser. It takes 10 mins to come back. During that time, I'd like my mouse pointer to change to the WAIT state. I can do that part, but the file chooser gives me back control before its done & my cursor goes back to the DEFAULT state.
Does anyone know how to find out when the file choose is really done?Hi all,
thanks for your answers I just realized that I did not expalin myself clearly:
I know about the rescanCurrentDirectory method BUT I do not know how to link it in the JFileChooser diaolog interface.
The only way I figured out is to create an accessory button an add it to JFileChooser (setAccessory) BUT it looks ugly .....
Any idea ????
Maybe you are looking for
-
hi. i have a problem . this program is taking so much time . can we reduce the execution time? pls help me. code is *& Report ZTEST11 REPORT ZTEST11 NO STANDARD PAGE HEADING LINE-SIZE 191 LINE-COUNT 65
-
I have created a merged letter (mail merge) using Word for Mac. But I cannot merge the letter to my contacts which are in the Apple Mail Application. Is it possible to do this? Word for Mac 8 i mac intel
-
Edit Autofill for a single field?
There's a form I use a lot. I simply enter currency amounts into it in the form of xx.95. IOW, two digits, decimal, 95. So, when I wanted to enter 11.95, I had only to enter "1". One time, when I wanted to enter 11.95, I accidentally entered 11 and l
-
Version mismatch error in SOAP interface
Hi Scenario is SOAP to SOAP. Sending request by SOAP UI to receiver webservice. Interface was perfectly done & tested successfully. Then we have done some changes in Receiver soap channel ...due to some error the changes were reverted but now i am ge
-
When I try to delete video message
" You are deleting video being used by projects. after deleting, you will not be able to play tis video in the projects. " If I have burned the project to dvd via IDVD will it still play on my mac?