Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3998732
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:33:49+00:00 2026-05-20T07:33:49+00:00

I seem to have a problem with my very simple implementation of a file

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T07:33:49+00:00Added an answer on May 20, 2026 at 7:33 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Problem Conditions I have a very simple Oracle (11g) Stored Procedure that is declared
Very simple problem, but can't seem to solve it. I'm probably just not thinking
I have a wierd problem that I can't seem to find the solution, probably
Currently I have a very simple tab system set up, the problem is when
I have a head aching problem that I can't seem to find an easy
I have (what I feel to be) a very simple scenario that I can't
I seem to have a problem with this SQL query: SELECT * FROM appts
I have a problem, which I do not seem to be able to solve...
I seem to have an app on my Dev server that has lots of
I always seem to have a hard time starting a new Firefox extension. Can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.