Java JButton Create createOkCancelPanel(final JButton okBtn, final JButton cancelBtn)

Here you can find the source of createOkCancelPanel(final JButton okBtn, final JButton cancelBtn)

Description

create Ok Cancel Panel

License

Open Source License

Declaration

public static JPanel createOkCancelPanel(final JButton okBtn, final JButton cancelBtn) 

Method Source Code

//package com.java2s;
/*/*  w  w  w . j a  v a 2 s .  co  m*/
 * Copyright (c) 2015 Memorial Sloan-Kettering Cancer Center
 * *
 * * Code written by: Christian Lopes
 * * Authors: Gary Bader, Elena Potylitsine, Chris Sander, Weston Whitaker
 * *
 * * This library is free software; you can redistribute it and/or modify it
 * * under the terms of the GNU Lesser General Public License as published
 * * by the Free Software Foundation; either version 2.1 of the License, or
 * * any later version.
 * *
 * * This library is distributed in the hope that it will be useful, but
 * * WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF
 * * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  The software and
 * * documentation provided hereunder is on an "as is" basis, and
 * * Memorial Sloan-Kettering Cancer Center
 * * has no obligations to provide maintenance, support,
 * * updates, enhancements or modifications.  In no type shall the
 * * Memorial Sloan-Kettering Cancer Center
 * * be liable to any party for direct, indirect, special,
 * * incidental or consequential damages, including lost profits, arising
 * * out of the use of this software and its documentation, even if
 * * Memorial Sloan-Kettering Cancer Center
 * * has been advised of the possibility of such damage.  See
 * * the GNU Lesser General Public License for more details.
 * *
 * * You should have received a copy of the GNU Lesser General Public License
 * * along with this library; if not, write to the Free Software Foundation,
 * * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 */

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout.ParallelGroup;
import javax.swing.GroupLayout.SequentialGroup;

import javax.swing.JButton;

import javax.swing.JPanel;

public class Main {
    public static JPanel createOkCancelPanel(final JButton okBtn, final JButton cancelBtn) {
        return createOkCancelPanel(okBtn, cancelBtn, new JButton[0]);
    }

    public static JPanel createOkCancelPanel(final JButton okBtn, final JButton cancelBtn, JButton... otherBtns) {
        final JPanel panel = new JPanel();

        final GroupLayout layout = new GroupLayout(panel);
        panel.setLayout(layout);
        layout.setAutoCreateContainerGaps(false);
        layout.setAutoCreateGaps(true);

        final SequentialGroup hg = layout.createSequentialGroup();
        final ParallelGroup vg = layout.createParallelGroup(Alignment.CENTER, false);

        if (otherBtns != null) {
            for (int i = 0; i < otherBtns.length; i++) {
                final JButton btn = otherBtns[i];
                hg.addComponent(btn);
                vg.addComponent(btn);
            }
        }

        hg.addGap(0, 0, Short.MAX_VALUE);

        final JButton btn1 = isMac() ? cancelBtn : okBtn;
        final JButton btn2 = isMac() ? okBtn : cancelBtn;

        if (btn1 != null) {
            hg.addComponent(btn1);
            vg.addComponent(btn1);
        }
        if (btn2 != null) {
            hg.addComponent(btn2);
            vg.addComponent(btn2);
        }

        layout.setHorizontalGroup(hg);
        layout.setVerticalGroup(vg);

        return panel;
    }

    public static boolean isMac() {
        return System.getProperty("os.name").startsWith("Mac OS X");
    }
}

Related

  1. createJButton(String label, Font font)
  2. createJButton(String text, String name, ActionListener a)
  3. createLabelButton(String text)
  4. createListButton(Action action)
  5. createNoURLGraphicalButton(String imageName, String alternateText)
  6. CreateOptionButtonGroup(Box boxGeneric, Border bdrButtonGroup, String[] elements, String[] commandActions, Dimension elementDimension)
  7. createRadioButton(JComponent parent, String text)
  8. createRadioButton(String name, String command, boolean isSelected, ActionListener listener)
  9. createRadioButton(String text, ButtonGroup buttonGroup, ActionListener... listeners)