Presentation is loading. Please wait.

Presentation is loading. Please wait.

Creating User Interfaces F JComponent F JButton F JLabel F JTextField F JTextArea F JComboBox F JList F JCheckBox F JRadioButton F Dialogs.

Similar presentations


Presentation on theme: "Creating User Interfaces F JComponent F JButton F JLabel F JTextField F JTextArea F JComboBox F JList F JCheckBox F JRadioButton F Dialogs."— Presentation transcript:

1 Creating User Interfaces F JComponent F JButton F JLabel F JTextField F JTextArea F JComboBox F JList F JCheckBox F JRadioButton F Dialogs

2 Component Properties The Component class is root for all UI components and containers. List of frequently used properties: F font F background F foreground F preferredSize F minimumSize F maximumSize

3 JComponent Properties All but a few Swing components ( such JFrame, JApplet and JDialog ) are subclasses of JComponent. F toolTipText  doubleBuffered (to reduce flickering) F border

4 Buttons

5 JButton A button is a component that triggers an Action Event when clicked. The following are JButton non-default constructors: JButton(String text) JButton(String text, Icon icon) JButton(Icon icon)

6 JButton Properties  mnemonic (to specify a shortcut key: Alt+S)  icon ( image on the button ) Position of text and icon on the button: F horizontalAlignment F verticalAlignment F horizontalTextPosition F verticalTextPosition

7 JLabel  A label is a display area for a short text, an image, or both. F The non-default constructors for labels are as follows: JLabel(String text, int horizontalAlignment) JLabel(String text) JLabel(Icon icon) JLabel(Icon icon, int horizontalAlignment)

8 JLabel Methods F void setText(String str) F String getText() F void setAlignment(int how) SwingConstants.LEFT SwingConstants.CENTER SwingConstants.RIGHT F int getAlignment()

9 JLabel Methods F Example: JLabel latLabel = new JLabel(”Latitude”, SwingConstants.RIGHT);

10 JTextField F A text field is an input area where the user can type in characters. Text fields are useful in that they enable the user to enter in variable data (such as a name or a description).

11 JTextField Constructors F JTextField(int columns) Creates an empty text field with the specified number of columns. F JTextField(String text) Creates a text field initialized with the specified text.  JTextField(String text, int columns) Creates a text field initialized with the specified text and the column size.

12 JTextField Properties F text F horizontalAlignment F editable F columns http://www.cs.joensuu.fi/~koles/utm/utm.html

13 JTextField Methods F String getText() Returns the string from the text field. F setText(String text) Puts the given string in the text field. F void setEditable(boolean editable) Enables or disables the text field to be edited. By default, editable is true. F void setColumns(int) Sets the number of columns in this text field. The length of the text field is changeable.

14 JTextField Example F Create JTextField JTextField latTextField = new JTextField("", 30); panel.add(latTextField);... F Use the JTextField String latString = latTextField.getText(); // double lat = Double.parseDouble(latString);

15 JTextArea If you want to let the user enter multiple lines of text, you cannot use text fields unless you create several of them. The solution is to use JTextArea, which enables the user to enter multiple lines of text.

16 JTextArea Constructors  JTextArea(int rows, int columns) Creates a text area with the specified number of rows and columns.  JTextArea(String s, int rows, int columns) Creates a text area with the initial text and the number of rows and columns specified.

17 JTextArea Properties  text F editable F columns F lineWrap F wrapStyleWord F rows F lineCount F tabSize

18 Example: Using Text Area String text = “… A text …/n” + “… more text …”; JTextArea jta = new JTextArea(); jta.setText(text);

19 JComboBox F A combo box is a simple list of items from which the user can choose. It performs basically the same function as a list, but can get only one value. Also known as choice or drop-down menu F To create a choice, use constructors: JComboBox() JComboBox(Object[] stringItems)

20 JComboBox Methods  To add an item to a JComboBox, use void addItem(Object item)  To get an item from JComboBox, use Object getItem()

