Adding Component to JFileChooser excludes other components
I want to add a JComboBox component to a JFileChooser but when I do, the default components that are supposed to be in that space don't show up.
For example, in the JFileChooser, there are several components (like the accept and cancel buttons) in the SOUTH area of the BorderLayout. When I add a custom combobox like so:
JFileChooser fc =new JFileChooser();
cb =new JComboBox();
fc.add(cb, BorderLayout.SOUTH);
dialogResult = fc.showSaveDialog();
the combobox I added is the ONLY component which shows up in the SOUTH area.
I have tried adding the combobox as an accessory, but it always shows up to the right of the file browser window. If I can control where the accessory shows up, that would be fine, but I don't see any way to do that.
I don't understand why the other components wouldn't show up?
Thanks in advance for any help.
[968 byte] By [
xgeoffa] at [2007-10-3 9:22:30]

fc.add(cb, BorderLayout.SOUTH);
This line *replaces* the components that were previously in that space - read the tutorial on BorderLayout. In addition, this code is very brittle and you can have no guarantee that it will work under different look-and-feels and in future versions of JDK, since you're making some far-fetching assumptions on the internal implementation of the file chooser dialog.
> fc.add(cb, BorderLayout.SOUTH);
>
> This line *replaces* the components that were
> previously in that space - read the tutorial on
> BorderLayout. In addition, this code is very brittle
> and you can have no guarantee that it will work under
> different look-and-feels and in future versions of
> JDK, since you're making some far-fetching
> assumptions on the internal implementation of the
> file chooser dialog.
Thanks very much for the help!
Fundamentally, i did not realize that in BorderLayout you may only set ONE component to each area (NORTH, SOUTH, CENTER, etc.).
Once I understood that, I grabbed the component at the bottom of the filechooser and placed it in a JPanel with the components I wanted to Add to the bottom.
This worked perfectly!
I am uncertain, however, about why you say the code is "brittle" and what you mean by making assumptions regarding the internal implementation.
In any case, thanks again for your help!
For anyone else who is interested, here is what the code looks like
JFileChooser fc = new JFileChooser();
JComboBox cb = new JComboBox();
JPanel panel = new JPanel();
BoxLayout box = new BoxLayout(panel,BoxLayout.Y_AXIS);
BorderLayout layout = (BorderLayout) fc.getLayout();
panel.setLayout(box);
Component comp = layout.getLayoutComponent(BorderLayout.SOUTH);
panel.add(comp);
panel.add(cb);
fc.add(panel,BorderLayout.SOUTH);
> I am uncertain, however, about why you say the code
> is "brittle" and what you mean by making assumptions
> regarding the internal implementation.
your code assumes that the the file chooser dialog looks a certain way and that it uses a BorderLayout. Both of these assumptions may not hold true in other L&Fs or in future versions of the JRE.
Regardless, the JFileChooser API already provides the means to do what you want without havign to resort to hacks. Look at setAccessory(JComponent) and getAccessory() calls.
>
> Regardless, the JFileChooser API already provides the
> means to do what you want without havign to resort to
> hacks. Look at setAccessory(JComponent) and
> getAccessory() calls.
I have looked at adding accessories but (as I state in my post) I see no way to make it show up where I want. Accessories always seem to show up to the right of the filelist view. This is most definitely NOT what I am doing.
If you have a way of specifying where that Accessory is placed, I would love to hear it and would be more than happy to use that method.
I actually don't see this as a hack. I am simply taking the components that are in the SOUTH segment and placing them into a replacement panel.
Thanks!
> I actually don't see this as a hack. I am simply
> taking the components that are in the SOUTH segment
> and placing them into a replacement panel.
Ok, let's try this again. This is a hack because you are relying on undocumented innerworkings of JFileChooser. Point me to any documentation that says that JFileChooser uses BorderLayout as the layout manager and I'll take my words back. As it stands now, your code will throw a ClassCastException if you try to run it under a L&F that implements JFileChooser using a GridBagLayout.
Incidentally, JFileChooser is just a JCompnent, which means you can add it to another panel. For example:
public class TestFileChooser extends JFrame {
public static void main(String[] args) {
JFrame app = new TestFileChooser();
app.pack();
app.setVisible(true);
}
public TestFileChooser() throws HeadlessException {
JPanel panel = new JPanel() {
{
setLayout(new BorderLayout());
JFileChooser fc = new JFileChooser();
add(fc, BorderLayout.CENTER);
add( new JComboBox(), BorderLayout.SOUTH);
}
};
setContentPane(panel);
}
}
> Point me to any documentation that
> says that JFileChooser uses BorderLayout as the
> layout manager and I'll take my words back.
I am not trying to get you to take your words back. I am not offended, I just wanted to know why you thought it was a hack.
> As it
> stands now, your code will throw a ClassCastException
> if you try to run it under a L&F that implements
> JFileChooser using a GridBagLayout.
>
True, but it is a fairly trivial task to check the layout manager beforehand.
> Incidentally, JFileChooser is just a JCompnent, which
> means you can add it to another panel. For example:
>
I like this idea. I've been using the FileChooser commands to display the modal dialog versions (e.g. showSaveDialog()) but I think your suggestion is probably a cleaner way to go. I will just need to experiment with modal windows and see how they work.
This worked beautifully, thanks for the help!
JPanel panel;
JComboBox cb;
JFileChooser fc;
cb = new JComboBox();
box = new BoxLayout(panel,BoxLayout.Y_AXIS);
panel.setLayout(box);
panel.add(fc);
panel.add(cb);
fc.showDialog(panel,"Save");
disregard my last comments, I hadn't 't recompiled my code. Am still working on it.