Slide navigation: Forward with space bar, → arrow key, or PgDn. Backwards with ← or PgUp.
ActionEvent
, WindowEvent
, ...) carries information about an event.ActionListener listener = new MyListener(); JButton button = new JButton("OK"); button.addActionListener(listener);
class MyListener implements ActionListener { public void actionPerformed(ActionEvent event) { reaction to button click } }
JButton
object:
JButton yellowButton = new JButton("Yellow"); JButton blueButton = new JButton(new ImageIcon("blue-ball.gif"));
JPanel
, and add the panel to the frame:
JPanel buttonPanel = new JPanel(); buttonPanel.add(yellowButton); buttonPanel.add(blueButton); frame.add(buttonPanel);
class ColorAction implements ActionListener { . . . } . . . yellowButton.addActionListener(new ColorAction(Color.YELLOW)); blueButton.addActionListener(new ColorAction(Color.BLUE));
exitButton.addActionListener(event -> System.exit(0));
public void makeButton(String name, Color backgroundColor) { JButton button = new JButton(name); buttonPanel.add(button); button.addActionListener( event -> buttonPanel.setBackground(backgroundColor)); }
makeButton("yellow", Color.YELLOW); makeButton("blue", Color.BLUE);
exitButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { System.exit(0); } });
WindowListener
interface has a separate method for each of them:
void windowOpened(WindowEvent e); void windowClosing(WindowEvent e); void windowClosed(WindowEvent e); . . .
windowClosing
.
WindowAdapter
instead of implementing WindowListener
:
class Terminator extends WindowAdapter { public void windowClosing(WindowEvent e) { if (user agrees) System.exit(0); } }
frame.addWindowListener(new Terminator());
Action
interface combines listener code and action properties.yellowAction.setEnabled(false);
NAME
SMALL_ICON
SHORT_DESCRIPTION
(for tooltips)MNEMONIC_KEY
(for underlined characters in menus)yellowAction.putValue(Action.SHORT_DESCRIPTION, "Set panel color to yellow");
Keystroke
object:
KeyStroke ctrlYKey = KeyStroke.getKeyStroke("ctrl Y");
InputMap imap = panel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); imap.put(ctrlYKey, "panel.yellow");
ActionMap amap = panel.getActionMap(); amap.put("panel.yellow", yellowAction);
MouseListener
reports on mouse events in a component.void mousePressed(MouseEvent event) void mouseReleased(MouseEvent event) void mouseClicked(MouseEvent event)
MouseAdapter
implements all methods to do nothing.MouseEvent
object has methods describing the event:
int getX() int getY() int getClickCount()
if ((event.getModifiersEx() & InputEvent.BUTTON3_DOWN_MASK) != 0)
MouseMotionListener
tracks movement with mouseMoved
and mouseDragged
methods.comp.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
ActionEvent
(button, menu, ...)AdjustmentEvent
(scrollbar)ItemEvent
(list box)KeyEvent
MouseEvent
MouseWheelEvent
FocusEvent
WindowEvent
ActionListener
(button, menu item, combo box, text field, timer)AdjustmentListener
(scroll bar)ItemListener
(combo box)FocusListener
(component)KeyListener
(component)MouseListener
, MouseMotionListener
, MouseWheelListener
(component)WindowListener
, WindowFocusListener
, WindowStateListener
(window)PopupMenuListener
, TreeWillExpandListener