Package ucb.gui2

Class TopLevel

  • All Implemented Interfaces:
    ActionListener, EventListener

    public class TopLevel
    extends Object
    implements ActionListener
    A top-level window with optional menu bar. A TopLevel primarily exists to contain Widgets. The general technique is to extend TopLevel. Most of the methods here are protected: the intent is that any calls to them are from within your extension class, thus allowing the latter to retain control over its size, menu, etc.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected JFrame frame
      The Swing frame representing this TopLevel object.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected TopLevel​(String title, boolean exitOnClose)
      A new TopLevel with the given TITLE (which window managers typically display on the border).
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void actionPerformed​(ActionEvent e)  
      protected void add​(Widget widget, LayoutSpec layout)
      Add WIDGET, placed according to LAYOUT.
      protected void addButton​(String label, Consumer<String> func, LayoutSpec layout)
      Add a new button displaying LABEL, laid out according to LAYOUT, which when clicked calls FUNC, with the button and LABEL as its arguments.
      protected void addCheckBox​(String label, boolean selected, Consumer<String> func, LayoutSpec layout)
      Add a new check box displaying LABEL, laid out according to LAYOUT, which when clicked calls FUNC, with the button and LABEL as its arguments.
      protected void addLabel​(String text, String id, LayoutSpec layout)
      Add a label that initially displays the text TEXT, placed according to LAYOUT, and identified by the tag ID.
      protected void addLabel​(String text, LayoutSpec layout)
      Add a new, anonymous label that displays the text TEXT, placed according to LAYOUT.
      protected void addMenuButton​(String label, Consumer<String> func)
      Add a new simple menu button labeled LABEL to my menus, which when clicked, invokes FUNC, sending LABEL as argument.
      protected void addMenuCheckBox​(String label, boolean selected, Consumer<String> func)
      Add a check box labeled LABEL to my menus, which when clicked, flips its state and invokes FUNC, sending LABEL as arguments.
      protected void addMenuRadioButton​(String label, String groupName, boolean selected, Consumer<String> func)
      Add a radio button labeled LABEL to my menus, belonging to the group of buttons called GROUPNAME.
      protected void addRadioButton​(String label, String groupName, boolean selected, Consumer<String> func, LayoutSpec layout)
      Add a radio button labeled LABEL, placed according to LAYOUT, belonging to the group of buttons called GROUPNAME.
      protected void addSeparator​(String label)
      Add a separator to the end of the menu labeled LABEL (which must exist) in my menu bar.
      void display​(boolean visible)
      If VISIBLE, display this TopLevel.
      int getHeight()
      Return my current height in pixels.
      String getTextInput​(String message, String title, String type, String init)
      Display a dialog box with message MESSAGE and title TITLE that prompts the user for textual input, with INIT providing the initial value of the text.
      String[] getTextInputs​(String topMessage, String title, String type, int textWidth, String... requests)
      Display a dialog box that requests and returns input from multiple text fields.
      int getWidth()
      Return my current width in pixels.
      protected boolean isSelected​(String label)
      Return true iff the button named LABEL is currently selected.
      protected void select​(String label, boolean val)
      Set isSelected(LABEL) to VAL, if LABEL is a valid button.
      protected void setEnabled​(boolean enable, String... labels)
      Set the enabled status of the buttons labeled LABELS[0], ...
      protected void setLabel​(String id, String text)
      Set the text of the existing label with tag ID to TEXT.
      void setMaximumSize​(int width, int height)
      Set my maximum size to WIDTH x HEIGHT pixels.
      void setMinimumSize​(int width, int height)
      Set my minimum size to WIDTH x HEIGHT pixels.
      void setPreferredFocus​(Widget widget)
      When the focus is in my window, request that WIDGET, which should be one of my components, get the focus.
      void setPreferredSize​(int width, int height)
      Set my preferred size to WIDTH x HEIGHT pixels.
      void showMessage​(String text, String title, String type)
      Display the dismissable message TEXT of type TYPE in a separate dialog window with title TITLE.
      int showOptions​(String message, String title, String type, String deflt, String... labels)
      Display a choice of optional responses, labeled LABELS[0],...,LABELS[n] in a separate dialog box with title TITLE and message MESSAGE.
    • Field Detail

      • frame

        protected final JFrame frame
        The Swing frame representing this TopLevel object.
    • Constructor Detail

      • TopLevel

        protected TopLevel​(String title,
                           boolean exitOnClose)
        A new TopLevel with the given TITLE (which window managers typically display on the border). If EXITONCLOSE, then closing this window exits the application.
    • Method Detail

      • display

        public void display​(boolean visible)
        If VISIBLE, display this TopLevel. Otherwise, make it invisible.
      • setPreferredSize

        public void setPreferredSize​(int width,
                                     int height)
        Set my preferred size to WIDTH x HEIGHT pixels.
      • setMinimumSize

        public void setMinimumSize​(int width,
                                   int height)
        Set my minimum size to WIDTH x HEIGHT pixels.
      • setMaximumSize

        public void setMaximumSize​(int width,
                                   int height)
        Set my maximum size to WIDTH x HEIGHT pixels.
      • getWidth

        public int getWidth()
        Return my current width in pixels.
      • getHeight

        public int getHeight()
        Return my current height in pixels.
      • addMenuButton

        protected void addMenuButton​(String label,
                                     Consumer<String> func)
        Add a new simple menu button labeled LABEL to my menus, which when clicked, invokes FUNC, sending LABEL as argument. A null value of FUNC indicates no action. LABEL has the form MENUNAME->SUBMENU1->...SUBMENUn->NAME, where n >= 0, (for example, "File->Open" or "File->New->Project"). This label denotes "the item labeled NAME in the submenu named SUBMENUn in the ... in the menu-bar entry MENUNAME. The new button appears at the end of its menu. Likewise, any previously non-existing menus get created at the end of the menu bar or their containing menu.
      • addMenuCheckBox

        protected void addMenuCheckBox​(String label,
                                       boolean selected,
                                       Consumer<String> func)
        Add a check box labeled LABEL to my menus, which when clicked, flips its state and invokes FUNC, sending LABEL as arguments. LABEL is as for addMenuButton. The box is initially checked iff CHECKED. A null value of FUNC indicates no action (aside from changing selection state).
      • addMenuRadioButton

        protected void addMenuRadioButton​(String label,
                                          String groupName,
                                          boolean selected,
                                          Consumer<String> func)
        Add a radio button labeled LABEL to my menus, belonging to the group of buttons called GROUPNAME. Initially, the button is selected iff SELECTED. Only one radio button in a group is selected at a time (see isSelected(java.lang.String)); when the user clicks one, it becomes selected and any other button in the group is deselected. If FUNC is non-null, it denotes a function that is invoked when the button is pressed, sending LABEL as its argument.
      • addSeparator

        protected void addSeparator​(String label)
        Add a separator to the end of the menu labeled LABEL (which must exist) in my menu bar. LABEL has the form MENUNAME->SUBMENU1->...->SUBMENUn.
      • isSelected

        protected boolean isSelected​(String label)
        Return true iff the button named LABEL is currently selected.
      • select

        protected void select​(String label,
                              boolean val)
        Set isSelected(LABEL) to VAL, if LABEL is a valid button.
      • setEnabled

        protected void setEnabled​(boolean enable,
                                  String... labels)
        Set the enabled status of the buttons labeled LABELS[0], ... to ENABLE. An ENABLE value of false causes the buttons to become unresponsive, typically displaying as being grayed out.
      • addButton

        protected void addButton​(String label,
                                 Consumer<String> func,
                                 LayoutSpec layout)
        Add a new button displaying LABEL, laid out according to LAYOUT, which when clicked calls FUNC, with the button and LABEL as its arguments.
      • addCheckBox

        protected void addCheckBox​(String label,
                                   boolean selected,
                                   Consumer<String> func,
                                   LayoutSpec layout)
        Add a new check box displaying LABEL, laid out according to LAYOUT, which when clicked calls FUNC, with the button and LABEL as its arguments. It is initially selected iff SELECTED.
      • addRadioButton

        protected void addRadioButton​(String label,
                                      String groupName,
                                      boolean selected,
                                      Consumer<String> func,
                                      LayoutSpec layout)
        Add a radio button labeled LABEL, placed according to LAYOUT, belonging to the group of buttons called GROUPNAME. Initially, the button is selected iff SELECTED. Only one radio button in a group is selected at a time (see isSelected(java.lang.String)); when the user clicks one, it becomes selected and any other button in the group is deselected. If FUNC is non-null, FUNC is invoked when pressed, sending the LABEL as argument.
      • add

        protected void add​(Widget widget,
                           LayoutSpec layout)
        Add WIDGET, placed according to LAYOUT.
      • addLabel

        protected void addLabel​(String text,
                                String id,
                                LayoutSpec layout)
        Add a label that initially displays the text TEXT, placed according to LAYOUT, and identified by the tag ID. If a label with the same ID already exists, its text is altered to TEXT.
      • setLabel

        protected void setLabel​(String id,
                                String text)
        Set the text of the existing label with tag ID to TEXT.
      • addLabel

        protected void addLabel​(String text,
                                LayoutSpec layout)
        Add a new, anonymous label that displays the text TEXT, placed according to LAYOUT.
      • showMessage

        public void showMessage​(String text,
                                String title,
                                String type)
        Display the dismissable message TEXT of type TYPE in a separate dialog window with title TITLE. TYPE may be any of the strings "information", "warning", "error", or "plain", which modify the look of the message.
      • showOptions

        public int showOptions​(String message,
                               String title,
                               String type,
                               String deflt,
                               String... labels)
        Display a choice of optional responses, labeled LABELS[0],...,LABELS[n] in a separate dialog box with title TITLE and message MESSAGE. Returns the selected option (0 -- n), or -1 if the user closes the dialog window. DEFLT is the default label (may be null). TYPE may be "question", "information", "warning", "error", or "plain".
      • getTextInput

        public String getTextInput​(String message,
                                   String title,
                                   String type,
                                   String init)
        Display a dialog box with message MESSAGE and title TITLE that prompts the user for textual input, with INIT providing the initial value of the text. TYPE may be "question", "information", "warning", "error", or "plain". Return the user's input text, or null if the user closes the dialog window.
      • getTextInputs

        public String[] getTextInputs​(String topMessage,
                                      String title,
                                      String type,
                                      int textWidth,
                                      String... requests)
        Display a dialog box that requests and returns input from multiple text fields. TITLE is the title of the dialog window. TOPMESSAGE is a message placed above the input entries. TYPE may be "question", "information", "warning", "error", or "plain", indicating the style of dialog desired. TEXTWIDTH is the common width of the text input fields. REQUESTS is a non-empty, even-length sequence containing pairs label, init, where label gives the text placed to the right of a text input area, and init is that area's initial contents (may be null). Returns the users responses in the order their specifications appear in REQUESTS, or null if the user cancels the request.
      • setPreferredFocus

        public void setPreferredFocus​(Widget widget)
        When the focus is in my window, request that WIDGET, which should be one of my components, get the focus.