Bringing JFileChooser on top of all windows

后端 未结 3 1069
不知归路
不知归路 2020-11-22 04:14

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 pre

相关标签:
3条回答
  • 2020-11-22 04:37
    fileSelect.showOpenDialog(this)
    

    Of course, this must be a Component of some sort (the JFrame or JPanel of your main interface). All dialogs need to have a parent component if you wish them to come to the front.

    0 讨论(0)
  • 2020-11-22 04:40

    I'm not sure what your problem actually is (it's probably your Netbeans.... who knows), but have you tried overriding the createDialog method?

    Example:

    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;
       }
    };
    

    This is merely a hack solution, you should not need to do that ordinarily.

    0 讨论(0)
  • 2020-11-22 04:43

    The API for showOpenDialog() refers to showDialog(), which says, "If the parent is null, then the dialog depends on no visible window, and it's placed in a look-and-feel-dependent position such as the center of the screen."

    The example below positions the chooser in the center of the screen on my L&F. You might see how it compares to yours.

    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);
                    }
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题