Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: JFileChooser.showOpenFileDialog()

  1. #1
    Junior Member
    Join Date
    Mar 2024
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JFileChooser.showOpenFileDialog()

    JFileChooser fchsr=new JFileCbooser();

    In the above show...Dialog() call, 'file type' combo box is still shown even
    flchsr.setFileSelectionMode(JFileChooser.DIRECTORI ES_ONLY) is called.
    Since only a folder is sought, 'file type' combo is irrelevant, right?
    I was looking for a method to either set the combo null, or disabled but can't identify the combo...
    n=flchsr.getComponentCount() reports 3. So for each Component, 0 to 2, I try to identify what it is by
    Component cmp; String nm; Point pt; boolean on;
    cmp=flchsr.getComponent(0 to 2);
    nm=cmp.getName();
    on=com.isShowing();
    pt=cmp.getLocation();
    Then nm=null, on=false, and pt.x=pt.y=0 for all reported component...

    So how can I drop or disable 'file type' in the dialog? Any input, suggestion is greatly appreciated.
    (I don't want to write a custom dialog since this Java-provided one is already available, you know.)

  2. #2
    Member
    Join Date
    Jan 2024
    Posts
    75
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: JFileChooser.showOpenFileDialog()

    It seems like you're facing a challenge in customizing the behavior of the file type combo box in the JFileChooser dialog. While the setFileSelectionMode() method is intended to limit the selection to directories only, the file type combo box remains visible, which might be irrelevant in this context.

    Unfortunately, directly accessing or modifying the file type combo box within the JFileChooser dialog is not officially supported by the Java API. The components inside the JFileChooser dialog might not be easily accessible or modifiable using standard methods.

    However, there might be a workaround you could consider. You could potentially subclass JFileChooser and override certain methods to customize its behavior. For instance, you might override the createDialog() method to intercept the creation of the dialog and make modifications to its components before it is displayed.

    Alternatively, you could explore third-party libraries or frameworks that provide more extensive customization options for file chooser dialogs.

    While it's understandable that you prefer to utilize the Java-provided dialog, sometimes customizing its behavior might require more advanced techniques beyond the standard API. If you're open to it, creating a custom dialog might be the most straightforward solution to achieve the exact behavior you desire.

    I hope this provides some insight into your issue, and I wish you the best of luck in finding a suitable solution! If you have any further questions or need clarification, feel free to ask.

    While it's understandable that you prefer to utilize the Java-provided dialog, sometimes customizing its behavior might require more advanced techniques beyond the standard API. If you're open to it, creating a custom dialog might be the most straightforward solution to achieve the exact behavior you desire. Exploring resources for help with Java assignment, such as programminghomeworkhelp.com, could also provide valuable insights into addressing this challenge effectively.

  3. #3
    Junior Member
    Join Date
    Jul 2024
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JFileChooser.showOpenFileDialog()

    If it's not too late, this is what I have used, you can set the line fileChooser.setFileSelectionMode(DIRECTORIES); to filter for folders, or fileChooser.setFileSelectionMode(JFileChooser.FILE S_AND_DIRECTORIES); for both. Also notice that I filter for ".png" files only, so you have a choice there. I normally I put this in a small window with a frame and a panel with OK and Close Button, that way I have control over the cosmetics. In my case a click on OK, the window closes and returns a filename, or if you modify it the folder.
    JFileChooser fileChooser = new JFileChooser(System.getProperty("user.dir"));
    fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    fileChooser.setAcceptAllFileFilterUsed(false);
    fileChooser.addChoosableFileFilter(new javax.swing.filechooser.FileFilter() {
    	@Override
    	public boolean accept(File f) {
    		return f.isDirectory() || f.getName().toLowerCase().endsWith(".png");
    	}
     
    	@Override
    	public String getDescription() {
    		return "Directories and PNG files";
    	}
    });
     
    fileChooser.addActionListener(new ActionListener() {
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		if (JFileChooser.APPROVE_SELECTION.equals(e.getActionCommand())) {
    			File selectedFile = fileChooser.getSelectedFile();
    			if (selectedFile != null && selectedFile.isFile()) {
    				fileName = selectedFile.getAbsolutePath();
    				filenameField.setText(fileName);
    				if (!fileName.isEmpty() && new File(fileName).exists()) {
    					try {
    					// do whatever                                }
    			} else if (selectedFile != null && selectedFile.isDirectory()) {
    				fileChooser.setCurrentDirectory(selectedFile);
    			}
    		}
    		fileFrame.dispose();
    	}
    });
    hope that helps someone.

Similar Threads

  1. Question about JFileChooser
    By rolfis in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: March 23rd, 2013, 07:24 PM
  2. JFileChooser
    By Karthik Prabhu in forum AWT / Java Swing
    Replies: 4
    Last Post: June 23rd, 2012, 04:47 AM
  3. JFileChooser Please Help
    By mulligan252 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: October 9th, 2011, 11:03 AM
  4. JFileChooser frustrations!
    By chrisivey1980 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 3rd, 2011, 05:32 AM
  5. JFileChooser
    By FretDancer69 in forum AWT / Java Swing
    Replies: 2
    Last Post: February 3rd, 2010, 06:35 AM