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 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).

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

Using a JFileChooser with Swing GUI classes and listeners

As a general rule, you should not have your GUI classes, such as the class extending JPanel, implement any listener interfaces, and in fact you should strive for just the opposite — to separate the control functions of the program (the listeners and the like) from the view functions of the program (the GUI). So … Read more

tech