Unit 6 Graphical user interfaces
3. GUI elements
Buttons
• A button is used so that the user can ask for action from a program.
• How to create:
JButton offButton = new JButton(); // without label
JButton onButton = new JButton("Cancel"); //with label
• Setting: To set the label of a button setText
offButton.setText("Press Me");
• Getting: To get the label of a button getText
String offButtonLabel = offButton.getText();
Labels
• A label is just text that can be placed within a container.
• How to create:
JLabel lb = new JLabel("Hello there");
• Setting: To set the text on the label setText
lb.setText("Good Bye");
• Getting: To get the text from a label getText
String currentLabelStr = lb.getText();
16
Unit 6 Graphical user interfaces
3. GUI elements
Check boxes
• Check boxes are used for the input of boolean data.
• How to create:
JCheckBox noviceUserType = new JCheckBox("Novice");
JCheckBox expeUserType = new JCheckBox("Experienced");
• Getting: The state of a check box – whether or not it is on – can be discovered by means of the method
isSelected, which returns a boolean result.
boolean b = expeUserType.isSelected();
17
Unit 6 Graphical user interfaces
3. GUI elements
Radio buttons
• Radio buttons allow the user to choose one of a list of choices.
• Radio buttons are grouped together and have the property that only one of the buttons can be selected at a
time.
• How to create:
// (1) Create the buttons
JRadioButton fr = new JRadioButton("French", true); //initially selected
JRadioButton en = new JRadioButton("English", false);
// (2) Group the buttons
ButtonGroup languageGroup = new ButtonGroup();
languageGroup.add(fr);
languageGroup.add(en);
Note:
when you add elements to the JFrame (or JPanel), you must add the RadioButton objects, not the ButtonGroup
object. The aim of using a ButtonGroup object is to tell Java that a set of buttons are are grouped together and
have the property that only one of the buttons can be selected at a time.
18
Unit 6 Graphical user interfaces
3. GUI elements
Combo boxes (drop-down lists)
• A combo box is a drop-down list that allows the programmer to specify a number of strings, one
of which can be selected.
• How to create:
// (1) Create the ComboBox
JComboBox computerChoice = new JComboBox();
// (2) Add items to the ComboBox
computerChoice.addItem("VAX");
computerChoice.addItem("PC");
computerChoice.addItem("Mac");
computerChoice.setSelectedItem("VAX"); //initially selected
• Getting: using a method to return the string of the currently selected item.
String s = computerChoice.getSelectedItem();
This results in the string s being set to the string "VAX".
19
Unit 6 Graphical user interfaces
3. GUI elements
Lists
• A list of items where the user can select a single string or a number of strings.
• How to create: the simplest way to create a list is…
// (1) Create an array containing the choices available
String []data = {"brendan", "anton", "barbara", "martin"};
//(2) then to pass this array to the JList constructor
JList nameList = new JList(data);
• Getting: The most important methods are…
String[] getSelectedValues()
returns a string array that contains the names of the items that have been selected.
• Example: String [] names = nameList.getSelectedValues(); places the strings in the
data array that have been selected by the user in the string array names.
int[] getSelectedIndices()
returns an array of all the selected indices in increasing order.
String getElementAt(int)
returns the string that can be found at the specified index.
20
Unit 6 Graphical user interfaces
3. GUI elements
Text fields
• Text fields are used for the input of small items of textual data.
• How to create:
JTextField txfA = new JTextField(); // empty textbox
JTextField txfB = new JTextField(20); // empty, width = 20 characters
JTextField txfC = new JTextField("Type here"); // not empty
JTextField txfD = new JTextField("Type here", 20); // not empty, width=20
• Setting: txfA.setText(“Type here");
• Getting: String t = txfA.getText();
21
Unit 6 Graphical user interfaces
3. GUI elements
Text areas
• A text area is used for entering comparatively large amounts of textual data.
• containing a number of lines of text, rather than the single line found in a text field.
• Both the class JTextArea and the class JTextField inherit from a class known as
JTextComponent.
• How to create:
JTextArea ta = new JTextArea (4, 20); // empty, 4 rows, 20 columns
JTextArea ta = new JTextArea ("line1\nline2", 4, 20);//not empty, 4 r, 20 c
• Setting & Getting
• same as textbox (setText(), getText())
22
Unit 6 Graphical user interfaces
4. JOptionPane Class
• JOptionPane makes it easy to pop up a standard dialog box that prompts users for a
value or informs them of something.
• The JOptionPane class is part of the javax.swing library.
• Three of the dialog boxes are:
• Message Dialog - Displays a message (left figure).
• Input Dialog - Prompt for some input. (middle figure).
• Confirm Dialog – Asks a confirming question, like yes/no/cancel (right figure)
Some JOptionPane methods to use the above dialogues are:
24
Unit 6 Graphical user interfaces
4. JOptionPane Class
The parameters to the JOptionPane’s methods follow consistent patterns:
• parentComponent: Defines the Component that is to be the parent of this dialog box. It is
used in two ways:
1. the Frame that contains it is used as the Frame parent for the dialog box, and its screen
coordinates are used in the placement of the dialog box.
2. This parameter may be null, in which case a default Frame is used as the parent, and
the dialog will be centered on the screen.
• Message: A descriptive message to be placed in the dialog box. In the most common usage,
message is just a String.
• title: The title for the dialog box.
• Option type, one of DEFAULT_OPTION, YES_NO_OPTION, YES_NO_CANCEL_OPTION or
OK_CANCEL_OPTION.
Unit 6 Graphical user interfaces
4. JOptionPane Class
Examples
JOptionPane.showMessageDialog(null, "Hello World");
JOptionPane.showMessageDialog(null, "alert", "alert",
JOptionPane.ERROR_MESSAGE);
String s = JOptionPane.showInputDialog("Please input a value");
Unit 6 Graphical user interfaces
4. JOptionPane
Examples
Class