Example usage for javax.swing SpringLayout SOUTH

List of usage examples for javax.swing SpringLayout SOUTH

Introduction

In this page you can find the example usage for javax.swing SpringLayout SOUTH.

Prototype

String SOUTH

To view the source code for javax.swing SpringLayout SOUTH.

Click Source Link

Document

Specifies the bottom edge of a component's bounding rectangle.

Usage

From source file:SpringDemo4.java

public static void setContainerSize(Container parent, int pad) {
    SpringLayout layout = (SpringLayout) parent.getLayout();
    Component[] components = parent.getComponents();
    Spring maxHeightSpring = Spring.constant(0);
    SpringLayout.Constraints pCons = layout.getConstraints(parent);

    // Set the container's right edge to the right edge
    // of its rightmost component + padding.
    Component rightmost = components[components.length - 1];
    SpringLayout.Constraints rCons = layout.getConstraints(rightmost);
    pCons.setConstraint(SpringLayout.EAST,
            Spring.sum(Spring.constant(pad), rCons.getConstraint(SpringLayout.EAST)));

    // Set the container's bottom edge to the bottom edge
    // of its tallest component + padding.
    for (int i = 0; i < components.length; i++) {
        SpringLayout.Constraints cons = layout.getConstraints(components[i]);
        maxHeightSpring = Spring.max(maxHeightSpring, cons.getConstraint(SpringLayout.SOUTH));
    }//  www  .  j  a va 2  s  . c  om
    pCons.setConstraint(SpringLayout.SOUTH, Spring.sum(Spring.constant(pad), maxHeightSpring));
}

From source file:Main.java

/**
 * Aligns the first <code>rows</code> * <code>cols</code> components of <code>parent</code> in a grid. Each
 * component in a column is as wide as the maximum preferred width of the components in that column; height is
 * similarly determined for each row. The parent is made just big enough to fit them all.
 * /*from   ww  w.  j av  a 2s  .com*/
 * @param parent
 * 
 * @param rows
 *            number of rows
 * @param cols
 *            number of columns
 * @param initialX
 *            x location to start the grid at
 * @param initialY
 *            y location to start the grid at
 * @param xPad
 *            x padding between cells
 * @param yPad
 *            y padding between cells
 */
private static void makeSpringCompactGrid(Container parent, int rows, int cols, int initialX, int initialY,
        int xPad, int yPad) {

    SpringLayout layout = new SpringLayout();
    parent.setLayout(layout);

    // Align all cells in each column and make them the same width.
    Spring x = Spring.constant(initialX);
    for (int c = 0; c < cols; c++) {
        Spring width = Spring.constant(0);
        for (int r = 0; r < rows; r++) {
            width = Spring.max(width, getConstraintsForCell(r, c, parent, cols).getWidth());
        }
        for (int r = 0; r < rows; r++) {
            SpringLayout.Constraints constraints = getConstraintsForCell(r, c, parent, cols);
            constraints.setX(x);
            constraints.setWidth(width);
        }
        x = Spring.sum(x, Spring.sum(width, Spring.constant(xPad)));
    }

    // Align all cells in each row and make them the same height.
    Spring y = Spring.constant(initialY);
    for (int r = 0; r < rows; r++) {
        Spring height = Spring.constant(0);
        for (int c = 0; c < cols; c++) {
            height = Spring.max(height, getConstraintsForCell(r, c, parent, cols).getHeight());
        }
        for (int c = 0; c < cols; c++) {
            SpringLayout.Constraints constraints = getConstraintsForCell(r, c, parent, cols);
            constraints.setY(y);
            constraints.setHeight(height);
        }
        y = Spring.sum(y, Spring.sum(height, Spring.constant(yPad)));
    }

    // Set the parent's size.
    SpringLayout.Constraints pCons = layout.getConstraints(parent);
    pCons.setConstraint(SpringLayout.SOUTH, y);
    pCons.setConstraint(SpringLayout.EAST, x);
}

From source file:com.awesomecoding.minetestlauncher.Main.java

