This document discusses creating graphical user interfaces (GUIs) in Java using Swing. It introduces JFrame for creating windows, layout managers for positioning components, and common Swing components like JButton and JLabel. It also covers event handling using the delegation model with interfaces like ActionListener, creating menus, dialogs using JOptionPane and JFileChooser, and separating GUI code from business logic by implementing listeners.
3. Creating Windows Windows javax.swing.JFrame – Empty window By default, JFrame is not visible setVisible(true)! Java Look And Feel (Metal) by default
4. Creating JFrame import javax.swing.JFrame; class Testing { public static void main(String [] args) { JFrame mywin = new JFrame(); mywin.setVisible(true); } }
5. Inheritance import javax.swing.JFrame; class MyJFrame extends JFrame { public MyJFrame() { setTitle("My Window!"); } } class Testing { public static void main(String [] args) { MyJFrame mywin = new MyJFrame(); mywin.setVisible(true); } }
7. AWT vs. Swing AWT = Abstract Window Toolkit java.awt.*; Old class library for Java Uses native GUI - components Swing javax.swing.*; From Java 1.2 -> Extends AWT GUI – components 100% Java Pluggable look and feel
8. Layouts UI is build on top of layouts Layout determinates where UI-elements are layed. You can add a layout to Jframe Layouts FlowLayout GridLayout BorderLayout GridBagLAyout CardLayout
9. Layout and Components import javax.swing.*; import java.awt.*; class MyJFrame extends JFrame { public MyJFrame() { setTitle("My Window!"); JButton clickMe = new JButton("Click me!"); FlowLayout layout = new FlowLayout(); setLayout(layout); add(clickMe); } } class Testing { public static void main(String [] args) { MyJFrame mywin = new MyJFrame(); mywin.setVisible(true); } }
14. JPanel It's possible to combine layouts using JPanel JPanel can have it's own layout. JPanel may hold other components JFrame can hold JPanels, that hold components..
15. Combining Layouts JPanel left = new JPanel(); left.setLayout(new GridLayout(2,1)); JPanel right = new JPanel(); right.setLayout(new GridLayout(3,1)); left right setLayout(new Gridlayout(1,2)); add(left); add(right); MyJFrame
16. Combining Layouts public MyJFrame(){ setLayout(new GridLayout(1,2)); JPanel left = new JPanel(); left.setLayout(new GridLayout(2,1)); left.add(new JButton("1")); left.add(new JButton("2")); JPanel right = new JPanel(); right.setLayout(new GridLayout(3,1)); right.add(new JButton("3")); right.add(new JButton("4")); right.add(new JButton("5")); add(left); add(right); }
18. Components UI – components can be added to window (JFrame) using the add() – method All components derive from JComponent UI – components? JButton JLabel JMenuItem JTable JTextArea JTextField...
19. JComponent Some component's common methods setVisible(boolean) setFont(Font) setEnabled(boolean) ... See JComponent API http://java.sun.com/javase/6/docs/api/javax/swing/JComponent.html
20. Components JButton JButton mybutton = new JButton("Click!"); JLabel JLabel mylabel = new JLabel("Some text"); JTextField JTextField myfield = new JTextField(); String text = myfield.getText(); JTextArea...
22. Delegation Event Handling Delegation Event Model: Simple and easy to learn Support a clean separation between application and GUI code Facilitate the creation of robust event handling code which is less error-prone (strong compile-time checking) Flexible enough to enable varied application models for event flow and propagation For visual tool builders, enable run-time discovery of both events that a component generates as well as the events it may observe Support backward binary compatibility with the old model
24. Concepts: Event Source Event source is usually some component that can raise events Examples of event source JButton (button is clicked) JMenuItem JTextField
25. Listener Any class that can handle the events => Any class that implements some interface
27. Recap on Polymorphism interface AbleToMove { public void start(); public void stop(); } class Car implements AbleToMove { public void start() { // do something } public void stop() { // do something } }
28. Recap on Polymorphism class Test { public static void main(String [] args) { Airplane a = new Airplane(); someMethod(a); } public static void someMethod(AbleToMove x) { x.start(); } } You can pass whatever object you desire as long as the object implements AbleToMove interface!
29. Example JButton is a event source. JButton source = new JButton(); When the button is clicked we want that something happens We need a class that listens to the button. We register the source and the listener with each other!
31. Registration JButton holds a method addActionListener public void addActionListener(ActionListener l) So you can call it like JButton source = new JButton("Click!"); source.addActionListener(...); Parameter ActionListener? What is it? It's an interface! This means that you can pass whatever object as long as the object implements the interface!
32. ActionListener So the listener can be what ever object as long as it implements the ActionListener. ActionListener: interface ActionListener { public void actionPerformed(ActionEvent e); }
33. Listener Some class that implements the ActionListener class MyListener implements ActionListener { public void actionPerformed(ActionEvent e) { // do something } }
34. Separation of GUI and BL JButton MyListener addActionListener ActionListener
35. Example Usage // Create the source JButton button = new JButton("click me"); // Create the listener MyListener listener = new MyListener(); // Registration button.addActionListener(listener);
36. Registration Different sources have different methods and interfaces for the registration. Registration: addXXXListener Examples addMouseMotionListener(...) addMouseListener(...) addKeyListener(...)
37. Example: Listening to Mouse in Window // Source JFrame a = new JFrame(); // Listener SomeClass listener = new SomeClass(); // Registration a.addMouseMotionListener(listener);
38. BL and GUI in the same class In small programs it is usual that the GUI - and the application code is implemented in the same class. No the listener is the same class: class MyJFrame extends JFrame implements ActionListener And registration: source.addActionListener(this);
43. About Dialogs JFrame is used for windows, JDialog for dialogs. You can inherit the JDialog. Every dialog needs to know who the host window is dialog belongs to the window
44. Standard Dialogs Really easy way to create dialogs is use standard dialogs JOptionPane JFileChooser JColorChooser ...
45. JOptionPane With one line of code, you can pop up a dialog JOptionPane.showMessageDialog(hostframe, "Hello!"); Also available showConfirmDialog yes/no/cancel showInputDialog prompt user
46. JFileChooser JFileChooser chooser = new JFileChooser(); int returnVal = chooser.showOpenDialog(parent); if(returnVal == JFileChooser.APPROVE_OPTION) String file = chooser.getSelectedFile().getName();