UIManager color at a JFileChooser

JFileChooser is compound JComponent, you can extract JButtons, JToggleButtons and JScrollPane with JViewPort that contains JList, please download Darryl’s Swing Utils , read descriptions, then run (Darryl’s) code, result is selection for JList or JTable (I voting for that) import java.awt.Color; import java.awt.Graphics; import javax.swing.*; import javax.swing.plaf.metal.MetalButtonUI; public class CrazyFileChooser { public static void main(String[] … Read more

How to “Open” and “Save” using java

You want to use a JFileChooser object. It will open and be modal, and block in the thread that opened it until you choose a file. Open: JFileChooser fileChooser = new JFileChooser(); if (fileChooser.showOpenDialog(modalToComponent) == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); // load from file } Save: JFileChooser fileChooser = new JFileChooser(); if (fileChooser.showSaveDialog(modalToComponent) == … Read more

How do I set a suggested file name using JFileChooser.showSaveDialog(…)?

If I understand you correctly, you need to use the setSelectedFile method. JFileChooser jFileChooser = new JFileChooser(); jFileChooser.setSelectedFile(new File(“fileToSave.txt”)); jFileChooser.showSaveDialog(parent); The file doesn’t need to exist. If you pass a File with an absolute path, JFileChooser will try to position itself in that directory (if it exists).

JFileChooser filters

I am putting a JFileChooser in my program, but that only takes images. For a list of types supported by that JRE on that OS, use ImageIO. FileFilter imageFilter = new FileNameExtensionFilter( “Image files”, ImageIO.getReaderFileSuffixes()); Types seen – Java 1.6/Windows 7 bmp jpg jpeg wbmp png gif Note: don’t hard-code that list! It might change … Read more

How do I restrict JFileChooser to a directory?

Incase anyone else needs this in the future: class DirectoryRestrictedFileSystemView extends FileSystemView { private final File[] rootDirectories; DirectoryRestrictedFileSystemView(File rootDirectory) { this.rootDirectories = new File[] {rootDirectory}; } DirectoryRestrictedFileSystemView(File[] rootDirectories) { this.rootDirectories = rootDirectories; } @Override public File createNewFolder(File containingDir) throws IOException { throw new UnsupportedOperationException(“Unable to create directory”); } @Override public File[] getRoots() { return rootDirectories; … Read more

Browse for image file and display it using Java Swing

Each time a new image is selected, you’re creating components unnecessarily and in error here: public void setTarget(File reference) { //…. panel_1.setLayout(new BorderLayout(0, 0)); panel_1.add(new JLabel(new ImageIcon(targetImg))); setVisible(true); Instead I would recommend that you have all these components created from the get-go, before any file/image has been selected, and then in this method, create an … Read more