Results 1 to 11 of 11
  1. #1
    Epicmaster is offline Member
    Join Date
    Aug 2010
    Posts
    6
    Rep Power
    0

    Default Adding JFilechooser to JFrame glass pane, unable to see Filechooser popupmenu.

    I tried adding a filechooser to the glass pane of a JFrame, and everytime I try clicking on the "look for" popup, the menu does not show up, I think its behind the filechooser since the glass pane is always on top, here is a reproduced test code

    Thanks!

    Java Code:
    public void gui(){
    
        JFrame frame = new JFrame("Test");
    
        frame.setSize(800, 800);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
    
        //Containment Panel
        RightBottomCenter = new JPanel();
    
    
         //Setup Glass Pane
        JPanel glass = (JPanel) frame.getGlassPane();
        glass.setVisible(true);
    
        //Config Filechooser
        fc = new JFileChooser();
        fc.setDragEnabled(false);
        fc.setDialogType(JFileChooser.OPEN_DIALOG);
        fc.setMultiSelectionEnabled(false);
        fc.setAcceptAllFileFilterUsed(false);
        fc.setApproveButtonText("Select");
        fc.setPreferredSize(new Dimension(frame.getWidth(), frame.getHeight() - 100));
    
    
        RightBottomCenter.add(fc);
        glass.add(RightBottomCenter);
        frame.setVisible(true);
    
    
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Adding JFilechooser to JFrame glass pane, unable to see Filechooser popupmenu.

    Hm, it seems to work for me:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Foo005 {
       public static void main(String[] args) {
          JFrame frame = new JFrame("Test");
    
          frame.setSize(800, 800);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setLocationRelativeTo(null);
    
          // Containment Panel
          JPanel RightBottomCenter = new JPanel();
    
          // Setup Glass Pane
          final JPanel glass = (JPanel) frame.getGlassPane();
          glass.setVisible(true);
    
          // Config Filechooser
          final JFileChooser fc = new JFileChooser();
          fc.setDragEnabled(false);
          fc.setDialogType(JFileChooser.OPEN_DIALOG);
          fc.setMultiSelectionEnabled(false);
          fc.setAcceptAllFileFilterUsed(false);
          fc.setApproveButtonText("Select");
          fc.setPreferredSize(new Dimension(frame.getWidth(),
                frame.getHeight() - 100));
    
          RightBottomCenter.add(fc);
          glass.add(RightBottomCenter);
          frame.setVisible(true);
          
          fc.addActionListener(new ActionListener() {
             
             @Override
             public void actionPerformed(ActionEvent e) {
                if (e.getActionCommand().equals(JFileChooser.APPROVE_SELECTION)) {
                   System.out.println("approved! Selection: " + fc.getSelectedFile());
                }
                fc.setVisible(false);
                glass.setVisible(false);
             }
          });
       }
    }

  3. #3
    Epicmaster is offline Member
    Join Date
    Aug 2010
    Posts
    6
    Rep Power
    0

    Default Re: Adding JFilechooser to JFrame glass pane, unable to see Filechooser popupmenu.

    Quote Originally Posted by Fubarable View Post
    Hm, it seems to work for me:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Foo005 {
       public static void main(String[] args) {
          JFrame frame = new JFrame("Test");
    
          frame.setSize(800, 800);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setLocationRelativeTo(null);
    
          // Containment Panel
          JPanel RightBottomCenter = new JPanel();
    
          // Setup Glass Pane
          final JPanel glass = (JPanel) frame.getGlassPane();
          glass.setVisible(true);
    
          // Config Filechooser
          final JFileChooser fc = new JFileChooser();
          fc.setDragEnabled(false);
          fc.setDialogType(JFileChooser.OPEN_DIALOG);
          fc.setMultiSelectionEnabled(false);
          fc.setAcceptAllFileFilterUsed(false);
          fc.setApproveButtonText("Select");
          fc.setPreferredSize(new Dimension(frame.getWidth(),
                frame.getHeight() - 100));
    
          RightBottomCenter.add(fc);
          glass.add(RightBottomCenter);
          frame.setVisible(true);
          
          fc.addActionListener(new ActionListener() {
             
             @Override
             public void actionPerformed(ActionEvent e) {
                if (e.getActionCommand().equals(JFileChooser.APPROVE_SELECTION)) {
                   System.out.println("approved! Selection: " + fc.getSelectedFile());
                }
                fc.setVisible(false);
                glass.setVisible(false);
             }
          });
       }
    }
    I click on the drop down menu and nothing happens...
    I'm using Windows 7 with Java JDK 1.6_29 and the latest Java JRE.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Adding JFilechooser to JFrame glass pane, unable to see Filechooser popupmenu.

    Quote Originally Posted by Epicmaster View Post
    I click on the drop down menu and nothing happens...
    I'm using Windows 7 with Java JDK 1.6_29 and the latest Java JRE.
    I have no idea what you're talking about. What drop-down menu? I see no code for this in your example above, unless I'm missing something.

  5. #5
    Epicmaster is offline Member
    Join Date
    Aug 2010
    Posts
    6
    Rep Power
    0

    Default Re: Adding JFilechooser to JFrame glass pane, unable to see Filechooser popupmenu.

    In the Filechooser there is a drop down menu beside the "look in" label. it allows you to browse through diffrent major directories, by clicking the down pointing arrow, a popup menu (or drop down) should appear displaying the directory tree on your system. I think because I added the Filechooser to the glass pane I dont see this drop down menu, that's my problem

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Adding JFilechooser to JFrame glass pane, unable to see Filechooser popupmenu.

    Quote Originally Posted by Epicmaster View Post
    In the Filechooser there is a drop down menu beside the "look in" label. it allows you to browse through diffrent major directories, by clicking the down pointing arrow, a popup menu (or drop down) should appear displaying the directory tree on your system. I think because I added the Filechooser to the glass pane I dont see this drop down menu, that's my problem
    That makes sense that it doesn't show since we're covering up the layer that would normally display this. This is one good reason you never see JFileChoosers or similar components being displayed in a glass pane.

    Instead consider using a CardLayout.

    e.g.,

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Foo005 {
       public static void main(String[] args) {
          JFrame frame = new JFrame("Test");
          
          final CardLayout cardlayout = new CardLayout();
          final JPanel cardPanel = new JPanel(cardlayout);
    
          frame.setSize(800, 800);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setLocationRelativeTo(null);
          frame.getContentPane().add(cardPanel);
    
    
          // Setup Glass Pane
    
          // Config Filechooser
          final JFileChooser fc = new JFileChooser();
          fc.setDragEnabled(false);
          fc.setDialogType(JFileChooser.OPEN_DIALOG);
          fc.setMultiSelectionEnabled(false);
          fc.setAcceptAllFileFilterUsed(false);
          fc.setApproveButtonText("Select");
          fc.setPreferredSize(new Dimension(frame.getWidth(),
                frame.getHeight() - 100));
          
          JPanel nextPanel = new JPanel();
          nextPanel.add(new JButton("Next Panel Button"));
          int ebGap = 15;            
          nextPanel.setBorder(BorderFactory.createEmptyBorder(ebGap, ebGap, ebGap, ebGap));
          
          cardPanel.add(fc, JFileChooser.class.getName());
          cardPanel.add(nextPanel, "Next Panel");
          
          frame.setVisible(true);
          
          fc.addActionListener(new ActionListener() {
             
             @Override
             public void actionPerformed(ActionEvent e) {
                if (e.getActionCommand().equals(JFileChooser.APPROVE_SELECTION)) {
                   System.out.println("approved! Selection: " + fc.getSelectedFile());
                }
                // fc.setVisible(false);
                cardlayout.next(cardPanel);
             }
          });
       }
    }
    Last edited by Fubarable; 05-06-2012 at 04:12 PM.

  7. #7
    Epicmaster is offline Member
    Join Date
    Aug 2010
    Posts
    6
    Rep Power
    0

    Default Re: Adding JFilechooser to JFrame glass pane, unable to see Filechooser popupmenu.

    Its imperative for me to use the glass pane due to the UI, so I can't use a Card Layout. The Filechooser should show the popup menu, its a bug in Swing. I was just looking for some workaround to make the Filechooser show the popup on a glasspane.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Adding JFilechooser to JFrame glass pane, unable to see Filechooser popupmenu.

    No, it's not a bug in Swing, it's your unwillingness to understand the layering of a Swing top level window including the root pane, layered pane, content pane and glass pane.

    Its imperative for me to use the glass pane
    Already told your boss that's the way to do it, huh?

    The Filechooser should show the popup menu
    Are you talking about the combo box?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    Epicmaster is offline Member
    Join Date
    Aug 2010
    Posts
    6
    Rep Power
    0

    Default Re: Adding JFilechooser to JFrame glass pane, unable to see Filechooser popupmenu.

    The only reason I said it was a bug was because of this if you don't call that a bug is irreverent,
    I still need to use the glass pane to place the Filechooser on top of all the UI components on the JFrame.

    Maybe this picture will help: Clicking on either of these buttons, a drop down menu should appear
    Adding JFilechooser to JFrame glass pane, unable to see Filechooser popupmenu.-pic.png

    There's got to be some sort of workaround.

    Thanks again for your time guys, out of the 4 forums I asked this question, this forum seems to be the only active one :)

    also I am my own Boss ;)
    Last edited by Epicmaster; 05-07-2012 at 12:52 AM.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Adding JFilechooser to JFrame glass pane, unable to see Filechooser popupmenu.

    Quote Originally Posted by Epicmaster View Post
    Its imperative for me to use the glass pane due to the UI, so I can't use a Card Layout. The Filechooser should show the popup menu, its a bug in Swing. I was just looking for some workaround to make the Filechooser show the popup on a glasspane.
    It is foolish to make it imperative to use a tool in a way that it absolutely can't be used. I agree with Darryl, that there is no bug in Swing regarding this, but in your understanding: You are displaying a component *above* the layer that shows pop-ups. Your only solution: change your requirements or your tools.

  11. #11
    Epicmaster is offline Member
    Join Date
    Aug 2010
    Posts
    6
    Rep Power
    0

    Default Re: Adding JFilechooser to JFrame glass pane, unable to see Filechooser popupmenu.

    Thanks for your time guys, I think I found an answer, turns out forcing the pop up to be a heavy weight component does the trick:

    Java Code:
     
             try {   
               Field field = PopupFactory.class.getDeclaredField("forceHeavyWeightPopupKey");
               field.setAccessible(true);
               fc.putClientProperty(field.get(null), true);
              }
                catch (Exception e) {
                e.printStackTrace();
              }

Similar Threads

  1. Unable to hide a JFrame
    By MiserySyndrome in forum AWT / Swing
    Replies: 3
    Last Post: 05-14-2011, 08:00 PM
  2. Adding jmenubar inside jtabbed pane
    By suresh_m5a5@yahoo.co.in in forum AWT / Swing
    Replies: 1
    Last Post: 02-25-2011, 03:10 PM
  3. adding multiple jpanels to content pane
    By tooktook22 in forum AWT / Swing
    Replies: 2
    Last Post: 01-19-2011, 07:40 PM
  4. Update the JFrame after change the Content Pane
    By alisonchan30 in forum AWT / Swing
    Replies: 1
    Last Post: 04-26-2010, 07:22 AM
  5. Changing content pane of a JFrame?
    By dunafrothint in forum AWT / Swing
    Replies: 1
    Last Post: 03-11-2010, 11:21 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •