Christopher Budo, Davis Nygren, spencer franks, Luke miller Advanced GUI’s Christopher Budo, Davis Nygren, spencer franks, Luke miller
Goals Familiarization with common interface components Text components Radio buttons Check boxes Menus Understand the use of layout managers to arrange user-interface components Build programs that handle events from user-interface components
Hierarchy of Swing User-Interface Components Big Java 4th Edition Chapter 18 Figure 2
Processing Text Input JTextField JTextArea
JTextFields When you use a JTextField you must give the field a width, or approximation of how many letters the user will type If a user types more characters than accounted for in the width, some characters will become invisible When using a JTextField the user can be told what to type by using a JLabel - a component that holds a string such as “Name: “ telling the user to put a name in the text field A text field fires an action when the enter key is pressed, but can be accessed without the enter key being pressed JTextField textField = new JTextField(FIELD_WIDTH);
JTextArea Holds multiple lines of text When defining an area you must supply the number of rows and columns. JTextArea textArea = new JTextArea(ROWS, COLUMNS);
Methods setText: sets the text of a text field or area textField.setText(“HI”); append: adds text to the end of a text area Note: this only works for a JTextArea not a JTextField textArea.append(account.getBalance() + "\n"); setEditable: enables or disables the user from entering text in a field or area textArea.setEditable(false);
Layout Managers In java you create user interfaces by adding components into containers such as panels Each container has its own layout manager which determines how the components are laid out. Some major layout managers FlowLayout BorderLayout GridLayout
FlowLayout Flow Layout arranges its components from left to right and starts a new row when there is no more room in the current row. Flow layout is the default layout of JPanel
BorderLayout Groups components into five areas Center North South East West Not all of the areas must be occupied Default layout for a frame
GridLayout Big Java 4th Edition Chapter 18 Figure 4 Arranges components in a grid with a fixed number of rows and columns Resizes each of the components so that they have the same size Expands each component to fill the entire area Sample code: JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new GridLayout(4, 3)); buttonPanel.add(button7); buttonPanel.add(button8); buttonPanel.add(button9); buttonPanel.add(button4); … Big Java 4th Edition Chapter 18 Figure 4
Nesting Panels By default, panels don’t have visible borders so you can use as many panels as you need to organize your components. You can mix together layouts by adding new panels into a larger panel such as adding a GridLayout to the center of a BorderLayout Sample code: JPanel keypadPanel = new JPanel(); keypadPanel.setLayout(new BorderLayout()); buttonPanel = new JPanel(); buttonPanel.setLayout(new GridLayout(4, 3)); keypadPanel.add(buttonPanel, BorderLayout.CENTER); JTextField display = new JTextField(); keypadPanel.add(display, BorderLayout.NORTH);
Giving the User Choices Radio Buttons Check Boxes Combo Boxes
Radio Buttons If the choices are mutually exclusive you can use a set of radio buttons When one button is selected all other buttons of the set are turned off To create a set of radio buttons create each button individually and then add all buttons of the set to a ButtonGroup object The Button Group does not place all buttons close to eachother, it only determines which buttons should be turned on or off The isSelected() method is called to determine whether or not a button is currently selected.
Using Multiple Groups If you have multiple button groups it is a good idea to group them together visually to do so you can put each group into a panel and add a border to the panel to make it visible You can also add a title to the border Sample code: JPanel panel = new JPanel(); panel.setBorder(new EtchedBorder()); panel.setBorder(new TitledBorder(new EtchedBorder(), "Size"));
Check Boxes A check box has two states checked and unchecked You can use check boxes for choices that are not exclusive. These are implemented separately unlike radio buttons Constructing a check box: JCheckBox italicCheckBox = new JCheckBox("Italic");
Combo Boxes If you have a large number of choices radio buttons become impractical due to their size. A solution to this problem is the combo box, a combination of a text field and a list When you click on the arrow to the right of the text field of a combo box a list of selections drops down and you can choose one of the items in the list If the combo box is editable you can also type in your own selection You can get the item that the user selected by using the getSelectedItem method which will return a object.
Menus Menu Bar Menu Menu Items Menu Bar Menu Sub-menu Menu Item Big Java 4th Edition Chapter 18 Figure 12
Menu Bar The top-level container of menus Contains the “File”, ”Edit” ,”View” menus common in programs To keep the code readable, it is suggested to use different methods to construct each part of the JMenuBar.
Menu JMenus contain JMenuItems and more JMenus (submenus) JMenus do not create ActionEvents When a use clicks on a menu name, and a submenu opens, no ActionEvent is created
Menu Items A JMenuItem is at the end of a menu/submenu. JMenuItems do create ActionEvents when the user clicks on them
Sources Cay Horstmann, Big Java 4th Edition