001 package com.croftsoft.core.gui; 002 003 import java.awt.*; 004 import javax.swing.*; 005 006 import com.croftsoft.core.lang.NullArgumentException; 007 008 /********************************************************************* 009 * A JPanel of JButtons evenly distributed in a single horizontal row. 010 * 011 * <p> 012 * Uses GridBagLayout instead of GridLayout to ensure even distribution 013 * during layout of the Buttons when the JPanel is odd-sized. 014 * </p> 015 * 016 * <p> 017 * Example: 018 * <code> 019 * <pre> 020 * buttonPanel2 = new ButtonPanel2 ( 021 * new JButton [ ] { 022 * playJButton = new JButton ( "Play" ), 023 * stopJButton = new JButton ( "Stop" ), 024 * pauseJButton = new JButton ( "Pause" ) } ); 025 * </pre> 026 * </code> 027 * </p> 028 * 029 * @version 030 * 2001-07-10 031 * @since 032 * 2001-07-10 033 * @author 034 * <a href="https://www.croftsoft.com/">David Wallace Croft</a> 035 *********************************************************************/ 036 037 public final class ButtonPanel2 038 extends JPanel 039 ////////////////////////////////////////////////////////////////////// 040 ////////////////////////////////////////////////////////////////////// 041 { 042 043 /********************************************************************* 044 * @param panelBackgroundColor 045 * 046 * If null, the default will be used. 047 * 048 * @throws NullArgumentException 049 * 050 * If jButtons is null or jButtons[i] is null. 051 *********************************************************************/ 052 public ButtonPanel2 ( 053 JButton [ ] jButtons, 054 Color panelBackgroundColor ) 055 ////////////////////////////////////////////////////////////////////// 056 { 057 super ( new GridBagLayout ( ) ); 058 059 NullArgumentException.check ( jButtons ); 060 061 GridBagConstraints gridBagConstraints = new GridBagConstraints ( ); 062 063 gridBagConstraints.weightx = 1.0; 064 065 gridBagConstraints.fill = GridBagConstraints.HORIZONTAL; 066 067 for ( int i = 0; i < jButtons.length; i++ ) 068 { 069 JButton jButton = jButtons [ i ]; 070 071 NullArgumentException.check ( 072 jButton, "jButtons[" + i + "] is null" ); 073 074 gridBagConstraints.gridx = i; 075 076 add ( jButton, gridBagConstraints ); 077 } 078 079 if ( panelBackgroundColor != null ) 080 { 081 setBackground ( panelBackgroundColor ); 082 } 083 } 084 085 /********************************************************************* 086 * this ( jButtons, null ); 087 *********************************************************************/ 088 public ButtonPanel2 ( JButton [ ] jButtons ) 089 ////////////////////////////////////////////////////////////////////// 090 { 091 this ( jButtons, null ); 092 } 093 094 ////////////////////////////////////////////////////////////////////// 095 ////////////////////////////////////////////////////////////////////// 096 }