Skip to content
Advertisement

Possible to remove Title bar from JFileChooser?

I am trying to display a simple JFileChooser that has no Titlebar. Below is the example code:

package ca.customfilepicker.main;
import java.awt.Component;
import java.awt.HeadlessException;

import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JFileChooser;

class CustomFileChooser

{

    public static void main(String args[]) {

        JFileChooser chooser = new JFileChooser() {
            @Override
            protected JDialog createDialog(Component parent) throws HeadlessException {
            
                JDialog diag = super.createDialog(parent);
                
                //diag.setUndecorated(true);
                return diag;
            }
        };
        
        chooser.setBorder(BorderFactory.createTitledBorder("Open"));
        chooser.showOpenDialog(null);

    }
}

So essentially I want the Border I set to be the top level Title bar. Example image:

https://i.stack.imgur.com/exogU.png

So far I have had zero luck achieving this, nor found any others looking for a similar appearance. Appreciate the help! Cheers

Advertisement

Answer

The JFileChooser is just a Swing component. It can be added to any Container.

So you could create an undecorated JDialog and add an instance of the JFileChooser to the dialog.

The problem is now that the “Open” and “Cancel” buttons won’t close the dialog, so you would need to to that manually. You could probably override the “approveSelection()andcancelSelection()` methods of the JFileChooser.

I would guess the logic would be to invoke super.approveSelection() or super.cancelSelection() and then use the SwingUtilities.windowForComponent(...) method to get the parent window and then invoke dispose() on the window.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement