1 30 31 package com.jgoodies.forms.tutorial.building; 32 33 import java.awt.Component ; 34 35 import javax.swing.*; 36 37 import com.jgoodies.forms.builder.ButtonBarBuilder; 38 import com.jgoodies.forms.factories.Borders; 39 import com.jgoodies.forms.layout.CellConstraints; 40 import com.jgoodies.forms.layout.FormLayout; 41 42 50 public final class ButtonBarsExample { 51 52 public static void main(String [] args) { 53 try { 54 UIManager.setLookAndFeel("com.jgoodies.plaf.plastic.PlasticXPLookAndFeel"); 55 } catch (Exception e) { 56 } 58 JFrame frame = new JFrame(); 59 frame.setTitle("Forms Tutorial :: Button Bars"); 60 frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); 61 JComponent panel = new ButtonBarsExample().buildPanel(); 62 frame.getContentPane().add(panel); 63 frame.pack(); 64 frame.show(); 65 } 66 67 68 public JComponent buildPanel() { 69 JTabbedPane tabbedPane = new JTabbedPane(); 70 tabbedPane.putClientProperty("jgoodies.noContentBorder", Boolean.TRUE); 71 72 tabbedPane.add(buildButtonBar1Panel(), "No Builder"); 73 tabbedPane.add(buildButtonBar2Panel(), "With Builder"); 74 tabbedPane.add(buildButtonBar3Panel(), "Related"); 75 tabbedPane.add(buildButtonBar4Panel(), "Unrelated "); 76 tabbedPane.add(buildButtonMixedBar1Panel(), "Mix 1"); 77 tabbedPane.add(buildButtonMixedBar2Panel(), "Mix 2"); 78 return tabbedPane; 79 } 80 81 private Component buildButtonBar1Panel() { 82 JPanel buttonBar = new JPanel( 83 new FormLayout("0:grow, p, 4px, p", "p")); 84 buttonBar.add(new JButton("Yes"), "2, 1"); 85 buttonBar.add(new JButton("No"), "4, 1"); 86 87 return wrap(buttonBar, 88 "\nThis bar has been built without a ButtonBarBuilder.\n" + 89 " o the buttons have no minimum widths and\n" + 90 " o gaps may be inconsistent between team members."); 91 } 92 93 private Component buildButtonBar2Panel() { 94 ButtonBarBuilder builder = new ButtonBarBuilder(); 95 builder.addGlue(); 96 builder.addGridded(new JButton("Yes")); 97 builder.addRelatedGap(); 98 builder.addGridded(new JButton("No")); 99 100 return wrap(builder.getPanel(), 101 "\nThis bar has been built with a ButtonBarBuilder.\n" + 102 " o The buttons have a minimum widths and\n" + 103 " o the button gap is a logical size that follows a style guide."); 104 } 105 106 private Component buildButtonBar3Panel() { 107 ButtonBarBuilder builder = new ButtonBarBuilder(); 108 builder.addGlue(); 109 builder.addGridded(new JButton("Related")); 110 builder.addRelatedGap(); 111 builder.addGridded(new JButton("Related")); 112 builder.addRelatedGap(); 113 builder.addGridded(new JButton("Related")); 114 115 return wrap(builder.getPanel(), 116 "\nThis bar uses the logical gap for related buttons.\n"); 117 } 118 119 private Component buildButtonBar4Panel() { 120 ButtonBarBuilder builder = new ButtonBarBuilder(); 121 builder.addGlue(); 122 builder.addGridded(new JButton("Unrelated")); 123 builder.addUnrelatedGap(); 124 builder.addGridded(new JButton("Unrelated")); 125 builder.addUnrelatedGap(); 126 builder.addGridded(new JButton("Unrelated")); 127 128 return wrap(builder.getPanel(), 129 "\nThis bar uses the logical gap for unrelated buttons.\n" + 130 "It is a little bit wider than the related gap."); 131 } 132 133 private Component buildButtonMixedBar1Panel() { 134 ButtonBarBuilder builder = new ButtonBarBuilder(); 135 builder.addGridded(new JButton("Help")); 136 builder.addGlue(); 137 builder.addUnrelatedGap(); 138 builder.addFixed(new JButton("Copy to Clipboard")); 139 builder.addUnrelatedGap(); 140 builder.addGridded(new JButton("OK")); 141 builder.addRelatedGap(); 142 builder.addGridded(new JButton("Cancel")); 143 144 return wrap(builder.getPanel(), 145 "\nDemonstrates a glue (between Help and the rest),\n" + 146 "has related and unrelated buttons and an ungridded button\n" + 147 "with a default margin (Copy to Clipboard)."); 148 } 149 150 private Component buildButtonMixedBar2Panel() { 151 ButtonBarBuilder builder = new ButtonBarBuilder(); 152 builder.addGridded(new JButton("Help")); 153 builder.addGlue(); 154 builder.addUnrelatedGap(); 155 builder.addFixedNarrow(new JButton("Copy to Clipboard")); 156 builder.addUnrelatedGap(); 157 builder.addGridded(new JButton("OK")); 158 builder.addRelatedGap(); 159 builder.addGridded(new JButton("Cancel")); 160 161 return wrap(builder.getPanel(), 162 "\nDemonstrates a glue (between Help and the rest),\n" + 163 "has related and unrelated buttons and an ungridded button\n" + 164 "with a narrow margin (Copy to Clipboard)."); 165 } 166 167 168 170 private Component wrap(Component buttonBar, String text) { 171 Component textPane = new JScrollPane(new JTextArea(text)); 172 173 FormLayout layout = new FormLayout( 174 "fill:default:grow", 175 "fill:p:grow, 4dlu, p"); 176 JPanel panel = new JPanel(layout); 177 CellConstraints cc = new CellConstraints(); 178 panel.setBorder(Borders.DIALOG_BORDER); 179 panel.add(textPane, cc.xy(1, 1)); 180 panel.add(buttonBar, cc.xy(1, 3)); 181 return panel; 182 } 183 184 } 185 186 | Popular Tags |