JFileChooserWithSettings.java

  1. package org.opentrafficsim.draw.graphs;

  2. import java.awt.BorderLayout;
  3. import java.awt.Component;
  4. import java.awt.Dimension;
  5. import java.awt.FlowLayout;

  6. import javax.swing.JFileChooser;
  7. import javax.swing.JPanel;

  8. /**
  9.  * Small helper class that adds some components to a JFileChooser, the contents of which can be used to retrieve user settings
  10.  * for output.
  11.  * <p>
  12.  * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  13.  * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  14.  * </p>
  15.  * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
  16.  * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
  17.  * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
  18.  */
  19. public class JFileChooserWithSettings extends JFileChooser
  20. {
  21.     /** */
  22.     private static final long serialVersionUID = 20181014L;

  23.     /**
  24.      * Constructor that adds components to the left of the 'Save' and 'Cancel' button.
  25.      * @param components Components... components to add
  26.      */
  27.     public JFileChooserWithSettings(final Component... components)
  28.     {
  29.         JPanel saveCancelPanel = (JPanel) ((JPanel) this.getComponent(3)).getComponent(3);
  30.         // insert leftVsRight panel in original place of saveCancelPanel
  31.         JPanel leftVsRightPanel = new JPanel();
  32.         leftVsRightPanel.setLayout(new FlowLayout(FlowLayout.RIGHT, 0, 0));
  33.         saveCancelPanel.getParent().add(leftVsRightPanel);
  34.         // saveCancelPanel goes in there to the right
  35.         leftVsRightPanel.add(saveCancelPanel);
  36.         // to the left a panel that places it's contents to the south
  37.         JPanel allignSouthPanel = new JPanel();
  38.         allignSouthPanel.setPreferredSize(new Dimension(300, 50));
  39.         leftVsRightPanel.add(allignSouthPanel, 0);
  40.         // in that left area to the south, a panel for the setting components right to left
  41.         JPanel settingsPanel = new JPanel();
  42.         allignSouthPanel.setLayout(new BorderLayout());
  43.         allignSouthPanel.add(settingsPanel, BorderLayout.SOUTH);
  44.         settingsPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
  45.         // add components in that final panel
  46.         int index = 0;
  47.         for (Component component : components)
  48.         {
  49.             settingsPanel.add(component, index);
  50.             index++;
  51.         }
  52.     }

  53.     @Override
  54.     public String toString()
  55.     {
  56.         return "JFileChooserWithSettings []";
  57.     }

  58. }