You are on page 1of 9

Chapter 3

Algoritma & Struktur Data

Action on Netbeans

Dosen Pengampu : Eka Prasetyaningrum, S.Kom., M.M


TA/ Semester: 2019/2020 – 4A
EVENT LISTENER
• To get the impression of "responsiveness" to user input, a
GUI application built using java language is equipped by an
event handler.
• Event handlers are obtained from the listener interface. The
Listener interface has a method that must be rewritten as a
method that must be run when it gets user input.
Add the event by right-click the
component to which you want to
add the event, then select what
event you want (usually the type of
action with the actionPerformed ()
method).

Or you can also click 2x the component that you want to add the event to. For the (JButton)
button, the event that is formed is action er actionPerformed (). The source code section will
appear in the gui as follows. Programming logic can be written in the method.
Method Swing Components
Some swing components have methods that can be used for programming logic
contained in event handlers. Some of the most commonly used are as follows:
Method Komponen Fungsi
setText(“X”) JButton, JTextfield, JLabel Sets the text that appears on
each of them Component
getText() JButton, JTextfield, JLabel Take the String value found in getSelectedIndex() JComboBox Retrieves the selected index from the
each of each component component. Index starts from 0
setToolTipText(“X”) JButton, JTextfield, JLabel Provides tooltip for
components getSelectedItem() JComboBox Retrieves the selected object from the
setEnabled(false) JButton, JTextfield, Enable a component or not component. Can be directly displayed if
JLabel, JComboBox, (can clicked on or not) the selected object String.
JRadioButton,
JCheckBox
setSelected(true) JRadioButton, JCheckBox Make each one component insertItemAt(objek, indek) JComboBox Enter selected items in the form of a
selected or not type object Objects in the index to
isSelected() JRadioButton, JCheckBox Check whether a component index
is selected or not setValueAt(Objek, Sets a table with an Object value of
JTable
setSelectedIndex(4) JComboBox Make an index of a certain baris, kolom) type Object data in rows and columns
number as a component Certain
Selected
getSelectedRow() JTable Retrieves the table row index selected.
Index starts from 0
getSelectedColumn() JTable Retrieves the index of the selected
table column. Index starts from 0
Dialogical use JOptianPane

On the swing component, this component is called JOptionPane. There are 4 options pane
a) showConfirmDialog : To confirm yes / no / cancel
b) showInputDialog : To display pop-ups as input data
c) showMessageDialog : To display status / message with 1 "OK" button.
d) showOptionDialog : A combination of the three components above.
There is a confirmation section, asking for input and displaying status /
message
showConfirmDialog
JOptionPane.showConfirmDialog(null, "Isi Pesan"); Comparative messages consist
of:
o YES_OPTION:
if you choose the button “YES”
o OK_OPTION:
if you choose the button “OK”
o NO_OPTION:
To retrieve the value of each if you choose the button “NO”
key pressed, use the following o CLOSED_OPTION:
code: if you choose the button “CLOSED”
o CANCEL_OPTION:
if you choose the button “CANCEL”
showInputDialog

JOptionPane.showInputDialog(null,"Input your name


here","Input Dialog",JOptionPane.INFORMATION_MESSAGE);

String[] options = {"Apple","Mango","Grape","Guava"};


JOptionPane.showInputDialog(null,"Choose this one Option","Input
dialog",JOptionPane.WARNING_MESSAGE,null,options,"Apple");
showMessageDialog
Example showMessageDialog Code with 2 parameters:

JOptionPane.showMessageDialog(null, "Isi Pesan");

Example showMessageDialog Code with 4 parameters:

JOptionPane.showMessageDialog(null, "Isi Pesan", "Judul


Pesan", JOptionPane.ERROR_MESSAGE);

The resulting symbol / icon (red cross or the letter "i" in


the circle), depends on the last parameter of the sample
code above (for the 4 parameter code). This parameter is
called the "option type". The types of option types are as
follows:
showOptionDialog
Show a message and get a user option from a set of options.
JOptionPane.showOptionDialog(null                                
  // position the dialog, the default position is center.
, “Message?”                                            // display
Message
, “Title”                                                      // Title in titlebar
, JOptionPane.NO_OPTION             // Option type
, JOptionPane.PLAIN_MESSAGE   // messageType
, null                                                          // Icon (none)
, buttons                                                   // Button text as
above.
, “default”);                                              // Default button’s
label

You might also like