21 Using the itemStateChanged Handler  JComboBox can generate ActionEvent and ItemEvent.  When a choice is checked or unchecked, itemStateChanged() for ItemEvent is invoked as well as the actionPerformed() handler for ActionEvent.

22 Example: JComboBox String itemString[]={”Item 1”, ”Item 2”, ”Item 3”}; // Create JComboBox JComboBox jcbo = new JComboBox(itemsString); // Register Listener jcbo.addItemListener(this); public void itemStateChanged(ItemEvent e) { int i = jcbo.getSelectedIndex(); //do something System.out.println(“Index is ” + i); System.out.println(“Item is ” + itemString[i]); }

23 JList A list is a component that performs basically the same function as a combo box, but it enables the user to choose a single value or multiple values.

24 JList Constructors  JList() Creates an empty list. F JList(Object[] stringItems) Creates a new list initialized with items.

25 JList Properties & Methods F selectedIndex F selectedIndices F selectedValue F selectedValues F selectionMode: SINGLE_SELECTION, SINGLE_INTERVAL_SELECTION, MULTIPLE_INTERVAL_SELECTION. F int getSelectedIndex() F int[] getSelectedIndices()

26 JCheckBox A check box is a component that enables the user to toggle a choice on or off, like a light switch.

27 JCheckBox Constructors F JCheckBox() F JCheckBox(String text) F JCheckBox(String text, boolean selected) F JCheckBox(Icon icon) F JCheckBox(String text, Icon icon) F JCheckBox(String text, Icon icon, boolean selected)

28 JCheckBox Properties  JCheckBox has all the properties in JButton.  Additionally, JCheckBox has the following property: selected

