jfilechooser

JFileChooser.showSaveDialog(…) - how to set suggested file name

笑着哭i 提交于 2019-11-26 15:57:31
问题 The JFileChooser seems to be missing afeature: a way to suggest the file name when saving a file (the thing that usually gets selected so that it would get replaced when user starts typing). Is there a way around this? 回答1: 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. EDIT: If you pass a

How to “Open” and “Save” using java

一曲冷凌霜 提交于 2019-11-26 15:51:50
问题 I want to make an "Open" and "Save" dialog in java. An example of what I want is in the images below: Open: Save: How would I go about doing this? 回答1: I would suggest looking into javax.swing.JFileChooser Here is a site with some examples in using as both 'Open' and 'Save'. http://www.java2s.com/Code/Java/Swing-JFC/DemonstrationofFiledialogboxes.htm This will be much less work than implementing for yourself. 回答2: You want to use a JFileChooser object. It will open and be modal, and block in

Using a JFileChooser with Swing GUI classes and listeners

亡梦爱人 提交于 2019-11-26 13:44:30
This is my current menu : public class DrawPolygons { public static void main (String[] args) throws FileNotFoundException { /** * Menu - file reader option */ JMenuBar menuBar; JMenu menu; JMenuItem menuItem; // Create the menu bar. menuBar = new JMenuBar(); // Build the first menu. menu = new JMenu("File"); menu.setMnemonic(KeyEvent.VK_F); menu.getAccessibleContext().setAccessibleDescription("I have items"); menuBar.add(menu); // a group of JMenuItems menuItem = new JMenuItem("Load",KeyEvent.VK_T); menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.ALT_MASK)); menuItem

Browse for image file and display it using Java Swing

回眸只為那壹抹淺笑 提交于 2019-11-26 12:30:38
My problem here is, after clicking Browse button it displays all files in a directory to choose, then the chosen image is displayed in GUI correctly. But When i click Browse button for the second time, it shows the old image only instead of showing the new one. Please help me in this. For reference, i uploaded the UI. package GUI; import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.Graphics2D; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel;

UIManager color at a JFileChooser

扶醉桌前 提交于 2019-11-26 11:34:10
问题 I\'m using Nimbus Look and Feel, with only 3 changes at its colors: UIManager.put(\"nimbusSelection\", new Color(164,164,164)); UIManager.put(\"nimbusSelectionBackground\", new Color(214,217,223)); UIManager.put(\"nimbusSelectedText\", Color.BLACK); My FileChooser looks like this: So selected file\'s name appears in white and looks bad, and it also happens for the file type selected at the combobox. I want to change it to Black, but nimbusSelectedText is already black and is not working. I

JFileChooser filters

微笑、不失礼 提交于 2019-11-26 09:47:30
问题 I am putting a JFileChooser in my program, but that only takes images. So I decided to add filters: Code import javax.swing.*; public class fileChooser { public static void main(String[] args) { JPanel panel = new JPanel(); final JFileChooser fc = new JFileChooser(); int file = fc.showOpenDialog(panel); fc.addChoosableFileFilter(new ImageFilter()); fc.setAcceptAllFileFilterUsed(false); } } I got that straight from the Java tutorials. But Eclipse underlines the following as an error: fc

How do I restrict JFileChooser to a directory?

怎甘沉沦 提交于 2019-11-26 05:37:18
问题 I want to limit my users to a directory and its sub directories but the \"Parent Directory\" button allows them to browse to an arbitrary directory. How should I go about doing that? 回答1: You can probably do this by setting your own FileSystemView. 回答2: Incase anyone else needs this in the future: class DirectoryRestrictedFileSystemView extends FileSystemView { private final File[] rootDirectories; DirectoryRestrictedFileSystemView(File rootDirectory) { this.rootDirectories = new File[]

Using a JFileChooser with Swing GUI classes and listeners

跟風遠走 提交于 2019-11-26 03:44:06
问题 This is my current menu : public class DrawPolygons { public static void main (String[] args) throws FileNotFoundException { /** * Menu - file reader option */ JMenuBar menuBar; JMenu menu; JMenuItem menuItem; // Create the menu bar. menuBar = new JMenuBar(); // Build the first menu. menu = new JMenu(\"File\"); menu.setMnemonic(KeyEvent.VK_F); menu.getAccessibleContext().setAccessibleDescription(\"I have items\"); menuBar.add(menu); // a group of JMenuItems menuItem = new JMenuItem(\"Load\"

Browse for image file and display it using Java Swing

不打扰是莪最后的温柔 提交于 2019-11-26 02:58:44
问题 My problem here is, after clicking Browse button it displays all files in a directory to choose, then the chosen image is displayed in GUI correctly. But When i click Browse button for the second time, it shows the old image only instead of showing the new one. Please help me in this. For reference, i uploaded the UI. package GUI; import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.Graphics2D; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing

Bringing JFileChooser on top of all windows

北战南征 提交于 2019-11-25 22:34:38
问题 I seem to have a problem with my very simple implementation of a file chooser dialogue that requires me to minimize Netbeans each time in order to get to it, and it gets pretty frustrating specially now with testing. I have seen a few solutions online including SO yet none seem to do the trick, while some other seem very lengthy and complicated for my current level. private void fileSearch() { JFileChooser fileSelect = new JFileChooser(); int returnVal = fileSelect.showOpenDialog(null);