swing - Problems with JFileChooser in java GUI -


I am trying to add a JFileChooser to JPanel. Sometimes it works properly and sometimes it just shows the JPNL without the JeffelCloser dialogue box. Now I do not really know what to do? Anyone can help me please

Here's my code (it is a way):

  public file chooseFileFromComputer () {methodPanel = new JPanel (); MethodPanel.setLayout (zero); MethodFrame = new JFrame (); MethodFrame.setTitle ("File Selector"); MethodFrame.setVisible (true); Remove BufferedImage java image = new buffford image (1, 1, buffard image .tep_inderg); MethodFrame.setIconImage (removeJavaImage); MethodLabel = New JLabel ("Choose a file:"); MethodLabel.setBounds (10, 10, 80, 20); MethodPanel.add (methodLabel); FileChooser = New JFileChooser (); FileChooser.setMultiSelectionEnabled (false); FileChooser.setBounds (10, 35, 550, 500); FileChooser.setVisible (true); Add (fileChooser); / ** * Work Event # AE ******** ******* # ** / fileChooser.addActionListener (new ActionListener () {public void actionPerformed (ActionEvent actionEvent) {string command = actionEvent.getActionCommand (); if (command.equals (JFileChooser.APPROVE_SELECTION)) {selectedFile = fileChooser.getSelectedFile (); methodFrame.setVisible (false);} else if (command.equals (JFileChooser.CANCEL_SELECTION)) {methodFrame.setVisible (wrong) ;}}}); // Completion of Proceedings Events #________ AE _______ # Legislation.ed (Filechanger); MethodFrame.setContentPane (methodPanel); MethodFrame.setResizable (wrong); MethodFrame.setSize (600, 600); MethodFrame.setDefaultCloseOperation (EXIT_ON_CLOSE); Return file selected; }  

postprop = "text">
  1. You are using a null layout and called SetBounds (...) On your components it seems a better way to create a complex GUI for a newbie, this is an illusion, and the more you make swing GUI is also you respect and Use layout managers and see if these creatures make flexible, beautiful and need to learn to help quite a lot in the The GUI
  2. added the component to the JFrame JFrame and then the setVisible (true) adds.
  3. If a JFrame should absolutely use does not look like you are making it visible and invisible, you might want to actually use a JDialog or even a free-standing JFileChooser Dialog Are there.

Edit
You are adding JFileChooser to more than one container:

 Add  (Filmmaker); // ************************************************************************************* fileChooser.addActionListener (New ActionListener actionPerformed (actionEvent actionEvent) {string command = actionEvent.getActionCommand (); if (command.equals (JFileChooser.APPROVE_SELECTION)) {selectedFile = fileChooser.getSelectedFile (); methodFrame.setVisible (false);} else if (command.equals ( JFileChooser.CANCEL_SELECTION)) {methodFrame.setVisible (false);}}}); MethodPanel.add (fileChooser); // ******** here *******  

You can not do this Only a container, and it can not be displayed properly or at all. Never set before, because it is determined by ActionListener, which has returned a long time after this method.

The solution: Do not even use a JFrame where a modal JDialog will work much better if you are ActionListener a model used to communicate and returned after the , your The code will work.


edit
But again for my money, I only use one JFileChooser as a modal dialogue. For example:

  JFileclosure filechutzer = new jfileclosure (); FileChooser.setDialogTitle ("Choose a file"); // Do not use the null in the method below, but reference to your current GUI int Regulation = File Chooser.Show Open Dialog (empty); If (feedback == JFileChooser.APPROVE_OPTION) {file file = filechocessor.tatset selected file (); Println (file); }  

Comments