private void initialize() {
    fileGetter = new FileGetter(userhome + "\\minetest\\temp\\");
    latest = fileGetter.getContents("http://socialmelder.com/minetest/latest.txt", true);
    currentVersion = latest.split("\n")[0];
    changelog = fileGetter.getContents("http://socialmelder.com/minetest/changelog.html", false);

    try {// w  w w. j  a  va2s.  c  o  m
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e) {
        e.printStackTrace();
    }

    frmMinetestLauncherV = new JFrame();
    frmMinetestLauncherV.setResizable(false);
    frmMinetestLauncherV.setIconImage(Toolkit.getDefaultToolkit()
            .getImage(Main.class.getResource("/com/awesomecoding/minetestlauncher/icon.png")));
    frmMinetestLauncherV.setTitle("Minetest Launcher (Version 0.1)");
    frmMinetestLauncherV.setBounds(100, 100, 720, 480);
    frmMinetestLauncherV.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    SpringLayout springLayout = new SpringLayout();
    frmMinetestLauncherV.getContentPane().setLayout(springLayout);

    final JProgressBar progressBar = new JProgressBar();
    springLayout.putConstraint(SpringLayout.WEST, progressBar, 10, SpringLayout.WEST,
            frmMinetestLauncherV.getContentPane());
    springLayout.putConstraint(SpringLayout.SOUTH, progressBar, -10, SpringLayout.SOUTH,
            frmMinetestLauncherV.getContentPane());
    springLayout.putConstraint(SpringLayout.EAST, progressBar, -130, SpringLayout.EAST,
            frmMinetestLauncherV.getContentPane());
    frmMinetestLauncherV.getContentPane().add(progressBar);

    final JButton btnDownloadPlay = new JButton("Play!");
    springLayout.putConstraint(SpringLayout.WEST, btnDownloadPlay, 6, SpringLayout.EAST, progressBar);
    springLayout.putConstraint(SpringLayout.SOUTH, btnDownloadPlay, 0, SpringLayout.SOUTH, progressBar);
    springLayout.putConstraint(SpringLayout.EAST, btnDownloadPlay, -10, SpringLayout.EAST,
            frmMinetestLauncherV.getContentPane());
    frmMinetestLauncherV.getContentPane().add(btnDownloadPlay);

    final JLabel label = new JLabel("Ready to play!");
    springLayout.putConstraint(SpringLayout.WEST, label, 10, SpringLayout.WEST,
            frmMinetestLauncherV.getContentPane());
    springLayout.putConstraint(SpringLayout.NORTH, btnDownloadPlay, 0, SpringLayout.NORTH, label);
    springLayout.putConstraint(SpringLayout.SOUTH, label, -37, SpringLayout.SOUTH,
            frmMinetestLauncherV.getContentPane());
    springLayout.putConstraint(SpringLayout.NORTH, progressBar, 6, SpringLayout.SOUTH, label);
    frmMinetestLauncherV.getContentPane().add(label);

    JTextPane txtpnNewFeatures = new JTextPane();
    txtpnNewFeatures.setBackground(SystemColor.window);
    springLayout.putConstraint(SpringLayout.NORTH, txtpnNewFeatures, 10, SpringLayout.NORTH,
            frmMinetestLauncherV.getContentPane());
    springLayout.putConstraint(SpringLayout.WEST, txtpnNewFeatures, 10, SpringLayout.WEST,
            frmMinetestLauncherV.getContentPane());
    springLayout.putConstraint(SpringLayout.SOUTH, txtpnNewFeatures, -10, SpringLayout.NORTH, btnDownloadPlay);
    springLayout.putConstraint(SpringLayout.EAST, txtpnNewFeatures, 0, SpringLayout.EAST, btnDownloadPlay);
    txtpnNewFeatures.setEditable(false);
    txtpnNewFeatures.setContentType("text/html");
    txtpnNewFeatures.setText(changelog);
    txtpnNewFeatures.setFont(new Font("Tahoma", Font.PLAIN, 12));
    frmMinetestLauncherV.getContentPane().add(txtpnNewFeatures);

    File file = new File(userhome + "\\minetest\\version.txt");

    if (!file.exists())
        newVersion = true;
    else {
        String version = fileGetter.getLocalContents(file, false);
        if (!version.equals(currentVersion))
            newVersion = true;
    }

    if (newVersion) {
        label.setText("New Version Available! (" + currentVersion + ")");
        btnDownloadPlay.setText("Download & Play");

        btnDownloadPlay.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                Thread t = new Thread() {
                    public void run() {
                        File file = new File(userhome + "\\minetest\\version.txt");
                        String version = fileGetter.getLocalContents(file, false);
                        try {
                            FileUtils.deleteDirectory(new File(userhome + "\\minetest\\minetest-" + version));
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        fileGetter.download(latest.split("\n")[1], userhome + "\\minetest\\temp\\",
                                "minetest.zip", label, progressBar, btnDownloadPlay, currentVersion);
                        try {
                            label.setText("Cleaning up...");
                            btnDownloadPlay.setText("Cleaning up...");
                            FileUtils.deleteDirectory(new File(userhome + "\\minetest\\temp"));
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        System.exit(0);
                    }
                };
                t.start();
            }
        });
    } else {
        btnDownloadPlay.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                try {
                    label.setText("Launching...");
                    btnDownloadPlay.setEnabled(false);
                    btnDownloadPlay.setText("Launching...");
                    File file = new File(userhome + "\\minetest\\version.txt");
                    String version = fileGetter.getLocalContents(file, false);
                    Runtime.getRuntime()
                            .exec(userhome + "\\minetest\\minetest-" + version + "\\bin\\minetest.exe");
                    System.exit(0);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        progressBar.setValue(100);
    }
}

From source file:SpringDemo4.java

public static void setContainerSize(Container parent, int pad) {
    SpringLayout layout = (SpringLayout) parent.getLayout();
    Component[] components = parent.getComponents();
    Spring maxHeightSpring = Spring.constant(0);
    SpringLayout.Constraints pCons = layout.getConstraints(parent);

    //Set the container's right edge to the right edge
    //of its rightmost component + padding.
    Component rightmost = components[components.length - 1];
    SpringLayout.Constraints rCons = layout.getConstraints(rightmost);
    pCons.setConstraint(SpringLayout.EAST,
            Spring.sum(Spring.constant(pad), rCons.getConstraint(SpringLayout.EAST)));

    //Set the container's bottom edge to the bottom edge
    //of its tallest component + padding.
    for (int i = 0; i < components.length; i++) {
        SpringLayout.Constraints cons = layout.getConstraints(components[i]);
        maxHeightSpring = Spring.max(maxHeightSpring, cons.getConstraint(SpringLayout.SOUTH));
    }//from   w w  w  .ja  va  2s .c om
    pCons.setConstraint(SpringLayout.SOUTH, Spring.sum(Spring.constant(pad), maxHeightSpring));
}

From source file:br.com.elotech.sits.config.form.ConfigForm.java

private void buildButtons() {
    add(configBtnSize(btnSave = new JButton("Salvar")));
    add(configBtnSize(btnCancel = new JButton("Cancelar")));

    btnSave.setActionCommand(BTN_SAVE);//from www. j  a  va  2 s.co m
    btnSave.addActionListener(this);

    btnCancel.setActionCommand(BTN_CANCEL);
    btnCancel.addActionListener(this);

    getLayoutManager().putConstraint(SpringLayout.SOUTH, btnSave, -10, SpringLayout.SOUTH, this);

    getLayoutManager().putConstraint(SpringLayout.SOUTH, btnCancel, -10, SpringLayout.SOUTH, this);

    getLayoutManager().putConstraint(SpringLayout.EAST, btnSave, -5, SpringLayout.WEST, btnCancel);

    getLayoutManager().putConstraint(SpringLayout.EAST, btnCancel, -10, SpringLayout.EAST, this);
}

From source file:org.gumtree.vis.awt.AbstractPlotEditor.java

