Results 1 to 11 of 11
- 05-05-2012, 11:32 PM #1
Member
- Join Date
- Aug 2010
- Posts
- 6
- Rep Power
- 0
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); }
-
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); } }); } }
- 05-06-2012, 01:25 PM #3
Member
- Join Date
- Aug 2010
- Posts
- 6
- Rep Power
- 0
-
Re: Adding JFilechooser to JFrame glass pane, unable to see Filechooser popupmenu.
- 05-06-2012, 02:52 PM #5
Member
- Join Date
- Aug 2010
- Posts
- 6
- Rep Power
- 0
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
-
Re: Adding JFilechooser to JFrame glass pane, unable to see Filechooser popupmenu.
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 03:12 PM.
- 05-06-2012, 06:13 PM #7
Member
- Join Date
- Aug 2010
- Posts
- 6
- Rep Power
- 0
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.
- 05-06-2012, 07:35 PM #8
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.
Already told your boss that's the way to do it, huh?Its imperative for me to use the glass pane
Are you talking about the combo box?The Filechooser should show the popup menu
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 05-06-2012, 11:30 PM #9
Member
- Join Date
- Aug 2010
- Posts
- 6
- Rep Power
- 0
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

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-06-2012 at 11:52 PM.
-
Re: Adding JFilechooser to JFrame glass pane, unable to see Filechooser popupmenu.
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.
- 05-08-2012, 05:17 AM #11
Member
- Join Date
- Aug 2010
- Posts
- 6
- Rep Power
- 0
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
-
Unable to hide a JFrame
By MiserySyndrome in forum AWT / SwingReplies: 3Last Post: 05-14-2011, 07:00 PM -
Adding jmenubar inside jtabbed pane
By suresh_m5a5@yahoo.co.in in forum AWT / SwingReplies: 1Last Post: 02-25-2011, 02:10 PM -
adding multiple jpanels to content pane
By tooktook22 in forum AWT / SwingReplies: 2Last Post: 01-19-2011, 06:40 PM -
Update the JFrame after change the Content Pane
By alisonchan30 in forum AWT / SwingReplies: 1Last Post: 04-26-2010, 06:22 AM -
Changing content pane of a JFrame?
By dunafrothint in forum AWT / SwingReplies: 1Last Post: 03-11-2010, 10:21 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks