Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #5366

Re: Using JFileChooser from modal dialog

Newsgroups comp.lang.java.gui
Date 2013-09-10 12:26 -0700
References <02d2e4b3-ab8a-4063-82c9-bd6e991bfac7@lr19g2000pbb.googlegroups.com>
Message-ID <c713853a-73c4-4bf3-9e9a-7abf98a52677@googlegroups.com> (permalink)
Subject Re: Using JFileChooser from modal dialog
From gosmith666@gmail.com

Show all headers | View raw


On Thursday, February 9, 2012 12:59:29 PM UTC-5, FredK wrote:
> How do I pop up and use a JFileChooser from a modal dialog?
> 
> 
> 
> I find the methods setModalExclusionType and setModalityType, but they
> 
> are methods of Window and Dialog; when I pop the file chooser using
> 
> showDialog(), how do I get access to the window or frame used to
> 
> display it so that I can set its modality?
> 
> --
> 
> Fred K

      try
      {
        // open a new file chooser dialog box.
        final JFileChooser aFileChooser = new JFileChooser();
        
        aFileChooser.setDragEnabled(false);
        aFileChooser.setDialogType(JFileChooser.OPEN_DIALOG);
        aFileChooser.setMultiSelectionEnabled(true);
        aFileChooser.setAcceptAllFileFilterUsed(true);
        aFileChooser.setApproveButtonText("Select");
        
        final JFrame aFrame = new JFrame("File Viewer");
        aFrame.getContentPane().add(aFileChooser, BorderLayout.CENTER);
        aFrame.pack();
        aFrame.setLocationRelativeTo(null);
        aFrame.setAlwaysOnTop(true);
        aFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        aFrame.setVisible(true);   
              
        aFileChooser.addActionListener(new ActionListener() 
        {  
          @Override
          public void actionPerformed(ActionEvent e) 
          {
            try
            {
              if ((e.getActionCommand()).equals(JFileChooser.APPROVE_SELECTION)) 
              {
                setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); 
                
                // user has selected a file
                File aFile = aFileChooser.getSelectedFile(); 

                // create text are to display the file
                JTextArea aText = new JTextArea();
                aText.setWrapStyleWord(true);
                aText.setEditable(false);
                aText.read(new FileReader(aFile.getAbsolutePath()), null);
                
                JScrollPane aScrollPane = new JScrollPane(aText);
           
                // display newly selected file in a seperate frame.
                JFrame aFrame2 = new JFrame(aFile.getName());
                aFrame2.getContentPane().add(aScrollPane, BorderLayout.CENTER);
                aFrame2.pack();
                aFrame2.setLocationRelativeTo(null);
                aFrame2.setAlwaysOnTop(true);
                aFrame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                aFrame2.setVisible(true);
              }
              else
              {
                aFrame.dispose();
              }
            }
              
            catch (Exception ex)
            {    
              setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
              Errors.displayErrorDialog(ex, "Exception");
            }
          }
        });
      }
      
      catch(Exception ex)
      {
        setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
        Errors.displayErrorDialog(ex, "Exception");
      }

This makes sure the File Chooser is Modal and AlwaysOnTop

Back to comp.lang.java.gui | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Using JFileChooser from modal dialog Fred <fred.l.kleinschmidt@gmail.com> - 2012-02-09 09:59 -0800
  Re: Using JFileChooser from modal dialog Fred <fred.l.kleinschmidt@gmail.com> - 2012-02-09 14:40 -0800
    Re: Using JFileChooser from modal dialog markspace <-@.> - 2012-02-09 23:07 -0800
  Re: Using JFileChooser from modal dialog gosmith666@gmail.com - 2013-09-10 12:26 -0700
    Re: Using JFileChooser from modal dialog "John B. Matthews" <nospam@nospam.invalid> - 2013-09-11 06:51 -0400

csiph-web