JFileChooser from the command line and appears under all windows

Ive implemented jFileChooser in my command line program, and it works the same way as with one annoying problem. It seems to open under each window without any warning. In fact, I even skipped this several times, first making me believe that I did it wrong.

I implemented this as follows:

System.out.println("Please select the file"); JFileChooser fc = new JFileChooser(); int retValue = fc.showOpenDialog(new JPanel()); if(retValue == JFileChooser.APPROVE_OPTION){ g.inputFile = fc.getSelectedFile(); }else { System.out.println("Next time select a file."); System.exit(1); } 

Essentially, I only need jFileChooser so that the user can select the file as the input file. This is the only component that needs a GUI implementation, so if I could avoid writing a GUI, that would be helpful.

+8
java command-line swing awt jfilechooser
source share
3 answers

So, after I tried to use different things from different problems, I got a result that opens consistently and reliably above every window in Windows 7.

 public class ChooseFile { private JFrame frame; public ChooseFile() { frame = new JFrame(); frame.setVisible(true); BringToFront(); } public File getFile() { JFileChooser fc = new JFileChooser(); if(JFileChooser.APPROVE_OPTION == fc.showOpenDialog(null)){ frame.setVisible(false); return fc.getSelectedFile(); }else { System.out.println("Next time select a file."); System.exit(1); } return null; } private void BringToFront() { frame.setExtendedState(JFrame.ICONIFIED); frame.setExtendedState(JFrame.NORMAL); } } 

As in my program, this is an inner class and is called by calling:

 System.out.println("Please select the file"); g.inputFile = g.new ChooseFile().getFile(); 
+10
source share

I think of two possible reasons for something like this:

  • Are you trying to connect the AWT and Swing GUI in the same program or
  • You are trying to mix a console program (that is, using System.out.println (...) and get input through a Scanner object) with the Swing GUI.

If you do one of these two things, then you should simplify and make it only a program like the Swing GUI. If this information does not help, you can provide us with more information about your problem.

Change 1
I just noticed the details of your code. The following is a new part of JPanel ():

 int retValue = fc.showOpenDialog(new JPanel()); 

To make JFileChooser act as a dialog to your top-level window (which it does not currently do and which is your main problem), you must pass a component that is in the parent's top-level window, for example, JPanel or JButton, which is stored inside your jframe or japplet.

Edit 2
Well, you're trying to mix a Java console program with a Swing GUI program that looks like ice cream with pickles — they just don't mix with each other. There is no top-level window offering the JFileChooser showOpenDialog method to act as a true dialog.

The best solution is not to do this, instead rewrite the application into the full Swing GUI.

+4
source share

In my code, I can just use null and it works. I am using Java 7 on Windows 7.

 JFileChooser chooser = new JFileChooser(System.getProperty("java.class.path")); FileNameExtensionFilter filter = new FileNameExtensionFilter("CSV files", "csv"); chooser.setFileFilter(filter); int returnVal = chooser.showOpenDialog(null); if(returnVal == JFileChooser.APPROVE_OPTION) { try { Scanner inputFile= new Scanner(new File(chooser.getSelectedFile().getAbsolutePath())); } catch (FileNotFoundException e) { e.printStackTrace(); } } 
+3
source share

All Articles