8

So I'm making a program selection tool, and currently i like the way everything looks with just the java look and feel. the only thing i want to change is the JFileChooser look and feel to Windows. When i call the filechooser and tell it to change the look and feel then, it doesn't do anything. when i call it when the program starts, it makes the buttons look crappy. so google doesn't have anything, and i cant figure out how to get this to work. please help! let me know what code would be relevant and useful. Thanks in advance!

EDIT: So here's some code relevent to the JFileChooser and how it is started:

public class Start(){
    public static JButton assignButton = new JButton(new AbstractAction(
        "Assign") {
    public void actionPerformed(ActionEvent e) {
        AssignWindow.run();
    }
});
}

public class AssignmentWindow(){
   public static void run() {
    Dialogs.assignmentInfo();

    bgImage = aw.getImage("files/background.png");

            //aw is the object of this class
    aw.makeFrame();     //makes the jframe for all the buttons to sit.
    aw.setGraphics();   //assigns a different graphics variable

    aw.fileChooser();
}

public void fileChooser() {
    JFileChooser jfc = new JFileChooser();
    jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

            // here is where i want to set the look and feel....

    if (jfc.showDialog(null, "Select") == JFileChooser.APPROVE_OPTION) {
        File file = jfc.getSelectedFile();
        fileDir = file.getPath();
    } else {
        Dialogs.msg("You cancled selecting a file. Returning to file frame...");
        AssignWindow.destroy();
    }
}
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
PulsePanda
  • 1,806
  • 10
  • 33
  • 56
  • well i have a bunch of code, so i wasnt sure on what parts to include that would be useful... – PulsePanda Aug 08 '12 at 20:19
  • Your problem is related to JFileChooser look and feel. That's the part that needs to be reviewed. It will also help you on analyze the problem by yourself, isolating the relevant code. – Alfabravo Aug 08 '12 at 20:21

4 Answers4

18

All that is needed is to change the UIManager while creating the JFileChooser Object, then setting it back to what it was previously, alternatively you could just catch Exception, but that is bad practice.

public void stuff(){
    JFileChooser chooser = null;
    LookAndFeel previousLF = UIManager.getLookAndFeel();
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        chooser = new JFileChooser();
        UIManager.setLookAndFeel(previousLF);
    } catch (IllegalAccessException | UnsupportedLookAndFeelException | InstantiationException | ClassNotFoundException e) {}
    //Add whatever other settings you want to the method
    chooser.showOpenDialog(frame);
}
Loligans
  • 497
  • 9
  • 24
PulsePanda
  • 1,806
  • 10
  • 33
  • 56
7

Yes, that is possible. You can set the UI per hand:

JFileChooser jfc = new JFileChooser();
WindowsFileChooserUI wui = new WindowsFileChooserUI(jfc);
wui.installUI(jfc);
jfc.showOpenDialog(parentComponent);

This will set the windows UI for the filechooser but keep the look and feel for all other components.

Stephan
  • 4,395
  • 3
  • 26
  • 49
  • could you explain how this helps? – PulsePanda Aug 08 '12 at 20:36
  • ok the WindowFileChooserUI is an import right? because it gives me an error when i try this... – PulsePanda Aug 08 '12 at 20:49
  • If this does not solve your problem, I do not understand what you are doing. Your code does not contain the part where you change something of the look and feel. – Stephan Aug 08 '12 at 20:50
  • This solution doesn't work for me. I tried to use it with JScrollPane, because I made my own LaF with modified scrollpanes. Scrollpanes are used in comboboxes too, these should stay modified, but in my case it's necessary to use the metal-LaF to scrollpanes if JTables attached to it. – prototype0815 Mar 09 '17 at 06:55
4

push Cancel JButton, there are changes from Metal to System and Nimbus Look and Feel

all updates to the already visible container must be called by code line

SwingUtilities.updateComponentTreeUI(Top-Level Container);

code

import java.io.File;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;

class ChooserFilterTest {

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                String[] properties = {"os.name", "java.version", "java.vm.version", "java.vendor"};
                for (String property : properties) {
                    System.out.println(property + ": " + System.getProperty(property));
                }
                JFileChooser jfc = new JFileChooser();
                jfc.showOpenDialog(null);
                jfc.addChoosableFileFilter(new FileFilter() {

                    @Override
                    public boolean accept(File f) {
                        return f.isDirectory() || f.getName().toLowerCase().endsWith(".obj");
                    }

                    @Override
                    public String getDescription() {
                        return "Wavefront OBJ (*.obj)";
                    }

                    @Override
                    public String toString() {
                        return getDescription();
                    }
                });
                int result = JOptionPane.showConfirmDialog(null, "Description was 'All Files'?");
                System.out.println("Displayed description (Metal): " + (result == JOptionPane.YES_OPTION));
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    SwingUtilities.updateComponentTreeUI(jfc);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                jfc.showOpenDialog(null);
                result = JOptionPane.showConfirmDialog(null, "Description was 'All Files'?");
                System.out.println("Displayed description (System): " + (result == JOptionPane.YES_OPTION));
                result = JOptionPane.showConfirmDialog(null, "Description was 'All Files'?");
                System.out.println("Displayed description (Metal): " + (result == JOptionPane.YES_OPTION));
                try {
                    for (UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                        if ("Nimbus".equals(info.getName())) {
                            UIManager.setLookAndFeel(info.getClassName());
                            SwingUtilities.updateComponentTreeUI(jfc);
                            break;
                        }
                    }
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                }
                jfc.showOpenDialog(null);
                result = JOptionPane.showConfirmDialog(null, "Description was 'All Files'?");
                System.out.println("Displayed description (System): " + (result == JOptionPane.YES_OPTION));
            }
        };
        SwingUtilities.invokeLater(r);
    }

    private ChooserFilterTest() {
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
2

You should read about Look and Feel.
Also I don't think you can have different L&F per component. At least I've never seen an application with non-uniform L&F

Cratylus
  • 52,998
  • 69
  • 209
  • 339
  • I've already looked into that link, and i understand how to do that. thats not what i want. i want to set the l&f for the filechooser, not this window. if i set it like that, everything is changed. – PulsePanda Aug 08 '12 at 20:30
  • Where did you read that you can have a different L&F per component? Do you have a reference? – Cratylus Aug 08 '12 at 20:34
  • @wbAnon The problem you are facing is that if you change the values of a given UI component, you effect all the future UI components created (such as the `JList`, `JButton`, `JLabel` ect) – MadProgrammer Aug 08 '12 at 20:34
  • I heard it for like changing the look of a scroll bar and such, and unfortunatly i do not have a reference – PulsePanda Aug 08 '12 at 20:36
  • @wbAnon:Where is the code where you try to update L&F as you say in your OP? – Cratylus Aug 08 '12 at 20:37