0

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

camickr
  • 321,443
  • 19
  • 166
  • 288
MrJones
  • 3
  • 1
  • 1
    A title bar is a useful part of the window that allows the user to move it around the screen, why would anyone want to hide it? You can change the text shown on the title bar using `setDialogTitle()`. – Palo Jul 11 '20 at 16:24
  • you can check whether setUndecorated(true); works for the JFileChooser. – Snorik Jul 11 '20 at 17:20
  • 1
    @Snorik, the JFileChooser is NOT a dialog or a frame, it is a component you add to a dialog or frame so it doesn't have a setUndecorated() method. Also, if you look at the posted code the OP has attempted to override the creation of the dialog to make the dialog undecorated. – camickr Jul 11 '20 at 17:29

1 Answers1

1

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.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Thank you so much for mentioning the parent dialog. Cannot believe I failed to remember that. Indeed I have to override the Approve/Cancel functionality, but that will be fairly simple! – MrJones Jul 11 '20 at 20:32