private JPanel createHelpPanel() {
    JPanel wrap = new JPanel(new GridLayout(1, 1));

    JPanel helpPanel = new JPanel(new GridLayout(1, 1));
    helpPanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
    //       helpPanel.setBorder(BorderFactory.createTitledBorder(
    //            BorderFactory.createEtchedBorder(), "Help Topics"));

    SpringLayout spring = new SpringLayout();
    JPanel inner = new JPanel(spring);
    inner.setBorder(BorderFactory.createEmptyBorder());

    final IHelpProvider provider = plot.getHelpProvider();
    final JList list = new JList(provider.getHelpMap().keySet().toArray());
    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    //      list.setBorder(BorderFactory.createEtchedBorder());
    JScrollPane listPane1 = new JScrollPane(list);
    inner.add(listPane1);//  w w  w .j  av a  2  s  .c  o m
    listPane1.setMaximumSize(new Dimension(140, 0));
    listPane1.setMinimumSize(new Dimension(70, 0));
    //      JPanel contentPanel = new JPanel(new GridLayout(2, 1));
    //      inner.add(list);
    final JTextField keyField = new JTextField();
    keyField.setMaximumSize(new Dimension(400, 20));
    keyField.setEditable(false);
    //      keyField.setMaximumSize();
    //      keyArea.setLineWrap(true);
    //      keyArea.setWrapStyleWord(true);
    //      keyArea.setBorder(BorderFactory.createEtchedBorder());
    inner.add(keyField);
    //      contentPanel.add(new JLabel());
    //      contentPanel.add(new JLabel());
    final JTextArea helpArea = new JTextArea();
    JScrollPane areaPane = new JScrollPane(helpArea);
    helpArea.setEditable(false);
    helpArea.setLineWrap(true);
    helpArea.setWrapStyleWord(true);
    //      helpArea.setBorder(BorderFactory.createEtchedBorder());
    inner.add(areaPane);
    //      contentPanel.add(new JLabel());
    //      contentPanel.add(new JLabel());
    //      inner.add(contentPanel);
    spring.putConstraint(SpringLayout.WEST, listPane1, 2, SpringLayout.WEST, inner);
    spring.putConstraint(SpringLayout.NORTH, listPane1, 2, SpringLayout.NORTH, inner);
    spring.putConstraint(SpringLayout.WEST, keyField, 4, SpringLayout.EAST, listPane1);
    spring.putConstraint(SpringLayout.NORTH, keyField, 2, SpringLayout.NORTH, inner);
    spring.putConstraint(SpringLayout.EAST, inner, 2, SpringLayout.EAST, keyField);
    spring.putConstraint(SpringLayout.WEST, areaPane, 4, SpringLayout.EAST, listPane1);
    spring.putConstraint(SpringLayout.NORTH, areaPane, 4, SpringLayout.SOUTH, keyField);
    spring.putConstraint(SpringLayout.EAST, areaPane, -2, SpringLayout.EAST, inner);
    spring.putConstraint(SpringLayout.SOUTH, inner, 2, SpringLayout.SOUTH, areaPane);
    spring.putConstraint(SpringLayout.SOUTH, listPane1, -2, SpringLayout.SOUTH, inner);

    list.addListSelectionListener(new ListSelectionListener() {

        @Override
        public void valueChanged(ListSelectionEvent e) {
            Object[] selected = list.getSelectedValues();
            if (selected.length >= 0) {
                HelpObject help = provider.getHelpMap().get(selected[0]);
                if (help != null) {
                    keyField.setText(help.getKey());
                    helpArea.setText(help.getDiscription());
                }
            }
        }
    });

    helpPanel.add(inner, BorderLayout.NORTH);
    wrap.setName("Help");

    wrap.add(helpPanel, BorderLayout.NORTH);
    return wrap;
}

From source file:SpringBox.java

/**
 * Aligns the first <code>rows</code> * <code>cols</code> components of
 * <code>parent</code> in a grid. Each component is as big as the maximum
 * preferred width and height of the components. The parent is made just big
 * enough to fit them all./*from   ww w. ja  v a  2 s  .c o  m*/
 * 
 * @param rows
 *          number of rows
 * @param cols
 *          number of columns
 * @param initialX
 *          x location to start the grid at
 * @param initialY
 *          y location to start the grid at
 * @param xPad
 *          x padding between cells
 * @param yPad
 *          y padding between cells
 */
public static void makeGrid(Container parent, int rows, int cols, int initialX, int initialY, int xPad,
        int yPad) {
    SpringLayout layout;
    try {
        layout = (SpringLayout) parent.getLayout();
    } catch (ClassCastException exc) {
        System.err.println("The first argument to makeGrid must use SpringLayout.");
        return;
    }

    Spring xPadSpring = Spring.constant(xPad);
    Spring yPadSpring = Spring.constant(yPad);
    Spring initialXSpring = Spring.constant(initialX);
    Spring initialYSpring = Spring.constant(initialY);
    int max = rows * cols;

    // Calculate Springs that are the max of the width/height so that all
    // cells have the same size.
    Spring maxWidthSpring = layout.getConstraints(parent.getComponent(0)).getWidth();
    Spring maxHeightSpring = layout.getConstraints(parent.getComponent(0)).getWidth();
    for (int i = 1; i < max; i++) {
        SpringLayout.Constraints cons = layout.getConstraints(parent.getComponent(i));

        maxWidthSpring = Spring.max(maxWidthSpring, cons.getWidth());
        maxHeightSpring = Spring.max(maxHeightSpring, cons.getHeight());
    }

    // Apply the new width/height Spring. This forces all the
    // components to have the same size.
    for (int i = 0; i < max; i++) {
        SpringLayout.Constraints cons = layout.getConstraints(parent.getComponent(i));

        cons.setWidth(maxWidthSpring);
        cons.setHeight(maxHeightSpring);
    }

    // Then adjust the x/y constraints of all the cells so that they
    // are aligned in a grid.
    SpringLayout.Constraints lastCons = null;
    SpringLayout.Constraints lastRowCons = null;
    for (int i = 0; i < max; i++) {
        SpringLayout.Constraints cons = layout.getConstraints(parent.getComponent(i));
        if (i % cols == 0) { // start of new row
            lastRowCons = lastCons;
            cons.setX(initialXSpring);
        } else { // x position depends on previous component
            cons.setX(Spring.sum(lastCons.getConstraint(SpringLayout.EAST), xPadSpring));
        }

        if (i / cols == 0) { // first row
            cons.setY(initialYSpring);
        } else { // y position depends on previous row
            cons.setY(Spring.sum(lastRowCons.getConstraint(SpringLayout.SOUTH), yPadSpring));
        }
        lastCons = cons;
    }

    // Set the parent's size.
    SpringLayout.Constraints pCons = layout.getConstraints(parent);
    pCons.setConstraint(SpringLayout.SOUTH,
            Spring.sum(Spring.constant(yPad), lastCons.getConstraint(SpringLayout.SOUTH)));
    pCons.setConstraint(SpringLayout.EAST,
            Spring.sum(Spring.constant(xPad), lastCons.getConstraint(SpringLayout.EAST)));
}

