Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the look and feel of a single part of a java program

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();
    }
}
}
like image 673
PulsePanda Avatar asked Aug 08 '12 20:08

PulsePanda


2 Answers

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);
}
like image 116
PulsePanda Avatar answered Oct 27 '22 06:10

PulsePanda


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.

like image 22
Stephan Avatar answered Oct 27 '22 08:10

Stephan