CSC 1214: Object-Oriented Programming J. Kizito Makerere University e-mail: jkizito@cis.mak.ac.ug www: http://serval.ug/~jona materials: http://serval.ug/~jona/materials/csc1214 e-learning environment: http://muele.mak.ac.ug office: block A, level 3, department of computer science alt. office: institute of open, distance, and elearning, room 2 Applets, Applications, and Swing Kizito (Makerere University) CSC 1214 April, 2018 1 / 18
Overview 1 Applets Introduction Applet Fundamentals 2 Swing The JApplet class Swing Constructors Swing Applet Example 3 GUI Applications Kizito (Makerere University) CSC 1214 April, 2018 2 / 18
Applets Introduction Applets Introduction Previous examples have been Java applications The other type of Java program is the Applet Applets are small applications that are accessed on an Internet server and run as part of a Web document They are used to provide interactive features to web applications that cannot be provided by HTML alone They differ from applications in several key areas They can capture mouse input and also have controls like buttons or check boxes They are also used to create online game collections They are usually written in Java but other languages such as Jython, JRuby, Scala or Eiffel may be used as well Kizito (Makerere University) CSC 1214 April, 2018 3 / 18
Applets Introduction Applets A Simple Applet import java.awt.*; import java.applet.*; public class SimpleApplet extends Applet { public void paint(graphics g) { g.drawstring("a Simple Applet", 20, 20); Output The look depends on your OS and Window Manager Kizito (Makerere University) CSC 1214 April, 2018 4 / 18
Applets Applet Fundamentals Applets Fundamentals The import statement java.awt.* imports the Abstract Windowing Toolkit (AWT) classes The class Applet is contained in the package java.applet Every applet must be a subclass of Applet paint() is a method defined by the AWT, has one parameter of type Graphics, and is called each time the applet must re-display its output The Graphics class defines a method, drawstring() with the following general form: void drawstring(string message, int x, int y) Applet code is compiled in the same way as programs However, running the applet involves a different process There are two ways: 1 Execute the applet within a Web browser 2 Use an applet viewer (e.g., appletviewer) Kizito (Makerere University) CSC 1214 April, 2018 5 / 18
Applets Applet Fundamentals Applet Fundamentals Using a Web browser Write a short HTML file containing the appropriate APPLET tag Example: <applet code="simpleapplet" width=200 height=60> </applet> Then execute your browser and load this file, which causes SimpleApplet to be executed The location of the code option should be relative to the HTML file s. In this case, SimpleApplet.class resides in the same folder as the HTML (usually.html) The width and height statements specify the dimensions of the display area used by the applet. (The tag contains several other options though) Kizito (Makerere University) CSC 1214 April, 2018 6 / 18
Applets Applet Fundamentals Using an applet viewer Option 1 Execute the HTML file (say RunApp.html) as follows: :> appletviewer RunApp.html Option 2 Include a comment at the head of your Java source file containing the APPLET tag and start the viewer with the Java source file 1 import java.awt.*; import java.applet.*; /* <applet code="simpleapplet" width=200 height=60> </applet> */ public class SimpleApplet extends Applet { public void paint(graphics g) { g.drawstring("a Simple Applet", 20, 20); 2 :> appletviewer SimpleApplet.java In summary 1 Edit a Java source file 2 Compile your program 3 Execute the applet viewer (specifying the name of your applet s source file) Kizito (Makerere University) CSC 1214 April, 2018 7 / 18
Applets Applet Fundamentals Using an applet viewer Option 1 Execute the HTML file (say RunApp.html) as follows: :> appletviewer RunApp.html Option 2 Include a comment at the head of your Java source file containing the APPLET tag and start the viewer with the Java source file 1 import java.awt.*; import java.applet.*; /* <applet code="simpleapplet" width=200 height=60> </applet> */ public class SimpleApplet extends Applet { public void paint(graphics g) { g.drawstring("a Simple Applet", 20, 20); 2 :> appletviewer SimpleApplet.java In summary 1 Edit a Java source file 2 Compile your program 3 Execute the applet viewer (specifying the name of your applet s source file) Kizito (Makerere University) CSC 1214 April, 2018 7 / 18
Applets Applet Fundamentals Applet Fundamentals Sumamry Applets do not need a main() method Instead the applet class must inherit Applet from java.applet or JApplet from javax.swing (in the case of a Swing applet) Applets must be run under an applet viewer or a Java-compatible browser User I/O is not accomplished with Java s stream I/O classes. Instead, applets use the interface provided by the AWT Kizito (Makerere University) CSC 1214 April, 2018 8 / 18
Swing Swing A supercharged and flexible alternative to AWT Swing is a set of classes provides more powerful and flexible components than are possible with the AWT It is a set of user interface components that is implemented entirely in Java In additional to the familiar components (e.g., buttons, check boxes, labels), Swing supplies several additions (including tabbed panes, scroll panes, trees, and tables) Even the familiar components such as buttons have more compatibilities in Swing Kizito (Makerere University) CSC 1214 April, 2018 9 / 18
Swing The JApplet class Swing The JApplet class Extends Applet and fundamental to Swing Applets that use Swing must be subclasses of JApplet JApplet is rich with functionality that is not found in Applet For example, JApplet supports various panes, such as the content pane, the glass pane, and the root pane The content pane can be obtained via the method: Container getcontentpane() The add() method of Container can be used to add a component to a content pane: void add(comp) Kizito (Makerere University) CSC 1214 April, 2018 10 / 18
Swing Swing Constructors Swing Icons, Labels, and Buttons Constructors Icons ImageIcon(String filename) ImageIcon(URL url) Labels JLabel(Icon i) Label(String s) JLabel(String s, Icon i, int align) Buttons JButton(Icon i) JButton(String s) JButton(String s, Icon i) Kizito (Makerere University) CSC 1214 April, 2018 11 / 18
Swing Swing Constructors Text Fields JTextField() JTextField(int cols) Swing Text Fields and Check Boxes JTextField(String s, int cols) JTextField(String s) Check Boxes JCheckBox(Icon i) JCheckBox(Icon i, boolean state) JCheckBox(String s) JCheckBox(String s, boolean state) JCheckBox(String s, Icon i) JCheckBox(String s, Icon i, boolean state) Kizito (Makerere University) CSC 1214 April, 2018 12 / 18
Swing Swing Constructors Swing Radio Buttons and Combo Boxes Radio Buttons JRadioButton(Icon i) JRadioButton(Icon i, boolean state) JRadioButton(String s) JRadioButton(String s, boolean state) JRadioButton(String s, Icon i) JRadioButton(String s, Icon i, boolean state) Combo Boxes JComboBox() JComboBox(Vector v) Kizito (Makerere University) CSC 1214 April, 2018 13 / 18
Swing Swing Constructors Swing Tabbed Panes, Scroll Panes, Trees and Tables Tabbed and Scroll Panes JTabbedPane() JScrollPane(Component comp) JScrollPane(int vsb, int hsb) JScrollPane(Component c, int vsb, int hsb) Trees and Tables JTree(Hashtable ht) JTree(Object obj[]) JTree(TreeNode tn) JTree(Vector v) JTable(Object data[][], Object colheads[]) Kizito (Makerere University) CSC 1214 April, 2018 14 / 18
Swing Swing Applet Example Swing JTextField Example import java.awt.*; import javax.swing.*; /* <applet code="jtextfielddemo" width=300 height=50> </applet> */ public class JTextFieldDemo extends JApplet { JTextField jtf; KDE public void init() { // Get content pane Container contentpane = getcontentpane(); contentpane.setlayout(new FlowLayout()); Win 8 // Add text field to content pane jtf = new JTextField(15); contentpane.add(jtf); Kizito (Makerere University) CSC 1214 April, 2018 15 / 18
GUI Applications GUI Applications We can also write GUI applications without extending Applet or JApplet We still make use of AWT and Swing We need to have a way of responding to actions/events (like mouse clicks) when they happen, so we need java.awt.event.* We shall use GUI containers like frames and panels A frame is displayed as a separate window with a title bar it can be repositioned and resized on the screen as needed A panel is a container that cannot be displayed on its own but is used to organize other components A panel must be added to another container to be displayed Recall our AsciiCounter assignment... Kizito (Makerere University) CSC 1214 April, 2018 16 / 18
GUI Applications GUI Applications AsciiCounter.java class AsciiCounter implements Counter { char value = a ; // or a? GUI public void reset() { value = a ; public void increment() { value = (char) (((int)value + 1) % 128); public void decrement() { value = (char) (((int)value - 1)<0? 127: --value); public String valueasstring () { return " "+ value +" ("+ ((int) value) +")" ; Kizito (Makerere University) CSC 1214 April, 2018 17 / 18
GUI Applications GUI Applications AsciiCounterUI.java 1. import java.awt.*; 31. pane.add(valuelbl); 2. import java.awt.event.*; 32. pane.add(inclbl); 3. import javax.swing.*; 33. pane.add(txtbx); 4. 34. pane.add(upbtn); 5. public class AsciiCounterUI 35. pane.add(downbtn); 6. implements ActionListener { 36. pane.add(resetbtn); 7. private Container pane; 37. 8. private JTextField txtbx; 38. upbtn.addactionlistener(this); 9. private JButton upbtn, downbtn, resetbtn; 39. downbtn.addactionlistener(this); 10. private JLabel vallbl, valuelbl, inclbl; 40. resetbtn.addactionlistener(this); 11. private JFrame frame; 41. frame.setvisible(true); 12. private AsciiCounter counter; 42. 13. 14. public AsciiCounterUI(AsciiCounter c) { 43. 44. public void actionperformed (ActionEvent e) { 15. counter = c; 45. JButton b = (JButton) e.getsource(); 16. frame = new JFrame("AsciiCounter"); 46. int inc = Integer.parseInt(txtBx.getText()); 17. frame.setsize(300,150); 47. if (b == resetbtn) frame.setdefaultcloseoperation(jframe.exit ON CLOSE); 48. counter.reset(); 19. pane = frame.getcontentpane(); 49. else 20. pane.setlayout(new GridLayout(4,2)); 50. for (int i=0; i<inc; i++) 21. 51. if (b == upbtn) counter.increment(); 22. vallbl = new JLabel("Counter value: "); 52. else counter.decrement(); 23. valuelbl = new JLabel("a"); 53. valuelbl.settext(counter.valueasstring()); 24. inclbl = new JLabel("Up/Down by: "); 54. 25. upbtn = new JButton("Up"); 55. 26. downbtn = new JButton("Down"); 56. public static void main(string args[]) { 27. resetbtn = new JButton("Reset"); 57. AsciiCounter c = new AsciiCounter(); 28. txtbx = new JTextField("1", 5); 58. AsciiCounterUI cui = new AsciiCounterUI(c); 29. 59. 30. pane.add(vallbl); 60. Kizito (Makerere University) CSC 1214 April, 2018 18 / 18