+ All Categories
Home > Documents > Creating User Interfaces F JComponent F JButton F JLabel F JTextField F JTextArea F JComboBox F...

Creating User Interfaces F JComponent F JButton F JLabel F JTextField F JTextArea F JComboBox F...

Date post: 16-Jan-2016
Category:
Upload: dulcie-may
View: 251 times
Download: 0 times
Share this document with a friend
Popular Tags:
51
Creating User Interfaces JComponent JButton JLabel JTextField JTextArea JComboBox JList JCheckBox JRadioButton Dialogs
Transcript
Page 1: Creating User Interfaces F JComponent F JButton F JLabel F JTextField F JTextArea F JComboBox F JList F JCheckBox F JRadioButton F Dialogs.

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

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

Component PropertiesThe Component class is root for all UIcomponents and containers.

List of frequently used properties: font

background

foreground

preferredSize

minimumSize

maximumSize

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

JComponent PropertiesAll but a few Swing components

(such JFrame, JApplet and JDialog)

are subclasses of JComponent.

toolTipText

doubleBuffered (to reduce flickering)

border

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

Buttons

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

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)

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

JButton Properties mnemonic (to specify a shortcut key: Alt+S) icon (image on the button)

Position of text and icon on the button: horizontalAlignment verticalAlignment horizontalTextPosition verticalTextPosition

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

JLabelA label is a display area for a short text, an image, or both. 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)

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

JLabel Methods void setText(String str) String getText()

void setAlignment(int how)SwingConstants.LEFTSwingConstants.CENTERSwingConstants.RIGHT

int getAlignment()

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

JLabel Methods Example: JLabel latLabel =

new JLabel(”Latitude”, SwingConstants.RIGHT);

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

JTextFieldA text field is an input area where the usercan type in characters. Text fields are usefulin that they enable the user to enter in variable data (such as a name or a description).

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

JTextField Constructors JTextField(int columns)

Creates an empty text field with the specified number of columns.

JTextField(String text)

Creates a text field initialized with the specified text.

JTextField(String text, int columns)

Creates a text field initialized with thespecified text and the column size.

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

JTextField Properties text horizontalAlignment editable columns

http://www.cs.joensuu.fi/~koles/utm/utm.html

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

JTextField Methods String getText()

Returns the string from the text field.

setText(String text)Puts the given string in the text field.

void setEditable(boolean editable)Enables or disables the text field to be edited. By default, editable is true.

void setColumns(int)Sets the number of columns in this text field.The length of the text field is changeable.

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

JTextField Example Create JTextField JTextField latTextField = new JTextField("", 30);

panel.add(latTextField);

. . .

Use the JTextField String latString = latTextField.getText();

// double lat = Double.parseDouble(latString);

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

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.

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

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 andthe number of rows and columns specified.

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

JTextArea PropertiestexteditablecolumnslineWrapwrapStyleWordrowslineCounttabSize

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

Example: Using Text Area

String text = “… A text …/n” + “… more text …”;

JTextArea jta = new JTextArea();

jta.setText(text);

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

JComboBoxA 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 To create a choice, use constructors:

JComboBox() JComboBox(Object[] stringItems)

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

JComboBox Methods

To add an item to a JComboBox, use

void addItem(Object item)

To get an item from JComboBox, use

Object getItem()

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

Using theitemStateChanged 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.

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

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]);

}

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

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.

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

JList Constructors JList()

Creates an empty list.

JList(Object[] stringItems)

Creates a new list initialized with items.

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

JList Properties & Methods selectedIndex

selectedIndices

selectedValue

selectedValues

selectionMode: SINGLE_SELECTION, SINGLE_INTERVAL_SELECTION, MULTIPLE_INTERVAL_SELECTION.

int getSelectedIndex() int[] getSelectedIndices()

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

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

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

JCheckBox Constructors

JCheckBox()

JCheckBox(String text)

JCheckBox(String text, boolean selected)

JCheckBox(Icon icon)

JCheckBox(String text, Icon icon)

JCheckBox(String text, Icon icon, boolean

selected)

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

JCheckBox Properties

JCheckBox has all the properties in JButton. Additionally, JCheckBox has the following property:

selected

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

JCheckBoxjchk1 = 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 } } }

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

JRadioButton

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

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

JRadioButton Constructors

JRadioButton()

JRadioButton(String text)

JRadioButton(String text, boolean selected)

JRadioButton(Icon icon)

JRadioButton(String text, Icon icon)

JRadioButton(String text, Icon icon,

boolean selected)

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

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);

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

Grouping Radio Buttons// Create JRadioButtons

JRadioButton jrb1 = JRadioButton(“Radio 1”);

JRadioButton jrb2 = JRadioButton(“Radio 2”, selected);

// Create group of JRadioButtonsButtonGroup jrbg = new ButtonGroup();

jrbg.add(jrb1);

jrbg.add(jrb2);

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

Message 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.

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

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

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

Dialogs

http://java.sun.com/docs/books/http://java.sun.com/docs/books/tutorial/uiswing/components/dialog.htmltutorial/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);

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

ExamplesString str = ”Eggs are not supposed to be green.”;JOptionPane.showMessageDialog(frame, str, “Message”);

JOptionPane.showMessageDialog(frame, str,

“Inane warning”,

JOptionPane.WARNING_MESSAGE);

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

ExamplesString 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);

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

Confirm Dialogint n = JOptionPane.showConfirmDialogshowConfirmDialog(frame,

"Would you like green eggs and ham?",

"An Inane Question",

JOptionPane.YES_NO_OPTION);

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

Input DialogObject[] possibilities = {"ham", "spam", "yam"};

String s = (String)JOptionPane.showInputDialogshowInputDialog(

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.

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

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/uiswinghttp://java.sun.com/docs/books/tutorial/uiswing// components/dialog.htmlcomponents/dialog.html

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

JScrollPane A scroll pane is a component that supports

automatically scrolling without coding.

http://www.cs.joensuu.fi/~koles/edm/edm2.html

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

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.

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.

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

Menu Demo

Menu Bar

Menu Items

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

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:

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

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:

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

The MenuItem Class Individual items of Menu.

MenuItem()

MenuItem(String ItemName)

MenuItem(String ItemName,

MenuShortcut keyAccel)

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

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

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

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

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

3. Register Listener

jmiNew.addActionListener(this);

jmiOpen.addActionListener(this);

jmiPrint.addActionListener(this);

jmiExit.addActionListener(this);

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

4. Implement Handlerpublic 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()


Recommended