From source file:AppSpringLayout.java

/**
 * Initialize the contents of the frame.
 *///from  w ww  .j  a v a  2s. co  m
private void initialize() {

    frame = new JFrame();
    frame.setBounds(0, 0, 850, 750);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    springLayout = new SpringLayout();
    frame.getContentPane().setLayout(springLayout);
    frame.setLocationRelativeTo(null);

    FileNameExtensionFilter filter = new FileNameExtensionFilter("Image files", "jpg", "jpeg", "png");
    fc = new JFileChooser();
    fc.setFileFilter(filter);
    // frame.getContentPane().add(fc);

    btnTurnCameraOn = new JButton("Turn camera on");
    springLayout.putConstraint(SpringLayout.WEST, btnTurnCameraOn, 51, SpringLayout.WEST,
            frame.getContentPane());
    springLayout.putConstraint(SpringLayout.EAST, btnTurnCameraOn, -581, SpringLayout.EAST,
            frame.getContentPane());
    frame.getContentPane().add(btnTurnCameraOn);

    urlTextField = new JTextField();
    springLayout.putConstraint(SpringLayout.SOUTH, btnTurnCameraOn, -6, SpringLayout.NORTH, urlTextField);
    springLayout.putConstraint(SpringLayout.WEST, urlTextField, 0, SpringLayout.WEST, btnTurnCameraOn);
    springLayout.putConstraint(SpringLayout.EAST, urlTextField, 274, SpringLayout.WEST, frame.getContentPane());
    frame.getContentPane().add(urlTextField);
    urlTextField.setColumns(10);

    originalImageLabel = new JLabel("");
    springLayout.putConstraint(SpringLayout.NORTH, originalImageLabel, 102, SpringLayout.NORTH,
            frame.getContentPane());
    springLayout.putConstraint(SpringLayout.WEST, originalImageLabel, 34, SpringLayout.WEST,
            frame.getContentPane());
    springLayout.putConstraint(SpringLayout.SOUTH, originalImageLabel, -326, SpringLayout.SOUTH,
            frame.getContentPane());
    springLayout.putConstraint(SpringLayout.EAST, originalImageLabel, -566, SpringLayout.EAST,
            frame.getContentPane());
    springLayout.putConstraint(SpringLayout.SOUTH, urlTextField, -6, SpringLayout.NORTH, originalImageLabel);
    frame.getContentPane().add(originalImageLabel);

    btnAnalyseImage = new JButton("Analyse image");
    springLayout.putConstraint(SpringLayout.NORTH, btnAnalyseImage, 6, SpringLayout.SOUTH, originalImageLabel);
    springLayout.putConstraint(SpringLayout.WEST, btnAnalyseImage, 58, SpringLayout.WEST,
            frame.getContentPane());
    springLayout.putConstraint(SpringLayout.EAST, btnAnalyseImage, 252, SpringLayout.WEST,
            frame.getContentPane());
    frame.getContentPane().add(btnAnalyseImage);

    lblTags = new JLabel("Tags:");
    frame.getContentPane().add(lblTags);

    lblDescription = new JLabel("Description:");
    springLayout.putConstraint(SpringLayout.WEST, lblDescription, 84, SpringLayout.EAST, lblTags);
    springLayout.putConstraint(SpringLayout.NORTH, lblTags, 0, SpringLayout.NORTH, lblDescription);
    springLayout.putConstraint(SpringLayout.NORTH, lblDescription, 6, SpringLayout.SOUTH, btnAnalyseImage);
    springLayout.putConstraint(SpringLayout.SOUTH, lblDescription, -269, SpringLayout.SOUTH,
            frame.getContentPane());
    frame.getContentPane().add(lblDescription);

    tagsTextArea = new JTextArea();
    springLayout.putConstraint(SpringLayout.NORTH, tagsTextArea, 459, SpringLayout.NORTH,
            frame.getContentPane());
    springLayout.putConstraint(SpringLayout.WEST, tagsTextArea, 10, SpringLayout.WEST, frame.getContentPane());
    springLayout.putConstraint(SpringLayout.EAST, tagsTextArea, -727, SpringLayout.EAST,
            frame.getContentPane());
    frame.getContentPane().add(tagsTextArea);

    descriptionTextArea = new JTextArea();
    springLayout.putConstraint(SpringLayout.NORTH, descriptionTextArea, 0, SpringLayout.SOUTH, lblDescription);
    springLayout.putConstraint(SpringLayout.WEST, descriptionTextArea, 18, SpringLayout.EAST, tagsTextArea);
    descriptionTextArea.setLineWrap(true);
    descriptionTextArea.setWrapStyleWord(true);
    frame.getContentPane().add(descriptionTextArea);

    lblImageType = new JLabel("Image type");
    springLayout.putConstraint(SpringLayout.WEST, lblTags, 0, SpringLayout.WEST, lblImageType);
    springLayout.putConstraint(SpringLayout.NORTH, lblImageType, 10, SpringLayout.SOUTH, tagsTextArea);
    springLayout.putConstraint(SpringLayout.WEST, lblImageType, 20, SpringLayout.WEST, frame.getContentPane());
    frame.getContentPane().add(lblImageType);

    lblSize = new JLabel("Size");
    springLayout.putConstraint(SpringLayout.NORTH, lblSize, 21, SpringLayout.SOUTH, lblImageType);
    springLayout.putConstraint(SpringLayout.WEST, lblSize, 20, SpringLayout.WEST, frame.getContentPane());
    frame.getContentPane().add(lblSize);

    lblLicense = new JLabel("License");
    springLayout.putConstraint(SpringLayout.WEST, lblLicense, 20, SpringLayout.WEST, frame.getContentPane());
    frame.getContentPane().add(lblLicense);

    lblSafeSearch = new JLabel("Safe search");
    springLayout.putConstraint(SpringLayout.WEST, lblSafeSearch, 10, SpringLayout.WEST, frame.getContentPane());
    springLayout.putConstraint(SpringLayout.SOUTH, lblSafeSearch, 139, SpringLayout.SOUTH,
            frame.getContentPane());
    frame.getContentPane().add(lblSafeSearch);

    String[] licenseTypes = { "unspecified", "Public", "Share", "ShareCommercially", "Modify" };
    licenseBox = new JComboBox(licenseTypes);
    springLayout.putConstraint(SpringLayout.WEST, licenseBox, 28, SpringLayout.EAST, lblLicense);
    frame.getContentPane().add(licenseBox);

    String[] sizeTypes = { "unspecified", "Small", "Medium", "Large", "Wallpaper" };
    sizeBox = new JComboBox(sizeTypes);
    springLayout.putConstraint(SpringLayout.WEST, sizeBox, 50, SpringLayout.EAST, lblSize);
    springLayout.putConstraint(SpringLayout.EAST, sizeBox, -556, SpringLayout.EAST, frame.getContentPane());
    springLayout.putConstraint(SpringLayout.EAST, licenseBox, 0, SpringLayout.EAST, sizeBox);
    frame.getContentPane().add(sizeBox);

    String[] imageTypes = { "unspecified", "AnimategGif", "Clipart", "Line", "Photo" };
    imageTypeBox = new JComboBox(imageTypes);
    springLayout.putConstraint(SpringLayout.SOUTH, descriptionTextArea, -6, SpringLayout.NORTH, imageTypeBox);
    springLayout.putConstraint(SpringLayout.NORTH, imageTypeBox, 547, SpringLayout.NORTH,
            frame.getContentPane());
    springLayout.putConstraint(SpringLayout.SOUTH, tagsTextArea, -6, SpringLayout.NORTH, imageTypeBox);
    springLayout.putConstraint(SpringLayout.WEST, imageTypeBox, 6, SpringLayout.EAST, lblImageType);
    springLayout.putConstraint(SpringLayout.EAST, imageTypeBox, -556, SpringLayout.EAST,
            frame.getContentPane());
    springLayout.putConstraint(SpringLayout.NORTH, sizeBox, 10, SpringLayout.SOUTH, imageTypeBox);
    frame.getContentPane().add(imageTypeBox);

    btnBrowse = new JButton("Browse");
    springLayout.putConstraint(SpringLayout.WEST, btnBrowse, 0, SpringLayout.WEST, btnTurnCameraOn);
    springLayout.putConstraint(SpringLayout.EAST, btnBrowse, -583, SpringLayout.EAST, frame.getContentPane());
    frame.getContentPane().add(btnBrowse);

    lblSafeSearch_1 = new JLabel("Safe search");
    springLayout.putConstraint(SpringLayout.WEST, lblSafeSearch_1, 20, SpringLayout.WEST,
            frame.getContentPane());
    springLayout.putConstraint(SpringLayout.SOUTH, lblLicense, -17, SpringLayout.NORTH, lblSafeSearch_1);
    frame.getContentPane().add(lblSafeSearch_1);

    String[] safeSearchTypes = { "Strict", "Moderate", "Off" };
    safeSearchBox = new JComboBox(safeSearchTypes);
    springLayout.putConstraint(SpringLayout.SOUTH, licenseBox, -6, SpringLayout.NORTH, safeSearchBox);
    springLayout.putConstraint(SpringLayout.WEST, safeSearchBox, 4, SpringLayout.EAST, lblSafeSearch_1);
    springLayout.putConstraint(SpringLayout.EAST, safeSearchBox, 0, SpringLayout.EAST, licenseBox);
    frame.getContentPane().add(safeSearchBox);

    btnSearchForSimilar = new JButton("Search for similar images");
    springLayout.putConstraint(SpringLayout.SOUTH, lblSafeSearch_1, -13, SpringLayout.NORTH,
            btnSearchForSimilar);
    springLayout.putConstraint(SpringLayout.SOUTH, safeSearchBox, -6, SpringLayout.NORTH, btnSearchForSimilar);
    springLayout.putConstraint(SpringLayout.NORTH, btnSearchForSimilar, 689, SpringLayout.NORTH,
            frame.getContentPane());
    springLayout.putConstraint(SpringLayout.WEST, btnSearchForSimilar, 36, SpringLayout.WEST,
            frame.getContentPane());
    frame.getContentPane().add(btnSearchForSimilar);

    btnCancel = new JButton("Cancel");
    springLayout.putConstraint(SpringLayout.NORTH, btnCancel, 0, SpringLayout.NORTH, btnBrowse);
    btnCancel.setVisible(false);
    frame.getContentPane().add(btnCancel);

    btnSave = new JButton("Save");
    springLayout.putConstraint(SpringLayout.WEST, btnCancel, 6, SpringLayout.EAST, btnSave);
    springLayout.putConstraint(SpringLayout.NORTH, btnSave, 0, SpringLayout.NORTH, btnBrowse);
    btnSave.setVisible(false);
    frame.getContentPane().add(btnSave);

    btnTakeAPicture = new JButton("Take a picture");
    springLayout.putConstraint(SpringLayout.WEST, btnTakeAPicture, 114, SpringLayout.EAST, btnBrowse);
    springLayout.putConstraint(SpringLayout.WEST, btnSave, 6, SpringLayout.EAST, btnTakeAPicture);
    springLayout.putConstraint(SpringLayout.NORTH, btnTakeAPicture, 0, SpringLayout.NORTH, btnBrowse);
    btnTakeAPicture.setVisible(false);
    frame.getContentPane().add(btnTakeAPicture);

    //// JScrollPane scroll = new JScrollPane(list);
    // Constraints c = springLayout.getConstraints(list);
    // frame.getContentPane().add(scroll, c);

    list = new JList();
    JScrollPane scroll = new JScrollPane(list);
    springLayout.putConstraint(SpringLayout.EAST, descriptionTextArea, -89, SpringLayout.WEST, list);
    springLayout.putConstraint(SpringLayout.EAST, list, -128, SpringLayout.EAST, frame.getContentPane());
    springLayout.putConstraint(SpringLayout.WEST, list, 64, SpringLayout.EAST, licenseBox);
    springLayout.putConstraint(SpringLayout.NORTH, list, 0, SpringLayout.NORTH, btnTurnCameraOn);
    springLayout.putConstraint(SpringLayout.SOUTH, list, -35, SpringLayout.SOUTH, lblSafeSearch_1);

    Constraints c = springLayout.getConstraints(list);
    frame.getContentPane().add(scroll, c);

    // frame.getContentPane().add(list);

    progressBar = new JProgressBar();
    springLayout.putConstraint(SpringLayout.NORTH, progressBar, 15, SpringLayout.SOUTH, list);
    springLayout.putConstraint(SpringLayout.WEST, progressBar, 24, SpringLayout.EAST, safeSearchBox);
    springLayout.putConstraint(SpringLayout.EAST, progressBar, 389, SpringLayout.WEST, btnTakeAPicture);
    progressBar.setIndeterminate(true);
    progressBar.setStringPainted(true);
    progressBar.setVisible(false);
    Border border = BorderFactory
            .createTitledBorder("We are checking every image, pixel by pixel, it may take a while...");
    progressBar.setBorder(border);
    frame.getContentPane().add(progressBar);

    labelTryLinks = new JLabel();
    labelTryLinks.setVisible(false);
    springLayout.putConstraint(SpringLayout.NORTH, labelTryLinks, -16, SpringLayout.SOUTH, btnSearchForSimilar);
    springLayout.putConstraint(SpringLayout.WEST, labelTryLinks, -61, SpringLayout.EAST, licenseBox);
    springLayout.putConstraint(SpringLayout.SOUTH, labelTryLinks, 0, SpringLayout.SOUTH, btnSearchForSimilar);
    springLayout.putConstraint(SpringLayout.EAST, labelTryLinks, 0, SpringLayout.EAST, licenseBox);
    frame.getContentPane().add(labelTryLinks);

    lblFoundLinks = new JLabel("");
    springLayout.putConstraint(SpringLayout.NORTH, lblFoundLinks, -29, SpringLayout.NORTH, progressBar);
    springLayout.putConstraint(SpringLayout.WEST, lblFoundLinks, 6, SpringLayout.EAST, scroll);
    springLayout.putConstraint(SpringLayout.SOUTH, lblFoundLinks, -13, SpringLayout.NORTH, progressBar);
    springLayout.putConstraint(SpringLayout.EAST, lblFoundLinks, -10, SpringLayout.EAST,
            frame.getContentPane());
    frame.getContentPane().add(lblFoundLinks);

    numberOfImagesToSearchFor = new JTextField();
    springLayout.putConstraint(SpringLayout.NORTH, numberOfImagesToSearchFor, 0, SpringLayout.NORTH,
            tagsTextArea);
    springLayout.putConstraint(SpringLayout.WEST, numberOfImagesToSearchFor, 6, SpringLayout.EAST,
            descriptionTextArea);
    springLayout.putConstraint(SpringLayout.EAST, numberOfImagesToSearchFor, -36, SpringLayout.WEST, scroll);
    frame.getContentPane().add(numberOfImagesToSearchFor);
    numberOfImagesToSearchFor.setColumns(10);

    JLabel lblNumberOfImages = new JLabel("Number");
    springLayout.putConstraint(SpringLayout.NORTH, lblNumberOfImages, 0, SpringLayout.NORTH, lblTags);
    springLayout.putConstraint(SpringLayout.WEST, lblNumberOfImages, 31, SpringLayout.EAST, btnAnalyseImage);
    springLayout.putConstraint(SpringLayout.EAST, lblNumberOfImages, -6, SpringLayout.WEST, scroll);
    frame.getContentPane().add(lblNumberOfImages);

    // label to get coordinates for web camera panel
    // lblNewLabel_1 = new JLabel("New label");
    // springLayout.putConstraint(SpringLayout.NORTH, lblNewLabel_1, 0,
    // SpringLayout.NORTH, btnTurnCameraOn);
    // springLayout.putConstraint(SpringLayout.WEST, lblNewLabel_1, 98,
    // SpringLayout.EAST, originalImagesLabel);
    // springLayout.putConstraint(SpringLayout.SOUTH, lblNewLabel_1, -430,
    // SpringLayout.SOUTH, lblSafeSearch_1);
    // springLayout.putConstraint(SpringLayout.EAST, lblNewLabel_1, 0,
    // SpringLayout.EAST, btnCancel);
    // frame.getContentPane().add(lblNewLabel_1);

    btnBrowse.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (fc.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) {
                openFilechooser();
            }
        }
    });

    btnTurnCameraOn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("turn camera on");
            turnCameraOn();
            btnCancel.setVisible(true);
            btnTakeAPicture.setVisible(true);
        }
    });

    btnTakeAPicture.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            btnSave.setVisible(true);
            imageWebcam = webcam.getImage();
            originalImage = imageWebcam;

            // to mirror the image we create a temporary file, flip it
            // horizontally and then delete

            // get user's name to store file in the user directory
            String user = System.getProperty("user.home");
            String fileName = user + "/webCamPhoto.jpg";
            File newFile = new File(fileName);

            try {
                ImageIO.write(originalImage, "jpg", newFile);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            try {
                originalImage = (BufferedImage) ImageIO.read(newFile);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            newFile.delete();
            originalImage = mirrorImage(originalImage);
            icon = scaleBufferedImage(originalImage, originalImageLabel);
            originalImageLabel.setIcon(icon);
        }
    });

    btnSave.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (fc.showSaveDialog(frame) == JFileChooser.APPROVE_OPTION) {
                try {

                    file = fc.getSelectedFile();
                    File output = new File(file.toString());
                    // check if image already exists
                    if (output.exists()) {
                        int response = JOptionPane.showConfirmDialog(null, //
                                "Do you want to replace the existing file?", //
                                "Confirm", JOptionPane.YES_NO_OPTION, //
                                JOptionPane.QUESTION_MESSAGE);
                        if (response != JOptionPane.YES_OPTION) {
                            return;
                        }
                    }
                    ImageIO.write(toBufferedImage(originalImage), "jpg", output);
                    System.out.println("Your image has been saved in the folder " + file.getPath());
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    });

    btnCancel.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            btnTakeAPicture.setVisible(false);
            btnCancel.setVisible(false);
            btnSave.setVisible(false);
            webcam.close();
            panel.setVisible(false);
        }
    });

    urlTextField.addKeyListener(new KeyAdapter() {
        @Override
        public void keyReleased(KeyEvent e) {
            if (urlTextField.getText().length() > 0) {
                String linkNew = urlTextField.getText();
                displayImage(linkNew, originalImageLabel);
                originalImage = imageResponses;
            }
        }
    });

    btnAnalyseImage.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Token computerVisionToken = new Token();
            String computerVisionTokenFileName = "APIToken.txt";

            try {
                computerVisionImageToken = computerVisionToken.getApiToken(computerVisionTokenFileName);
                try {
                    analyse();
                } catch (NullPointerException e1) {
                    // if user clicks on "analyze" button without uploading
                    // image or posts a broken link
                    JOptionPane.showMessageDialog(null, "Please choose an image");
                    e1.printStackTrace();
                }
            } catch (NullPointerException e1) {
                e1.printStackTrace();
            }
        }
    });

    btnSearchForSimilar.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            listModel.clear();

            Token bingImageToken = new Token();
            String bingImageTokenFileName = "SearchApiToken.txt";
            bingToken = bingImageToken.getApiToken(bingImageTokenFileName);

            // in case user edited description or tags, update it and
            // replace new line character, spaces and breaks with %20
            text = descriptionTextArea.getText().replace(" ", "%20").replace("\r", "%20").replace("\n", "%20");
            String tagsString = tagsTextArea.getText().replace(" ", "%20").replace("\r", "%20").replace("\n",
                    "%20");

            String numberOfImages = numberOfImagesToSearchFor.getText();

            try {
                int numberOfImagesTry = Integer.parseInt(numberOfImages);
                System.out.println(numberOfImagesTry);

                imageTypeString = imageTypeBox.getSelectedItem().toString();
                sizeTypeString = sizeBox.getSelectedItem().toString();
                licenseTypeString = licenseBox.getSelectedItem().toString();
                safeSearchTypeString = safeSearchBox.getSelectedItem().toString();

                searchParameters = tagsString + text;
                System.out.println("search parameters: " + searchParameters);

                if (searchParameters.length() != 0) {

                    // add new thread for searching, so that progress bar
                    // and searching could run simultaneously
                    Thread t1 = new Thread(new Runnable() {
                        @Override
                        public void run() {
                            progressBar.setVisible(true);
                            workingUrls = searchToDisplayOnJList(searchParameters, imageTypeString,
                                    sizeTypeString, licenseTypeString, safeSearchTypeString, numberOfImages);
                        }
                    });
                    // start searching in a separate thread
                    t1.start();
                } else {
                    JOptionPane.showMessageDialog(null,
                            "Please choose first an image to analyse or insert search parameters");
                }
            } catch (NumberFormatException e1) {
                JOptionPane.showMessageDialog(null, "Please insert a valid number of images to search for");
                e1.printStackTrace();

            }
        }
    });

    list.addListSelectionListener(new ListSelectionListener() {
        @Override
        public void valueChanged(ListSelectionEvent e) {
            int i = list.getSelectedIndex();

            if (fc.showSaveDialog(frame) == JFileChooser.APPROVE_OPTION) {
                try {
                    file = fc.getSelectedFile();
                    File output = new File(file.toString());
                    // check if file already exists, ask user if they
                    // wish to overwrite it
                    if (output.exists()) {
                        int response = JOptionPane.showConfirmDialog(null, //
                                "Do you want to replace the existing file?", //
                                "Confirm", JOptionPane.YES_NO_OPTION, //
                                JOptionPane.QUESTION_MESSAGE);
                        if (response != JOptionPane.YES_OPTION) {
                            return;
                        }
                    }
                    try {

                        URL fileNameAsUrl = new URL(linksResponse[i]);
                        originalImage = ImageIO.read(fileNameAsUrl);
                        ImageIO.write(toBufferedImage(originalImage), "jpeg", output);
                        System.out.println("image saved, in the folder: " + output.getAbsolutePath());

                    } catch (MalformedURLException e1) {
                        e1.printStackTrace();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                } catch (NullPointerException e2) {
                    e2.getMessage();
                }
            }
        }
    });
}

