Swing dialogs

Well I've been lurking here for a while and its time I gave something back for everyone's helpful hints. I needed the option to load textures from a file chooser, but didn't want to change my application to swing. So I worked this out to do it for me. Hope it helps anyone looking to do the same.  :smiley:



import com.jme.scene.state.TextureState;
import com.jme.system.DisplaySystem;
import com.jme.util.LoggingSystem;
import com.jme.util.TextureManager;
import com.jme.image.Texture;
import com.jme.renderer.Renderer;
import javax.swing.*;
import java.io.File;
import java.util.logging.Level;

public class TextureLoad
{
    private File file;
    private TextureState ts;
    private JFrame frame;

    public TextureLoad()
    {
        initTexture();
    }
   
    private void createUI() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            LoggingSystem.getLogger().log(Level.WARNING,
                    "Could not set native look and feel.");
        }
        frame.pack();
    }
 
    private void initTexture()
    { 
        frame = new JFrame();
        // center the frame
        frame.setLocationRelativeTo(null);
        // set frame so it is invisible yet still allows the dialog to pop up
        frame.setUndecorated(true);
        frame.setVisible(true);
        frame.toFront();
        createUI();
 
        JFileChooser fc = new JFileChooser();
        fc.setMultiSelectionEnabled(false);

        int result = fc.showOpenDialog(frame);
       
        if (result == JFileChooser.APPROVE_OPTION)
        {
            File file = fc.getSelectedFile();
            if (file != null)
            {
                JOptionPane.showMessageDialog(frame,
                                  "You chose this file: " +
                                  file.getPath()); 
                Renderer renderer = DisplaySystem.getDisplaySystem().getRenderer();
                // Get a TextureState
                ts = renderer.createTextureState();
                // Use the TextureManager to load a texture
                Texture t = TextureManager.loadTexture(file.getPath(),Texture.MM_LINEAR,Texture.FM_LINEAR);
                // Assign the texture to the TextureState
                ts.setTexture(t);
        }
        } else if (result == JFileChooser.CANCEL_OPTION) {
            JOptionPane.showMessageDialog(null, "User cancelled operation. No file was chosen.");
        } else if (result == JFileChooser.ERROR_OPTION) {
            JOptionPane.showMessageDialog(null, "An error occured. No file was chosen.");
        } else {
            JOptionPane.showMessageDialog(null, "Unknown operation occured.");
        }
        frame.dispose();
    }
   
    public File getfile()
    {
        return file;
    }

    public void setfile(File file)
    {
        this.file = file;
    }

    public TextureState getTs()
    {
        return ts;
    }

    public void setTs(TextureState ts)
    {
        this.ts = ts;
    }
   
}