package examples; import ise.java.awt.*; import java.awt.*; import java.applet.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; /* This displays a "find" dialog as is shown in Figure 30 in the Java Look and Feel Design Guidelines. This class shows how to use KappaLayout to create this layout while conforming exactly to the guidelines. */ public class Figure30 extends Applet { public Figure30() { // set up a JFrame final JFrame f = new JFrame("Figure30"); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent we) { f.dispose(); } }); /* create the content pane and its layout manager. Use our own JPanel rather than the default container so we can set a javax.swing.border and use a KappaLayout rather than the default BorderLayout (the default content pane for a JFrame uses a BorderLayout. In fact, the default content pane is a JPanel, but would need to be cast as such to be useable as a JPanel.) */ KappaLayout kl = new KappaLayout(); JPanel contents = new JPanel(kl); f.setContentPane(contents); /* set an EmptyBorder on our content panel to create the desired blank space around the contained components. See text accompanying Figure 76 in the Java Look and Feel Guidelines -- the guideline is 12 pixels spacing on top and left, 11 pixels on right and bottom. The EmptyBorder does this nicely. An alternate method (suitable for AWT only layouts) is to override the getInsets method. Panel contents = new Panel(kl) { Insets insets = new Insets(12, 12, 11, 11); public Insets getInsets() { return insets; } }; KappaLayout is one of the few layout managers that respect the insets of a container. */ contents.setBorder(new EmptyBorder(12, 11, 11, 12)); /* add the components, following the design grid. Constraint string follows this pattern: "x, y, w, h, a, s, p" where x is start column of component y is start row of component w is width of component in columns h is height of component in rows a is alignment in cell s is stretch parameter p is padding around component in pixels All parameters have defaults and all parameters are optional. See KappaLayout documentation for details of the constraint string. */ // align label to right side of cell contents.add(new JLabel("Find:"), "0, 0, 1, 1, E"); // stretch text field to fit across 3 columns contents.add(new JTextField(), "2, 0, 3, 1, 0, w"); // align check boxes and radio buttons to left side of cell contents.add(new JCheckBox("Match Case"), "2, 2, 1, 1, W"); contents.add(new JCheckBox("Whole Word"), "2, 3, 1, 1, W"); contents.add(new JRadioButton("Start at Top"), "4, 2, 1, 1, W"); contents.add(new JRadioButton("Wrap Around"), "4, 3, 1, 1, W"); /* add some struts to ensure fixed separation between components follows the guidelines: 11 pixels between groups, 17 between bottom group and command buttons. */ // strut to separate textfield and top checkbox contents.add(KappaLayout.createVerticalStrut(11, true), "0, 1"); // strut to separate "Find:" label and textfield contents.add(KappaLayout.createHorizontalStrut(11, true), "1, 1"); // strut to separate checkbox column and radio button column contents.add(KappaLayout.createHorizontalStrut(11, true), "3, 1"); // strut to separate bottom checkbox and command buttons contents.add(KappaLayout.createVerticalStrut(17, true), "0, 4"); /* use a separate panel for the buttons to properly align the buttons across the correct grids. This panel can stretch to the full width of the dialog, so will fit regardless of the text on the buttons. */ KappaLayout kl2 = new KappaLayout(); JPanel btn_panel = new JPanel(kl2); // stretch button to fill full width of cell btn_panel.add(new JButton("Find"), "0, 0, 1, 1, , w"); // guidelines say to use 5 pixels between buttons btn_panel.add(KappaLayout.createHorizontalStrut(5), "1, 0, 1, 1"); // stretch button to fill full width of cell btn_panel.add(new JButton("Close"), "2, 0, 1, 1, , w"); /* make the 2 columns holding the buttons the same width. KappaLayout finds the button with the widest preferred width, and uses that width for both columns. The 'w' parameter used in laying out the buttons lets the button with the smaller width of the 2 stretch to be the same size. This is exactly the layout specified by the guidelines. */ kl2.makeColumnsSameWidth(0, 2); /* add the button panel, Allow the panel to fill the full width of the dialog if needed, but align it to the right side and bottom of the dialog as required by the guidelines */ contents.add(btn_panel, "0, 5, 5, 1, 4"); /* pack and show -- this is a 'perfect' dialog, that is, all components are laid out at their preferred size (or, in the case of the buttons, at the same size as the largest preferred size in the button group), inter-component spacing is fixed, and there is no need to stretch any component when resizing the dialog, so set the dialog to disallow resizing. */ f.setResizable(false); f.pack(); f.show(); } }