From source file:org.gumtree.vis.hist2d.Hist2DChartEditor.java

private JPanel createHelpPanel(JFreeChart chart) {
    JPanel wrap = new JPanel(new GridLayout(1, 1));

    JPanel helpPanel = new JPanel(new GridLayout(1, 1));
    helpPanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
    //       helpPanel.setBorder(BorderFactory.createTitledBorder(
    //            BorderFactory.createEtchedBorder(), "Help Topics"));

    SpringLayout spring = new SpringLayout();
    JPanel inner = new JPanel(spring);
    inner.setBorder(BorderFactory.createEmptyBorder());

    final IHelpProvider provider = new Hist2DHelpProvider();
    final JList list = new JList(provider.getHelpMap().keySet().toArray());
    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    //      list.setBorder(BorderFactory.createEtchedBorder());
    JScrollPane listPane1 = new JScrollPane(list);
    inner.add(listPane1);//from  w  w w . j a  v  a  2s .com
    listPane1.setMaximumSize(new Dimension(140, 0));
    listPane1.setMinimumSize(new Dimension(70, 0));
    //      JPanel contentPanel = new JPanel(new GridLayout(2, 1));
    //      inner.add(list);
    final JTextField keyField = new JTextField();
    keyField.setMaximumSize(new Dimension(400, 20));
    keyField.setEditable(false);
    //      keyField.setMaximumSize();
    //      keyArea.setLineWrap(true);
    //      keyArea.setWrapStyleWord(true);
    //      keyArea.setBorder(BorderFactory.createEtchedBorder());
    inner.add(keyField);
    //      contentPanel.add(new JLabel());
    //      contentPanel.add(new JLabel());
    final JTextArea helpArea = new JTextArea();
    JScrollPane areaPane = new JScrollPane(helpArea);
    helpArea.setEditable(false);
    helpArea.setLineWrap(true);
    helpArea.setWrapStyleWord(true);
    //      helpArea.setBorder(BorderFactory.createEtchedBorder());
    inner.add(areaPane);
    //      contentPanel.add(new JLabel());
    //      contentPanel.add(new JLabel());
    //      inner.add(contentPanel);
    spring.putConstraint(SpringLayout.WEST, listPane1, 2, SpringLayout.WEST, inner);
    spring.putConstraint(SpringLayout.NORTH, listPane1, 2, SpringLayout.NORTH, inner);
    spring.putConstraint(SpringLayout.WEST, keyField, 4, SpringLayout.EAST, listPane1);
    spring.putConstraint(SpringLayout.NORTH, keyField, 2, SpringLayout.NORTH, inner);
    spring.putConstraint(SpringLayout.EAST, inner, 2, SpringLayout.EAST, keyField);
    spring.putConstraint(SpringLayout.WEST, areaPane, 4, SpringLayout.EAST, listPane1);
    spring.putConstraint(SpringLayout.NORTH, areaPane, 4, SpringLayout.SOUTH, keyField);
    spring.putConstraint(SpringLayout.EAST, areaPane, -2, SpringLayout.EAST, inner);
    spring.putConstraint(SpringLayout.SOUTH, inner, 2, SpringLayout.SOUTH, areaPane);
    spring.putConstraint(SpringLayout.SOUTH, listPane1, -2, SpringLayout.SOUTH, inner);

    list.addListSelectionListener(new ListSelectionListener() {

        @Override
        public void valueChanged(ListSelectionEvent e) {
            Object[] selected = list.getSelectedValues();
            if (selected.length >= 0) {
                HelpObject help = provider.getHelpMap().get(selected[0]);
                if (help != null) {
                    keyField.setText(help.getKey());
                    helpArea.setText(help.getDiscription());
                }
            }
        }
    });

    helpPanel.add(inner, BorderLayout.NORTH);
    wrap.setName("Help");

    wrap.add(helpPanel, BorderLayout.NORTH);
    return wrap;

}

