Car*_*los 19 java swing jfilechooser openfiledialog
我似乎对我的文件选择器对话的非常简单的实现有一个问题,这需要我每次都要最小化Netbeans才能到达它,并且特别是现在通过测试它变得非常令人沮丧.
我已经在网上看到了一些解决方案,包括SO,但似乎没有一个可以做到这一点,而其他一些解决方案对我目前的水平看起来非常冗长和复杂.
private void fileSearch() {
JFileChooser fileSelect = new JFileChooser();
int returnVal = fileSelect.showOpenDialog(null);
String pathToFile;
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileSelect.getSelectedFile();
pathToFile = file.getAbsolutePath();
try {
P.binaryFileToHexString(pathToFile);
} catch (Exception e) {
System.out.print("Oops! there was an error there..." + e);
}
System.out.println("\nYou chose to open this file: " + file.getName());
}
}
Run Code Online (Sandbox Code Playgroud)
我的一些尝试包括使用;
.requestFocus();
.requestFocusInWindow();
.setVisible();
Run Code Online (Sandbox Code Playgroud)
我可以设置一个特定的属性/方法来解决问题吗?
showOpenDialog()引用的API 指的是showDialog(),"如果父级是null,则对话框取决于没有可见的窗口,并且它被放置在依赖于外观的位置,例如屏幕的中心."
下面的示例将选择器定位在L&F屏幕中央.您可能会看到它与您的比较.
package gui;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.KeyStroke;
/**
* @see http://stackoverflow.com/questions/8507521
* @see http://stackoverflow.com/questions/5129294
*/
public class ImageApp extends JPanel {
private static final int MASK =
Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
private JFileChooser chooser = new JFileChooser();
private Action openAction = new ImageOpenAction("Open");
private Action clearAction = new ClearAction("Clear");
private JPopupMenu popup = new JPopupMenu();
private BufferedImage image;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new ImageApp().create();
}
});
}
public void create() {
JFrame f = new JFrame();
f.setTitle("Title");
f.add(new JScrollPane(this), BorderLayout.CENTER);
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("File");
menu.setMnemonic('F');
menu.add(new JMenuItem(openAction));
menu.add(new JMenuItem(clearAction));
menuBar.add(menu);
f.setJMenuBar(menuBar);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setSize(new Dimension(640, 480));
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public ImageApp() {
this.setComponentPopupMenu(popup);
popup.add("Popup Menu");
popup.add(new JMenuItem(openAction));
popup.add(new JMenuItem(clearAction));
}
@Override
public Dimension getPreferredSize() {
if (image == null) {
return new Dimension();
} else {
return new Dimension(image.getWidth(), image.getHeight());
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, null);
}
private class ClearAction extends AbstractAction {
public ClearAction(String name) {
super(name);
this.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_C);
this.putValue(Action.ACCELERATOR_KEY,
KeyStroke.getKeyStroke(KeyEvent.VK_C, MASK));
}
@Override
public void actionPerformed(ActionEvent e) {
image = null;
revalidate();
repaint();
}
}
private class ImageOpenAction extends AbstractAction {
public ImageOpenAction(String name) {
super(name);
this.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_O);
this.putValue(Action.ACCELERATOR_KEY,
KeyStroke.getKeyStroke(KeyEvent.VK_O, MASK));
}
@Override
public void actionPerformed(ActionEvent e) {
int returnVal = chooser.showOpenDialog(chooser);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File f = chooser.getSelectedFile();
try {
image = ImageIO.read(f);
revalidate();
repaint();
} catch (IOException ex) {
ex.printStackTrace(System.err);
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我不确定你的问题究竟是什么(可能是你的Netbeans ......谁知道),但是你试过覆盖这个createDialog方法吗?
例:
JFileChooser fc = new JFileChooser() {
@Override
protected JDialog createDialog(Component parent) throws HeadlessException {
// intercept the dialog created by JFileChooser
JDialog dialog = super.createDialog(parent);
dialog.setModal(true); // set modality (or setModalityType)
return dialog;
}
};
Run Code Online (Sandbox Code Playgroud)
这只是一个黑客解决方案,您通常不需要这样做.