You are on page 1of 7

Java GUIs Programming #3

- Other GUI components

JOptionPane Dialogs
— A dialog is normally used as a temporary window to

receive additional information from the user, or to provide notification that some event has occurred.

2

INT105: Computer Programming II

Message Dialogs A message dialog box simply displays a message to alert the user and waits for the user to click the OK button to close the dialog. 3 INT105: Computer Programming II Message Types The messageType is one of the following constants: JOptionPane.ERROR_MESSAGE JOptionPane.PLAIN_MESSAGE JOptionPane.QUESTION_MESSAGE 4 INT105: Computer Programming II .WARNING_MESSAGE JOptionPane.INFORMATION_MESSAGE JOptionPane.

cont. 5 INT105: Computer Programming II Confirmation Dialogs A message dialog box displays a message and waits for the user to click the OK button to dismiss the dialog. The confirmation dialog returns a value that corresponds to a selected button. The message dialog does not return any value. 6 INT105: Computer Programming II . A confirmation dialog asks a question and requires the user to respond with an appropriate button.Message Types.

JMenuItem. — A JFrame or JApplet can hold a menu bar to which the pull-down menus are attached.Menus — Java provides several classes—JMenuBar. JCheckBoxMenuItem. Menu bars can be viewed as a structure to support menus. Menus consist of menu items that the user can select (or toggle on or off). JMenu. 7 INT105: Computer Programming II Menu Demo 8 INT105: Computer Programming II . and JRadioButtonMenuItem —to implement menus in a frame.

false). The following code creates two menus. f.setSize(300.add(helpMenu). and adds them to the JMenuBar mb: JMenu fileMenu = new JMenu("File". 200). the menu bar can only be added to a frame. File and Help. true).add(fileMenu).setJMenuBar(mb). mb. JMenu helpMenu = new JMenu("Help". 9 INT105: Computer Programming II The Menu Class You attach menus onto a JMenuBar.The JMenuBar Class A menu bar holds menus. Following is the code to create and add a JMenuBar to a frame: JFrame f = new JFrame(). f. JMenuBar mb = new JMenuBar(). f. 10 INT105: Computer Programming II .setVisible(true). mb.

helpMenu.The JMenuItem Class You add menu items on a menu. 11 INT105: Computer Programming II Submenus You can add submenus into menu items.add(new JMenuItem("print")). fileMenu. fileMenu.” “NT.add(new JMenuItem("Win95")).” JMenu softwareHelpSubMenu = new JMenu("Software").add(new JMenuItem("Unix")). 12 INT105: Computer Programming II . fileMenu.addSeparator() .” and “Win95” into the menu item “Software.addSeparator().add(softwareHelpSubMenu).add(new JMenuItem("exit")). softwareHelpSubMenu.add(new JMenuItem("NT")).add(new JMenuItem("open")). softwareHelpSubMenu. The following code adds menu items and item separators in menu fileMenu: fileMenu. helpMenu.add(hardwareHelpSubMenu). JMenu hardwareHelpSubMenu = new JMenu("Hardware"). fileMenu. The following code adds the submenus “Unix.add(new JMenuItem("new")). softwareHelpSubMenu. fileMenu.

MouseWheelListener) MouseMotionAdapter (implements MouseMotionListener) WindowAdapter (implements WindowFocusListener. WindowListener.ActiveEvent) — ItemEvent — TextEvent FocusListener) HierarchyBoundsAdapter (implements HierarchyBoundsListener) KeyAdapter (implements KeyListener) MouseAdapter (implements MouseListener.AWTEvent — ActionEvent — AdjustmentEvent — ComponentEvent — ContainerEvent — FocusEvent — InputEvent — KeyEvent — MouseEvent o MouseWheelEvent — PaintEvent — WindowEvent — HierarchyEvent — InputMethodEvent — InvocationEvent (implements — FocusAdapter (implements — — — — — java.Submenu Demo 13 INT105: Computer Programming II Event Listener — java.awt.awt. MouseMotionListener. WindowStateListener) 14 INT105: Computer Programming II .