From source file:SpringBox.java

/**
 * Aligns the first <code>rows</code> * <code>cols</code> components of
 * <code>parent</code> in a grid. Each component in a column is as wide as
 * the maximum preferred width of the components in that column; height is
 * similarly determined for each row. The parent is made just big enough to
 * fit them all./*from w  ww .  j  av a 2s .  co m*/
 * 
 * @param rows
 *          number of rows
 * @param cols
 *          number of columns
 * @param initialX
 *          x location to start the grid at
 * @param initialY
 *          y location to start the grid at
 * @param xPad
 *          x padding between cells
 * @param yPad
 *          y padding between cells
 */
public static void makeCompactGrid(Container parent, int rows, int cols, int initialX, int initialY, int xPad,
        int yPad) {
    SpringLayout layout;
    try {
        layout = (SpringLayout) parent.getLayout();
    } catch (ClassCastException exc) {
        System.err.println("The first argument to makeCompactGrid must use SpringLayout.");
        return;
    }

    // Align all cells in each column and make them the same width.
    Spring x = Spring.constant(initialX);
    for (int c = 0; c < cols; c++) {
        Spring width = Spring.constant(0);
        for (int r = 0; r < rows; r++) {
            width = Spring.max(width, getConstraintsForCell(r, c, parent, cols).getWidth());
        }
        for (int r = 0; r < rows; r++) {
            SpringLayout.Constraints constraints = getConstraintsForCell(r, c, parent, cols);
            constraints.setX(x);
            constraints.setWidth(width);
        }
        x = Spring.sum(x, Spring.sum(width, Spring.constant(xPad)));
    }

    // Align all cells in each row and make them the same height.
    Spring y = Spring.constant(initialY);
    for (int r = 0; r < rows; r++) {
        Spring height = Spring.constant(0);
        for (int c = 0; c < cols; c++) {
            height = Spring.max(height, getConstraintsForCell(r, c, parent, cols).getHeight());
        }
        for (int c = 0; c < cols; c++) {
            SpringLayout.Constraints constraints = getConstraintsForCell(r, c, parent, cols);
            constraints.setY(y);
            constraints.setHeight(height);
        }
        y = Spring.sum(y, Spring.sum(height, Spring.constant(yPad)));
    }

    // Set the parent's size.
    SpringLayout.Constraints pCons = layout.getConstraints(parent);
    pCons.setConstraint(SpringLayout.SOUTH, y);
    pCons.setConstraint(SpringLayout.EAST, x);
}