29 JCheckBox jchk1 = new JCheckBox(“Check 1”, FALSE); jchk2 = new JCheckBox(“Check 2”, TRUE); jchk1.addItemListener(); jchk2.addItemListener();... public void itemStateChanged(ItemEvent e) { int i; if(e.getSource() instanceof JCheckBox) { if(jchk1.isSelected()) { // do something } if(jchk2.isSelected()) { // do something else }

30 JRadioButton F Radio buttons are variations of check boxes. F They are often used in the group, where only one button is checked at a time.

31 JRadioButton Constructors F JRadioButton() F JRadioButton(String text) F JRadioButton(String text, boolean selected) F JRadioButton(Icon icon) F JRadioButton(String text, Icon icon) F JRadioButton(String text, Icon icon, boolean selected)

32 JRadioButton Properties  JRadioButton has all the properties in JButton.  Additionally, JRadioButton has the following property: selected Example: JRadioButton jrb1 = JRadioButton(“Radio 1”); JRadioButton jrb2 = JRadioButton(“Radio 2”, selected);

33 Grouping Radio Buttons // Create JRadioButtons JRadioButton jrb1 = JRadioButton(“Radio 1”); JRadioButton jrb2 = JRadioButton(“Radio 2”, selected); // Create group of JRadioButtons ButtonGroup jrbg = new ButtonGroup(); jrbg.add(jrb1); jrbg.add(jrb2);

34 Message Dialogs F 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.

35 Creating Message Dialogs Use static method in JOptionPane class. F showMessageDialog( Component parentComponent, Object message, String title, int messageType) F showMessageDialog( Component parentComponent, Object message, String title, int messageType, Icon icon)

36 Dialogs F http://java.sun.com/docs/books/tutorial /uiswing/components/dialog.html Example: Message dialog with default title and icon: String str = ”Eggs are not supposed to be green.”; JOptionPane.showMessageDialog(frame, str);

37 Examples String str = ”Eggs are not supposed to be green.”; JOptionPane.showMessageDialog(frame, str, “Message”); JOptionPane.showMessageDialog(frame, str, “Inane warning”, JOptionPane.WARNING_MESSAGE);

38 Examples String str = ”Eggs are not supposed to be green.”; JOptionPane.showMessageDialog(frame, str, “Inane error”, JOptionPane.ERROR_MESSAGE); JOptionPane.showMessageDialog(frame, str, “A plain message”, JOptionPane.A_PLAIN_MESSAGE);

39 Confirm Dialog showConfirmDialog int n = JOptionPane.showConfirmDialog(frame, "Would you like green eggs and ham?", "An Inane Question", JOptionPane.YES_NO_OPTION);

40 Input Dialog Object[] possibilities = {"ham", "spam", "yam"}; showInputDialog String s = (String)JOptionPane.showInputDialog( frame, "Complete the sentence:\n” + ”\"Green eggs and...\"", "Customized Dialog", JOptionPane.PLAIN_MESSAGE, icon, possibilities,"ham"); //If a string was returned, say so. if ((s != null) && (s.length() > 0)) { setLabel("Green eggs and... " + s + "!"); return; } //If you're here, the return value was null/empty. setLabel("Come on, finish the sentence!"); int n = JOptionPane.

41 Dialogs  As the previous code snippets showed, the showMessageDialog, showConfirmDialog, and showOptionDialog methods return an integer indicating the user's choice.  The values for this integer are YES_OPTION, NO_OPTION, CANCEL_OPTION, OK_OPTION, and CLOSED_OPTION. Each option corresponds to the button the user pressed.  Exception: when CLOSED_OPTION is returned, it indicates that the user closed the dialog window explicitly, rather than by choosing a button inside the option pane. http://java.sun.com/docs/books/tutorial/uiswing/ http://java.sun.com/docs/books/tutorial/uiswing/ components/dialog.html components/dialog.html

42 JScrollPane F A scroll pane is a component that supports automatically scrolling without coding. http://www.cs.joensuu.fi/~koles/edm/edm2.html

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

44 Menu Demo Menu Bar Menu Items

45 The JMenuBar Class JFrame frame = new JFrame(); frame.setSize(300, 200); frame.setVisible(true); JMenuBar mb = new JMenuBar(); frame.setJMenuBar(mb); A menu bar holds menus; the menu bar can only be added to a frame. Example: Create and add a JMenuBar to a frame:

46 The Menu Class JMenu fileMenu = new JMenu("File", false); JMenu helpMenu = new JMenu("Help", true); mb.add(fileMenu); mb.add(helpMenu); Attach menus onto a JMenuBar. Example: create two menus, File and Help, and add them to the JMenuBar mb :

47 The MenuItem Class  Individual items of Menu. MenuItem() MenuItem(String ItemName) MenuItem(String ItemName, MenuShortcut keyAccel)

48 1. Create Menu Items JMenuItem jmiNew = new JMenuItem("new"); JMenuItem jmiOpen = new JMenuItem("open"); JMenuItem jmiPrint= new JMenuItem("print"); JMenuItem jmiExit = new JMenuItem("exit");

49 2. Add Menu Items fileMenu.add(jmiNew); fileMenu.add(jmiOpen); fileMenu.add(new JMenuItem("-")); // separator fileMenu.add(jmiPrint); fileMenu.add(jmiExit); fileMenu.add(new JMenuItem("-")); // separator

50 3. Register Listener jmiNew.addActionListener(this); jmiOpen.addActionListener(this); jmiPrint.addActionListener(this); jmiExit.addActionListener(this);

51 4. Implement Handler public void actionPerformed(ActionEvent e) { String actionCommand = e.getActinCommand(); if(e.getSource() isntanceof JMenuItem) { if(“New”.equals(actionCommand)) { // DO IT! } else if(“Open”.equals(actionCommand)) { // DO IT! } else if(“Print”.equals(actionCommand)) { // DO IT! } else if(“Exit”.equals(actionCommand)) { System.out(0); } }//e.getSource() }//actionPerformed()


Download ppt "Creating User Interfaces F JComponent F JButton F JLabel F JTextField F JTextArea F JComboBox F JList F JCheckBox F JRadioButton F Dialogs."

Similar presentations


Ads by Google