Bringing JFileChooser on top of all windows
Asked Answered
M

4

22

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

Some of my try's include using;

.requestFocus();
.requestFocusInWindow();
.setVisible();

Is there a particular attribute/method I can set in order to solve the problem?

Modred answered 26/2, 2011 at 19:44 Comment(5)
You shouldn't have to do any of this. I wonder if the reason for this is located elsewhere in your code. For instance, do you have AWT components mixed with Swing components?Exciseman
Actually no, that's the only method in the whole program that makes use of a UI the rest still is pure console. Regarding the imports am only using java.io.File, java.util.Scanner and javax.swing.JFileChooserModred
Can you create and post an SSCCE (please click on the link), a small compilable, runnable program that demonstrates your best attempt at solving this. Then we can inspect your code, run it, modify it and best be able to help you fix it.Exciseman
Reformatted code; please revert if incorrect.Kerrikerrie
Would it matter if the FJFileChooser was being called from a modal JFrame?Waggle
K
11

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);
                }
            }
        }
    }
}
Kerrikerrie answered 26/2, 2011 at 21:7 Comment(3)
I appreciate your effort and help trashgod. However like I said earlier some other (solutions) seem very lengthy and complicated for my current level. Initially i thought it was an straight forwards error on my side, but now i realize is a bit more complicated than i thought and as it is not a must due it will only benefit me during my testing procedure, i think i will leave it for now and focus on functionality. Thanks again, i will certainly try your advice when time is not a limiting factor.Modred
No problem; I have simplified the example, accordingly. The earlier version is still accessible: stackoverflow.com/posts/5129757/revisionsKerrikerrie
Updated example to use menu/actions/popup.Kerrikerrie
U
5

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.

Unseat answered 26/2, 2011 at 20:8 Comment(3)
nop that did not do the trick either, am starting to think is netbeans doing the problem. Thanks thoughModred
Could instead try: dialog.setAlwaysOnTop(true); ?Hansen
This solution worked for me when I passed my application (which is a JFrame) into the super constructor instead of passing "parent". Thanks!Turnabout
M
3
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.

Magnoliamagnoliaceous answered 27/2, 2011 at 14:41 Comment(0)
M
0

You wrote (in your comment):

Actually no, that's the only method in the whole program that makes use of a UI the rest still is pure console.

In other words, you want to add GUI components to a console application and your problem is that the JFileChooser does not become the active window, i.e. the window that has the keyboard focus.

As stated in the other answer, the problem is that you are setting the parent of the JFileChooser [open] dialog to null.

The below code creates a minimal JFrame that serves as the JFileChooser dialog parent and also serves as a top-level container. In the below code, the user is asked – via the console – if [s]he wants to select a file. If the answer is Y, the JFileChooser dialog is displayed. Once that dialog is closed, the user is again asked whether [s]he wants to select a file. Hence a console application with a GUI component which, I understand, is what you are trying to achieve.

(Notes after the code.)

import java.io.File;
import java.util.Scanner;

import javax.swing.JFileChooser;
import javax.swing.JFrame;

public class SlctFile {

    private static void fileSearch() {
        JFrame frame = new JFrame();
        frame.setAlwaysOnTop(true);
        frame.setVisible(true);
        JFileChooser fileSelect = new JFileChooser();
        int returnVal = fileSelect.showOpenDialog(frame);
        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());
        }
        frame.dispose();
    }

    public static void main(String[] args) {
        String answer;
        Scanner stdin = new Scanner(System.in);
        do {
            System.out.print("Select file? ");
            answer = stdin.nextLine();
            if ("Y".equalsIgnoreCase(answer)) {
                fileSearch();
            }
        } while ("Y".equalsIgnoreCase(answer));
        System.exit(0);
    }
}
  • Method setAlwaysOnTop ensures that the JFrame will become the active window.
  • As soon as the JFrame is displayed, the JFileChooser is immediately displayed.
  • Calling method exit is required since displaying the JFrame starts up the Event Dispatch Thread (EDT). When the main thread terminates, the EDT does not so calling method exit ensures termination of the JVM.
Madancy answered 22/6 at 6:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.