Java for Interfaces and Networks (DT3010, HT10)

Size: px
Start display at page:

Download "Java for Interfaces and Networks (DT3010, HT10)"

Transcription

1 Java for Interfaces and Networks (DT3010, HT10) More on Garbage Collection, More on Swing Federico Pecora School of Science and Technology Örebro University Federico Pecora Java for Interfaces and Networks Lecture 9 1 / 21

2 Outline 1 More on Garbage Collection 2 Introspection 3 More on Swing Federico Pecora Java for Interfaces and Networks Lecture 9 2 / 21

3 More on Garbage Collection Outline 1 More on Garbage Collection 2 Introspection 3 More on Swing Federico Pecora Java for Interfaces and Networks Lecture 9 3 / 21

4 More on Garbage Collection Garbage collection in Java When does garbage collection occur? If I instantiate objects with new how do I delete them? Should I trust the garbage collector to do all the work for me?... most of the time :-) Federico Pecora Java for Interfaces and Networks Lecture 9 4 / 21

5 More on Garbage Collection Garbage collection in Java Java garbage collection eliminates the need to free objects explicitly This eliminates a common cause of errors (memory leaks) You never have to worry about dangling references When an object is no longer reachable the space it occupies can be reclaimed Space is reclaimed at the garbage collector s discretion sometimes the programmer should, however, influence what happens when an object is reclaimed typically, when non-java resources are involved Federico Pecora Java for Interfaces and Networks Lecture 9 5 / 21

6 More on Garbage Collection Garbage collection and files FileCounter1.java 1 public class FileCounter1 { 2 public static void main(string[] args) { 3 try { 4 int nrfiles = 0; 5 ArrayList<BufferedReader> files = 6 new ArrayList<BufferedReader>(); 7 while (true) { 8 BufferedReader in = 9 new BufferedReader(new FileReader("dummy.txt")); 10 files.add(in); 11 ++nrfiles; 12 System.out.println("nrFiles = " + nrfiles); 13 } 14 } 15 catch (FileNotFoundException e) { 16 System.out.println("Exception: " + e.getmessage()); 17 } 18 } //main 19 } //class FileCounter1 Federico Pecora Java for Interfaces and Networks Lecture 9 6 / 21

7 More on Garbage Collection Garbage collection and files linux> java FileCounter1 nrfiles = 1 nrfiles = 2 nrfiles = 3 nrfiles = 4... nrfiles = 1018 nrfiles = 1019 nrfiles = 1020 nrfiles = 1021 Exception: dummy.txt (Too many open files) Federico Pecora Java for Interfaces and Networks Lecture 9 6 / 21

8 More on Garbage Collection Garbage collection and files (OK, the program is stupid... ) Unsurprisingly, the program crashes All instances of BufferedReader in are accumulated in a list Since all these references stay valid, they are clearly never collected Federico Pecora Java for Interfaces and Networks Lecture 9 6 / 21

9 More on Garbage Collection Garbage collection and files FileCounter2.java 1 public class FileCounter2 { 2 public static void main(string[] args) { 3 try { 4 int nrfiles = 0; 5 //ArrayList<BufferedReader> files = 6 // new ArrayList<BufferedReader>(); 7 while (true) { 8 BufferedReader in = 9 new BufferedReader(new FileReader("dummy.txt")); 10 //files.add(in); 11 ++nrfiles; 12 System.out.println("nrFiles = " + nrfiles); 13 } 14 } 15 catch (FileNotFoundException e) { 16 System.out.println("Exception: " + e.getmessage()); 17 } 18 } //main 19 } //class FileCounter2 Federico Pecora Java for Interfaces and Networks Lecture 9 6 / 21

10 More on Garbage Collection Garbage collection and files linux> java FileCounter2 nrfiles = 1 nrfiles = 2 nrfiles = 3 nrfiles = 4... nrfiles = 4951 nrfiles = 4952 nrfiles = 4953 nrfiles = 4954 Exception: dummy.txt (Too many open files) Federico Pecora Java for Interfaces and Networks Lecture 9 6 / 21

11 More on Garbage Collection Garbage collection and files Again, the program crashes! What is happening? the garbage collector closes files and reclaims memory when it starts running out (see implementation of BufferedReader) BufferedReader.java (probably!) 1 public void close() { 2 if (file!= null) { 3 file.close(); 4 file = null; 5 } 6 } 7 protected void finalize() throws Throwable { 8 try { close(); } 9 finally { super.finalize(); } 10 } Federico Pecora Java for Interfaces and Networks Lecture 9 6 / 21

12 More on Garbage Collection Garbage collection and files Again, the program crashes! What is happening? the garbage collector closes files and reclaims memory when it starts running out (see implementation of BufferedReader) however, the rate at which files are being opened can lead to a resource limit the OS reaches the soft limit of file descriptors it is willing to give our process In fact, we could get lucky on machines with less memory, the garbage collector may intervene before this limit is reached Federico Pecora Java for Interfaces and Networks Lecture 9 6 / 21

13 More on Garbage Collection Garbage collection and files linux> java FileCounter2 nrfiles = 1 nrfiles = 2 nrfiles = 3 nrfiles = 4... nrfiles = nrfiles = nrfiles = nrfiles = Federico Pecora Java for Interfaces and Networks Lecture 9 6 / 21

14 More on Garbage Collection Garbage collection and files Garbage collection occurs at its own pace, in its own thread Cannot count on finalize() method of BufferedReader to close files i.e., BufferedReader s finalize() method closes the file just in case it wasn t i.e., the file certainly has no reason to be open when it is being collected Solution to our problem close the files when not needed! files are non-java resources! Federico Pecora Java for Interfaces and Networks Lecture 9 6 / 21

15 More on Garbage Collection Garbage collection and files MyFileObject.java 1 class MyFileObject { 2 private final BufferedReader reader; 3 static int nrinstances = 0; 4 public MyFileObject(BufferedReader reader) { 5 this.reader = reader; 6 ++nrinstances; 7 System.out.println("nrInstances up, now " 8 + nrinstances); 9 } 10 public void finalize() { 11 --nrinstances; 12 System.out.println("nrInstances down, now " 13 + nrinstances); 14 } 15 public void close() throws IOException { 16 reader.close(); 17 } 18 } //class MyFileObject Federico Pecora Java for Interfaces and Networks Lecture 9 6 / 21

16 More on Garbage Collection Garbage collection and files FileCounter3.java 1 public class FileCounter3 { 2 public static void main(string[] args) { 3 try { 4 int nrfiles = 0; 5 while (true) { 6 BufferedReader in = 7 new BufferedReader(new FileReader("dummy.txt")); 8 MyFileObject mfo = new MyFileObject(in); 9 System.out.println("nrFiles = " + (++nrfiles)); 10 mfo.close(); 11 } 12 } 13 catch (FileNotFoundException e) {... } 14 catch (IOException e) {... } 15 } //main 16 } //class FileCounter3 Federico Pecora Java for Interfaces and Networks Lecture 9 6 / 21

17 More on Garbage Collection Garbage collection and files In summary when garbage collection occurs is out of our control we can only affect what happens when it occurs this is useful for disposing of non-java resources however, non-java resources ned to be disposed of manually as soon as logically possible this includes removing all references (e.g., from containers) this includes performing system-level operations (e.g., closing files) Federico Pecora Java for Interfaces and Networks Lecture 9 6 / 21

18 More on Garbage Collection Garbage collection and JFrames As with files, also JFrames deal with non-java resources... the user! The JVN cannot know when a JFrame ceases to be relevant (except for trivial cases) Also, it is up to the programmer to implement behavior of the JFrame when it closes the default is: do nothing! Problem can arise when an application can spawn new JFrames (e.g., setting options, etc.) Federico Pecora Java for Interfaces and Networks Lecture 9 7 / 21

19 More on Garbage Collection Garbage collection and JFrames Federico Pecora Java for Interfaces and Networks Lecture 9 7 / 21

20 More on Garbage Collection Garbage collection and JFrames JFrameExample.java 1 public class JFrameExample { 2 public static int nrchildren = 0; 3 public static void main(string[] args) { 4 JFrame f = new JFrame("This is a test"); 5 f.setsize(200, 150); 6 f.getcontentpane().setbackground(color.white); 7 f.getcontentpane().setlayout(new FlowLayout()); 8 JButton button = new JButton("New frame"); 9 f.getcontentpane().add(button); 10 //continues... Federico Pecora Java for Interfaces and Networks Lecture 9 7 / 21

21 More on Garbage Collection Garbage collection and JFrames JFrameExample.java (cont.) 11 button.addactionlistener(new ActionListener(){ 12 public void actionperformed(actionevent arg0) { 13 JFrame childf = new JFrame("Child frame") { 14 protected void finalize() { 15 System.out.println("DOWN: " + --nrchildren); 16 } 17 }; 18 childf.setsize(200, 100); 19 JLabel label = new JLabel("" + 20 new Random().nextDouble()); 21 childf.getcontentpane().add(label); 22 childf.setvisible(true); 23 System.out.println("UP: " + (++nrchildren)); 24 } 25 }); //addactionlistener 26 f.setvisible(true); 27 } //main 28 } //JFrameExample class Federico Pecora Java for Interfaces and Networks Lecture 9 7 / 21

22 More on Garbage Collection Garbage collection and JFrames linux> java -verbose:gc -Xmx1K JFrameExample [GC 768K->302K(4992K), secs] [GC 1070K->522K(4992K), secs] [GC 1279K->791K(4992K), secs] [GC 1389K->928K(4992K), secs] UP: 1 UP: 2 [GC 1696K->1229K(4992K), secs] UP: 3... UP: 62 UP: 63 [GC 2142K->1786K(4800K), secs] UP: 64 Federico Pecora Java for Interfaces and Networks Lecture 9 7 / 21

23 More on Garbage Collection Garbage collection and JFrames nrchildren never goes DOWN because JFrames are never collected even if closed the GC lines in the output are the JVM trying to free up space by removing collecting other objects we know it never collects the child JFrames because it never prints DOWN:... This is because the default behavior of JFrame is to make the frame invisible equivalent to setvisible(false) Federico Pecora Java for Interfaces and Networks Lecture 9 7 / 21

24 More on Garbage Collection Garbage collection and JFrames Overriding default behavior with WindowClosing events JFrameExample.java (part) 1 //What happens when the user closes the JFrame 2 WindowListener windowlistener = new WindowAdapter() { 3 //Anonymous WindowAdapter class 4 public void windowclosing(windowevent w) { 5 //What to do on close, e.g., remember location, 6 //then how to actually close the JFrame, e.g., 7 childf.setvisible(false); 8 childf.dispose(); 9 } //windowclosing 10 }; //anonymous class 11 childf.setdefaultcloseoperation(jframe.do_nothing_on_close); 12 childf.addwindowlistener(windowlistener); Federico Pecora Java for Interfaces and Networks Lecture 9 7 / 21

25 More on Garbage Collection Garbage collection and JFrames Overriding default behavior with WindowClosing events... or, use one of four shortcuts to writing WindowEvent event handlers JFrameExample.java (part) 1 //This calls dispose() 2 childf.setdefaultcloseoperation (JFrame.DISPOSE_ON_CLOSE); 3 4 //This calls setvisible(false) -- default! 5 childf.setdefaultcloseoperation (JFrame.HIDE_ON_CLOSE); 6 7 //This calls System.exit(0) 8 childf.setdefaultcloseoperation (JFrame.EXIT_ON_CLOSE); 9 10 //This does nothing 11 childf.setdefaultcloseoperation (JFrame.DO_NOTHING_ON_CLOSE); Federico Pecora Java for Interfaces and Networks Lecture 9 7 / 21

26 More on Garbage Collection Garbage collection and JFrames JFrameExample.java (cont.) 11 button.addactionlistener(new ActionListener(){ 12 public void actionperformed(actionevent arg0) { 13 JFrame childf = new JFrame("Child frame") {... }; 14 childf.setsize(200, 100); 15 JLabel label = new JLabel("" + 16 new Random().nextDouble()); 17 childf.getcontentpane().add(label); 18 childf.setvisible(true); 19 System.out.println("UP: " + (++nrchildren)); 20 childf. 21 setdefaultcloseoperation(jframe.dispose_on_close); 22 } 23 }); //addactionlistener 24 f.setvisible(true); 25 } //main 26 } //JFrameExample class Federico Pecora Java for Interfaces and Networks Lecture 9 7 / 21

27 More on Garbage Collection Garbage collection and JFrames linux> java -verbose:gc -Xmx1K JFrameExample [GC 768K->306K(4992K), secs] [GC 1074K->526K(4992K), secs] [GC 1283K->788K(4992K), secs] [GC 1381K->930K(4992K), secs] UP: 1 UP: 2 [GC 1698K->1228K(4992K), secs] UP: 3... UP: 20 UP: 21 [GC 1804K->1408K(4800K), secs] DOWN: 20 DOWN: 19 DOWN: 18 DOWN: 17 UP: 18 Federico Pecora Java for Interfaces and Networks Lecture 9 7 / 21

28 Introspection Outline 1 More on Garbage Collection 2 Introspection 3 More on Swing Federico Pecora Java for Interfaces and Networks Lecture 9 8 / 21

29 Introspection What is introspection? Introspection: a way for Java programs to look inside themselves Introspection allows programs to get and use information on classes and objects at run-time Using introspection, one can ask an object for its class ask a class for its methods and constructors find out the details of those methods and constructors tell those methods to execute Federico Pecora Java for Interfaces and Networks Lecture 9 9 / 21

30 Introspection Introspection: the java.lang.class class At the center of Java introspection is the class java.lang.class There are two common ways to obtain a Class object the Object.getClass() method the static Class.forName(String name) method Given a Class type object, you can obtain information on the class name, its fields, its constructors, its methods, and its package getconstructors() returns an array of Constructors getmethods() returns an array of Methods getfields() returns an array of Fields... see Java Class API Federico Pecora Java for Interfaces and Networks Lecture 9 10 / 21

31 Introspection Introspection: the Constructor class A Constructor provides information about, and access to, a single constructor The central method of Constructor is newinstance(object[] initargs) allows to instantiate a new object of that class using a particular constructor returns a new Object instance Federico Pecora Java for Interfaces and Networks Lecture 9 11 / 21

32 Introspection Introspection: the Method class A Method provides information about, and access to, a single method The method can be a class method or an instance method The central method of Method is invoke(object obj, String[] params) allows to invoke the method on a given obj with specific params returns the method s returned Object Federico Pecora Java for Interfaces and Networks Lecture 9 12 / 21

33 Introspection Introspection: obfuscated code example Rewriting the method call foo.bar("baz", 42); with introspection: Code snippet 1 foo.getclass().getmethod("bar",new Class[] { 2 Class.forName("java.lang.String"), 3 Integer.TYPE 4 }). 5 invoke(foo, new Object[] {"Baz", new Integer(42)}); Plug-in support: main application examines each of its plug-ins for the methods it supports and then call them when appropriate Introspection is central to JavaBeans technology We could write an alternative JavaDoc with introspection Federico Pecora Java for Interfaces and Networks Lecture 9 13 / 21

34 Introspection Introspection: simple example IntrospectionTest.java 1 public class IntrospectionTest extends JFrame { 2 public static void showclassinfo(object o) { 3 Class c = o.getclass(); 4 System.out.println("Class: " + c.getname()); 5 Class sc = c.getsuperclass(); 6 System.out.println("Superclass: " + sc.getname()); 7 //continues... Federico Pecora Java for Interfaces and Networks Lecture 9 14 / 21

35 Introspection Introspection: simple example IntrospectionTest.java (cont.) 8 Method[] methods = c.getdeclaredmethods(); 9 System.out.println("Methods:"); 10 for (int i = 0; i < methods.length; ++i) 11 System.out.println(" " + methods[i].getname()); 12 Constructor[] constructors = 13 c.getdeclaredconstructors(); 14 System.out.println("Constructors:"); 15 for (int i = 0; i < constructors.length; ++i) 16 System.out.println(" " + 17 constructors[i].getname()); 18 //continues... Federico Pecora Java for Interfaces and Networks Lecture 9 14 / 21

36 Introspection Introspection: simple example IntrospectionTest.java (cont.) 19 Field[] fields = c.getdeclaredfields(); 20 System.out.println("Fields:"); 21 for (int i = 0; i < fields.length; ++i) 22 System.out.println(" " + 23 fields[i].getname()); 24 } 25 public static void main(string[] args) { 26 JButton button = new JButton("Hello"); 27 showclassinfo(button); 28 } 29 } //class IntrospectionTest Federico Pecora Java for Interfaces and Networks Lecture 9 14 / 21

37 Introspection Introspection: simple example linux> java IntrospectionTest Class: javax.swing.jbutton Superclass: javax.swing.abstractbutton Methods: writeobject getaccessiblecontext paramstring removenotify getuiclassid isdefaultbutton isdefaultcapable setdefaultcapable updateui Constructors: javax.swing.jbutton javax.swing.jbutton javax.swing.jbutton javax.swing.jbutton javax.swing.jbutton Fields: uiclassid... stay tuned for a more compelling use of introspection in a few slides! Federico Pecora Java for Interfaces and Networks Lecture 9 14 / 21

38 More on Swing Outline 1 More on Garbage Collection 2 Introspection 3 More on Swing Federico Pecora Java for Interfaces and Networks Lecture 9 15 / 21

39 More on Swing A Swing-component example: ButtonGroup ButtonGroup: used to create a multiple-exclusion scope for a set of buttons turning "on" one of those buttons turns off all other buttons in the group Can be used with any set of objects that inherit from AbstractButton JRadioButton, JRadioButtonMenuItem, or JToggleButton,... Federico Pecora Java for Interfaces and Networks Lecture 9 16 / 21

40 More on Swing A Swing-component example: ButtonGroup ButtonGroupTest.java 1 public class ButtonGroupTest extends JFrame { 2 public ButtonGroupTest() { 3 super("buttongrouptest"); 4 Container cp = getcontentpane(); 5 cp.setlayout(new FlowLayout()); 6 ButtonGroup group = new ButtonGroup(); 7 JPanel panel = new JPanel(); 8 panel.setborder(new TitledBorder("3 x JRadioButton")); 9 JRadioButton button1 = new JRadioButton("Tripp"); 10 group.add(button1); 11 panel.add(button1); 12 JRadioButton button2 = new JRadioButton("Trapp"); 13 group.add(button2); 14 panel.add(button2); 15 //continues... Federico Pecora Java for Interfaces and Networks Lecture 9 16 / 21

41 More on Swing A Swing-component example: ButtonGroup ButtonGroupTest.java (cont.) 16 JRadioButton button3 = new JRadioButton("Trull"); 17 group.add(button3); 18 panel.add(button3); 19 cp.add(panel); 20 } 21 public static void main(string[] args) { 22 ButtonGroupTest frame = new ButtonGroupTest(); 23 frame.setdefaultcloseoperation(jframe.exit_on_close); 24 frame.setsize(300, 100); 25 frame.setvisible(true); 26 } 27 } //class ButtonGroupTest 28 Federico Pecora Java for Interfaces and Networks Lecture 9 16 / 21

42 More on Swing A Swing-component example: multiple ButtonGroups Now let s use other button types to make multiple button groups... Federico Pecora Java for Interfaces and Networks Lecture 9 17 / 21

43 More on Swing A Swing-component example: multiple ButtonGroups ButtonGroups1.java 1 public class ButtonGroups1 extends JFrame { 2 private static String[] ids = 3 { "Tripp", "Trapp", "Trull" }; 4 public ButtonGroups1() { 5 super("buttongroups"); 6 Container cp = getcontentpane(); 7 cp.setlayout(new FlowLayout()); 8 ButtonGroup group = new ButtonGroup(); 9 JPanel panel = new JPanel(); 10 panel.setborder(new TitledBorder("3 x JButton")); 11 for (int i = 0; i < ids.length; i++) { 12 JButton button = new JButton(ids[i]); 13 group.add(button); 14 panel.add(button); 15 } 16 cp.add(panel); 17 //continues... Federico Pecora Java for Interfaces and Networks Lecture 9 17 / 21

44 More on Swing A Swing-component example: multiple ButtonGroups ButtonGroups1.java (cont.) 18 group = new ButtonGroup(); 19 panel = new JPanel(); 20 panel.setborder(new TitledBorder("3 x JToggleButton")); 21 for (int i = 0; i < ids.length; i++) { 22 JToggleButton button = new JToggleButton(ids[i]); 23 group.add(button); 24 panel.add(button); 25 } 26 cp.add(panel); 27 //continues... Federico Pecora Java for Interfaces and Networks Lecture 9 17 / 21

45 More on Swing A Swing-component example: multiple ButtonGroups ButtonGroups1.java (cont.) 28 group = new ButtonGroup(); 29 panel = new JPanel(); 30 panel.setborder(new TitledBorder("3 x JCheckBox")); 31 for (int i = 0; i < ids.length; i++) { 32 JCheckBox button = new JCheckBox(ids[i]); 33 group.add(button); 34 panel.add(button); 35 } 36 cp.add(panel); 37 //continues... Federico Pecora Java for Interfaces and Networks Lecture 9 17 / 21

46 More on Swing A Swing-component example: multiple ButtonGroups ButtonGroups1.java (cont.) 38 group = new ButtonGroup(); 39 panel = new JPanel(); 40 panel.setborder(new TitledBorder("3 x JRadioButton")); 41 for (int i = 0; i < ids.length; i++) { 42 JRadioButton button = new JRadioButton(ids[i]); 43 group.add(button); 44 panel.add(button); 45 } 46 cp.add(panel); 47 } 48 public static void main(string[] args) { 49 ButtonGroups1 frame = new ButtonGroups1(); 50 frame.setdefaultcloseoperation(jframe.exit_on_close); 51 frame.setsize(300, 300); 52 frame.setvisible(true); 53 } 54 } //class ButtonGroups1 Federico Pecora Java for Interfaces and Networks Lecture 9 17 / 21

47 More on Swing Multiple ButtonGroups and introspection Notice how in the previous example we are repeating similar code four times in the instantiation and addition of buttons to the ButtonGroups Idea: use introspection so we can use more general code... after all, they are all extensions of AbstractButton Federico Pecora Java for Interfaces and Networks Lecture 9 18 / 21

48 More on Swing Multiple ButtonGroups and introspection ButtonGroups2.java 1 public class ButtonGroups2 extends JFrame { 2 private static String[] ids = { "Tripp", "Trapp", "Trull" }; 3 private JPanel makebuttonpanel(class class, String[] ids) { 4 ButtonGroup group = new ButtonGroup(); 5 JPanel panel = new JPanel(); 6 String title = class.getname(); 7 panel.setborder(new TitledBorder(title)); 8 //continues... Federico Pecora Java for Interfaces and Networks Lecture 9 18 / 21

49 More on Swing Multiple ButtonGroups and introspection ButtonGroups2.java (cont.) 9 for (int i = 0; i < ids.length; i++) { 10 AbstractButton button = new JButton("failed"); 11 try { 12 Constructor ctor = 13 class.getconstructor(new Class[] {String.class}); 14 button = (AbstractButton) 15 ctor.newinstance(new Object[] {ids[i]}); 16 } 17 catch (Exception e) {... } 18 group.add(button); 19 panel.add(button); 20 } 21 return panel; 22 } //makebuttonpanel method 23 //continues... Federico Pecora Java for Interfaces and Networks Lecture 9 18 / 21

50 More on Swing Multiple ButtonGroups and introspection ButtonGroups2.java (cont.) 24 public ButtonGroups2() { 25 super("buttongroups2"); 26 Container cp = getcontentpane(); 27 cp.setlayout(new FlowLayout()); 28 cp.add(makebuttonpanel(jbutton.class, ids)); 29 cp.add(makebuttonpanel(jtogglebutton.class, ids)); 30 cp.add(makebuttonpanel(jcheckbox.class, ids)); 31 cp.add(makebuttonpanel(jradiobutton.class, ids)); 32 } 33 public static void main(string[] args) { 34 ButtonGroups2 frame = new ButtonGroups2(); 35 frame.setdefaultcloseoperation(jframe.exit_on_close); 36 frame.setsize(300, 300); 37 frame.setvisible(true); 38 } 39 } //class ButtonGroups2 Federico Pecora Java for Interfaces and Networks Lecture 9 18 / 21

51 More on Swing Look and Feel The Java Look and Feel (L&F) is the default interface for Java applications Java (L&F) designed for cross-platform use and can provide consistency in the appearance and behavior of common design elements compatibility with industry-standard components and interaction styles aesthetic appeal that does not distract from application content Federico Pecora Java for Interfaces and Networks Lecture 9 19 / 21

52 More on Swing Look and Feel Default vs. System L&F Federico Pecora Java for Interfaces and Networks Lecture 9 19 / 21

53 More on Swing Look and Feel Sqrt1.java 1 public class Sqrt1 extends JFrame { 2 private JLabel label = new JLabel("Result"); 3 private JTextField text = new JTextField("Input number"); 4 private JButton button = new JButton("Calculate!"); 5 private ButtonListener bl = new ButtonListener(); 6 public Sqrt1() { 7 super("sqrt1"); 8 button.addactionlistener(bl); 9 Container cp = getcontentpane(); 10 cp.setlayout(new FlowLayout()); 11 cp.add(text); 12 cp.add(button); 13 cp.add(label); 14 } 15 //continues... Federico Pecora Java for Interfaces and Networks Lecture 9 19 / 21

54 More on Swing Look and Feel Sqrt1.java (cont.) 16 //member class for button action handling 17 class ButtonListener implements ActionListener { 18 public void actionperformed(actionevent event) { 19 String numbuf = text.gettext(); 20 try { 21 float num = Float.parseFloat(numbuf); 22 float res = (float)math.sqrt(num); 23 if (Float.isNaN(res)) 24 label.settext("number must be positive."); 25 else 26 label.settext("root of " + num + " is " + res); 27 } 28 catch (NumberFormatException exc) { 29 label.settext(" " + numbuf + " is NaN."); 30 } 31 } //actionperformed 32 } //class ButtonListener 33 //continues... Federico Pecora Java for Interfaces and Networks Lecture 9 19 / 21

55 More on Swing Look and Feel Sqrt1.java (cont.) 34 public static void main(string[] args) { 35 Sqrt1 frame = new Sqrt1(); 36 frame.setdefaultcloseoperation(jframe.exit_on_close); 37 frame.setsize(200, 100); 38 frame.setvisible(true); 39 } Federico Pecora Java for Interfaces and Networks Lecture 9 19 / 21

56 More on Swing Look and Feel In order to get the system-specific L&F we need specify before instantiating the first JFrame: Sqrt1.java (cont.) 34 public static void main(string[] args) { 35 String laf = UIManager.getSystemLookAndFeelClassName(); 36 try { UIManager.setLookAndFeel(laf); } 37 catch (UnsupportedLookAndFeelException e) { 38 System.err.println("Warning: UnsupportedLnF: " + laf); 39 } 40 catch (Exception e) { 41 System.err.println("Error loading " + laf + ": " + e); 42 } 43 Sqrt1 frame = new Sqrt1(); 44 frame.setdefaultcloseoperation(jframe.exit_on_close); 45 frame.setsize(200, 100); 46 frame.setvisible(true); 47 } Federico Pecora Java for Interfaces and Networks Lecture 9 19 / 21

57 More on Swing Look and Feel... for the cross-platform L&F: Sqrt1.java (cont.) 34 public static void main(string[] args) { 35 String laf = 36 UIManager.getCrossPlatformLookAndFeelClassName(); 37 try { UIManager.setLookAndFeel(laf); } 38 catch (UnsupportedLookAndFeelException e) { 39 System.err.println("Warning: UnsupportedLnF: " + laf); 40 } 41 catch (Exception e) { 42 System.err.println("Error loading " + laf + ": " + e); 43 } 44 Sqrt1 frame = new Sqrt1(); 45 frame.setdefaultcloseoperation(jframe.exit_on_close); 46 frame.setsize(200, 100); 47 frame.setvisible(true); 48 } Federico Pecora Java for Interfaces and Networks Lecture 9 19 / 21

58 More on Swing Look and Feel on different platforms/jvms See examples in $JAVA_HOME/demo/jfc/SwingSet2/SwingSet2.jar Federico Pecora Java for Interfaces and Networks Lecture 9 20 / 21

59 More on Swing Look and Feel on different platforms/jvms... on linux in 2003 Federico Pecora Java for Interfaces and Networks Lecture 9 20 / 21

60 More on Swing Look and Feel on different platforms/jvms... on windows with system L&F Federico Pecora Java for Interfaces and Networks Lecture 9 20 / 21

61 More on Swing More on Garbage Collection, More on Swing Thank you! Federico Pecora Java for Interfaces and Networks Lecture 9 21 / 21

Java for Interfaces and Networks (DT3010, HT10)

Java for Interfaces and Networks (DT3010, HT10) Java for Interfaces and Networks (DT3010, HT10) Inner Classes Federico Pecora School of Science and Technology Örebro University federico.pecora@oru.se Federico Pecora Java for Interfaces and Networks

More information

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class CHAPTER GRAPHICAL USER INTERFACES 10 Slides by Donald W. Smith TechNeTrain.com Final Draft 10/30/11 10.1 Frame Windows Java provides classes to create graphical applications that can run on any major graphical

More information

Eclipsing Your IDE. Figure 1 The first Eclipse screen.

Eclipsing Your IDE. Figure 1 The first Eclipse screen. Eclipsing Your IDE James W. Cooper I have been hearing about the Eclipse project for some months, and decided I had to take some time to play around with it. Eclipse is a development project (www.eclipse.org)

More information

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008 Overview Lecture 7: Inheritance and GUIs Written by: Daniel Dalevi Inheritance Subclasses and superclasses Java keywords Interfaces and inheritance The JComponent class Casting The cosmic superclass Object

More information

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User CSE3461 Control Flow Paradigms: Reacting to the User Control Flow: Overview Definition of control flow: The sequence of execution of instructions in a program. Control flow is determined at run time by

More information

Swing from A to Z Some Simple Components. Preface

Swing from A to Z Some Simple Components. Preface By Richard G. Baldwin baldwin.richard@iname.com Java Programming, Lecture Notes # 1005 July 31, 2000 Swing from A to Z Some Simple Components Preface Introduction Sample Program Interesting Code Fragments

More information

MIT AITI Swing Event Model Lecture 17

MIT AITI Swing Event Model Lecture 17 MIT AITI 2004 Swing Event Model Lecture 17 The Java Event Model In the last lecture, we learned how to construct a GUI to present information to the user. But how do GUIs interact with users? How do applications

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 35 November 29, 2017 Swing II: Building GUIs Inner Classes Chapter 29 Announcements Game Project Complete Code Due: December 11 th NO LATE SUBMISSIONS

More information

CS 11 java track: lecture 3

CS 11 java track: lecture 3 CS 11 java track: lecture 3 This week: documentation (javadoc) exception handling more on object-oriented programming (OOP) inheritance and polymorphism abstract classes and interfaces graphical user interfaces

More information

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7 PROGRAMMING DESIGN USING JAVA (ITT 303) Graphical User Interface Unit 7 Learning Objectives At the end of this unit students should be able to: Build graphical user interfaces Create and manipulate buttons,

More information

Java for Interfaces and Networks (DT3010, HT10)

Java for Interfaces and Networks (DT3010, HT10) Java for Interfaces and Networks (DT3010, HT10) More on Swing and Threads Federico Pecora School of Science and Technology Örebro University federico.pecora@oru.se Federico Pecora Java for Interfaces and

More information

Graphical User Interface (Part-1) Supplementary Material for CPSC 233

Graphical User Interface (Part-1) Supplementary Material for CPSC 233 Graphical User Interface (Part-1) Supplementary Material for CPSC 233 Introduction to Swing A GUI (graphical user interface) is a windowing system that interacts with the user The Java AWT (Abstract Window

More information

We are on the GUI fast track path

We are on the GUI fast track path We are on the GUI fast track path Chapter 13: Exception Handling Skip for now Chapter 14: Abstract Classes and Interfaces Sections 1 9: ActionListener interface Chapter 15: Graphics Skip for now Chapter

More information

Graphical User Interfaces. Comp 152

Graphical User Interfaces. Comp 152 Graphical User Interfaces Comp 152 Procedural programming Execute line of code at a time Allowing for selection and repetition Call one function and then another. Can trace program execution on paper from

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 35 November 28, 2018 Swing II: Inner Classes and Layout Chapter 30 Announcements Game Project Complete Code Due: December 10 th NO LATE SUBMISSIONS

More information

Final Examination Semester 2 / Year 2011

Final Examination Semester 2 / Year 2011 Southern College Kolej Selatan 南方学院 Final Examination Semester 2 / Year 2011 COURSE COURSE CODE TIME DEPARTMENT LECTURER : JAVA PROGRAMMING : PROG1114 : 2 1/2 HOURS : COMPUTER SCIENCE : LIM PEI GEOK Student

More information

Final Examination Semester 2 / Year 2012

Final Examination Semester 2 / Year 2012 Final Examination Semester 2 / Year 2012 COURSE : JAVA PROGRAMMING COURSE CODE : PROG1114 TIME : 2 1/2 HOURS DEPARTMENT : COMPUTER SCIENCE LECTURER : LIM PEI GEOK Student s ID : Batch No. : Notes to candidates:

More information

Systems Programming Graphical User Interfaces

Systems Programming Graphical User Interfaces Systems Programming Graphical User Interfaces Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: José Jesús García Rueda Systems Programming GUIs based on Java

More information

Midterm assessment - MAKEUP Fall 2010

Midterm assessment - MAKEUP Fall 2010 M257 MTA Faculty of Computer Studies Information Technology and Computing Date: /1/2011 Duration: 60 minutes 1-Version 1 M 257: Putting Java to Work Midterm assessment - MAKEUP Fall 2010 Student Name:

More information

Window Interfaces Using Swing Objects

Window Interfaces Using Swing Objects Chapter 12 Window Interfaces Using Swing Objects Event-Driven Programming and GUIs Swing Basics and a Simple Demo Program Layout Managers Buttons and Action Listeners Container Classes Text I/O for GUIs

More information

Introduction This assignment will ask that you write a simple graphical user interface (GUI).

Introduction This assignment will ask that you write a simple graphical user interface (GUI). Computing and Information Systems/Creative Computing University of London International Programmes 2910220: Graphical Object-Oriented and Internet programming in Java Coursework one 2011-12 Introduction

More information

Java for Interfaces and Networks (DT3029)

Java for Interfaces and Networks (DT3029) Java for Interfaces and Networks (DT3029) Lecture 9 More on Swing and Threads Federico Pecora federico.pecora@oru.se Center for Applied Autonomous Sensor Systems (AASS) Örebro University, Sweden www.clipartlord.com

More information

AP CS Unit 11: Graphics and Events

AP CS Unit 11: Graphics and Events AP CS Unit 11: Graphics and Events This packet shows how to create programs with a graphical interface in a way that is consistent with the approach used in the Elevens program. Copy the following two

More information

Java for Interfaces and Networks (DT3010, HT10)

Java for Interfaces and Networks (DT3010, HT10) Java for Interfaces and Networks (DT3010, HT10) More Basics: Classes, Exceptions, Garbage Collection, Interfaces, Packages Federico Pecora School of Science and Technology Örebro University federico.pecora@oru.se

More information

BBM 102 Introduction to Programming II Spring Exceptions

BBM 102 Introduction to Programming II Spring Exceptions BBM 102 Introduction to Programming II Spring 2018 Exceptions 1 Today What is an exception? What is exception handling? Keywords of exception handling try catch finally Throwing exceptions throw Custom

More information

(Incomplete) History of GUIs

(Incomplete) History of GUIs CMSC 433 Programming Language Technologies and Paradigms Spring 2004 Graphical User Interfaces April 20, 2004 (Incomplete) History of GUIs 1973: Xerox Alto 3-button mouse, bit-mapped display, windows 1981:

More information

Java for Interfaces and Networks

Java for Interfaces and Networks Java for Interfaces and Networks Threads and Networking Federico Pecora School of Science and Technology Örebro University federico.pecora@oru.se Federico Pecora Java for Interfaces and Networks Lecture

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 32 April 9, 2018 Swing I: Drawing and Event Handling Chapter 29 HW8: Spellchecker Available on the web site Due: Tuesday! Announcements Parsing, working

More information

Introduction to the JAVA UI classes Advanced HCI IAT351

Introduction to the JAVA UI classes Advanced HCI IAT351 Introduction to the JAVA UI classes Advanced HCI IAT351 Week 3 Lecture 1 17.09.2012 Lyn Bartram lyn@sfu.ca About JFC and Swing JFC Java TM Foundation Classes Encompass a group of features for constructing

More information

University of Cape Town Department of Computer Science. Computer Science CSC117F Solutions

University of Cape Town Department of Computer Science. Computer Science CSC117F Solutions University of Cape Town Department of Computer Science Computer Science CSC117F Solutions Class Test 4 Wednesday 14 May 2003 Marks: 40 Approximate marks per question are shown in brackets Time: 40 minutes

More information

Lecture 28. Exceptions and Inner Classes. Goals. We are going to talk in more detail about two advanced Java features:

Lecture 28. Exceptions and Inner Classes. Goals. We are going to talk in more detail about two advanced Java features: Lecture 28 Exceptions and Inner Classes Goals We are going to talk in more detail about two advanced Java features: Exceptions supply Java s error handling mechanism. Inner classes ease the overhead of

More information

Programming Languages and Techniques (CIS120e)

Programming Languages and Techniques (CIS120e) Programming Languages and Techniques (CIS120e) Lecture 29 Nov. 19, 2010 Swing I Event- driven programming Passive: ApplicaHon waits for an event to happen in the environment When an event occurs, the applicahon

More information

Graphic User Interfaces. - GUI concepts - Swing - AWT

Graphic User Interfaces. - GUI concepts - Swing - AWT Graphic User Interfaces - GUI concepts - Swing - AWT 1 What is GUI Graphic User Interfaces are used in programs to communicate more efficiently with computer users MacOS MS Windows X Windows etc 2 Considerations

More information

H212 Introduction to Software Systems Honors

H212 Introduction to Software Systems Honors Introduction to Software Systems Honors Lecture #19: November 4, 2015 1/14 Third Exam The third, Checkpoint Exam, will be on: Wednesday, November 11, 2:30 to 3:45 pm You will have 3 questions, out of 9,

More information

Final Examination Semester 2 / Year 2010

Final Examination Semester 2 / Year 2010 Southern College Kolej Selatan 南方学院 Final Examination Semester 2 / Year 2010 COURSE : JAVA PROGRAMMING COURSE CODE : PROG1114 TIME : 2 1/2 HOURS DEPARTMENT : COMPUTER SCIENCE LECTURER : LIM PEI GEOK Student

More information

RAIK 183H Examination 2 Solution. November 10, 2014

RAIK 183H Examination 2 Solution. November 10, 2014 RAIK 183H Examination 2 Solution November 10, 2014 Name: NUID: This examination consists of 5 questions and you have 110 minutes to complete the test. Show all steps (including any computations/explanations)

More information

Java Programming Lecture 6

Java Programming Lecture 6 Java Programming Lecture 6 Alice E. Fischer Feb 15, 2013 Java Programming - L6... 1/32 Dialog Boxes Class Derivation The First Swing Programs: Snow and Moving The Second Swing Program: Smile Swing Components

More information

Jonathan Aldrich Charlie Garrod

Jonathan Aldrich Charlie Garrod Principles of Software Construction: Objects, Design, and Concurrency (Part 3: Design Case Studies) Introduction to GUIs Jonathan Aldrich Charlie Garrod School of Computer Science 1 Administrivia Homework

More information

SampleApp.java. Page 1

SampleApp.java. Page 1 SampleApp.java 1 package msoe.se2030.sequence; 2 3 /** 4 * This app creates a UI and processes data 5 * @author hornick 6 */ 7 public class SampleApp { 8 private UserInterface ui; // the UI for this program

More information

Course Status Networking GUI Wrap-up. CS Java. Introduction to Java. Andy Mroczkowski

Course Status Networking GUI Wrap-up. CS Java. Introduction to Java. Andy Mroczkowski CS 190 - Java Introduction to Java Andy Mroczkowski uamroczk@cs.drexel.edu Department of Computer Science Drexel University March 10, 2008 / Lecture 8 Outline Course Status Course Information & Schedule

More information

JRadioButton account_type_radio_button2 = new JRadioButton("Current"); ButtonGroup account_type_button_group = new ButtonGroup();

JRadioButton account_type_radio_button2 = new JRadioButton(Current); ButtonGroup account_type_button_group = new ButtonGroup(); Q)Write a program to design an interface containing fields User ID, Password and Account type, and buttons login, cancel, edit by mixing border layout and flow layout. Add events handling to the button

More information

Java Graphical User Interfaces AWT (Abstract Window Toolkit) & Swing

Java Graphical User Interfaces AWT (Abstract Window Toolkit) & Swing Java Graphical User Interfaces AWT (Abstract Window Toolkit) & Swing Rui Moreira Some useful links: http://java.sun.com/docs/books/tutorial/uiswing/toc.html http://www.unix.org.ua/orelly/java-ent/jfc/

More information

Window Interfaces Using Swing Objects

Window Interfaces Using Swing Objects Chapter 12 Window Interfaces Using Swing Objects Event-Driven Programming and GUIs Swing Basics and a Simple Demo Program Layout Managers Buttons and Action Listeners Container Classes Text I/O for GUIs

More information

Handout 14 Graphical User Interface (GUI) with Swing, Event Handling

Handout 14 Graphical User Interface (GUI) with Swing, Event Handling Handout 12 CS603 Object-Oriented Programming Fall 15 Page 1 of 12 Handout 14 Graphical User Interface (GUI) with Swing, Event Handling The Swing library (javax.swing.*) Contains classes that implement

More information

CSIS 10A Assignment 7 SOLUTIONS

CSIS 10A Assignment 7 SOLUTIONS CSIS 10A Assignment 7 SOLUTIONS Read: Chapter 7 Choose and complete any 10 points from the problems below, which are all included in the download file on the website. Use BlueJ to complete the assignment,

More information

CompSci 125 Lecture 17. GUI: Graphics, Check Boxes, Radio Buttons

CompSci 125 Lecture 17. GUI: Graphics, Check Boxes, Radio Buttons CompSci 125 Lecture 17 GUI: Graphics, Check Boxes, Radio Buttons Announcements GUI Review Review: Inheritance Subclass is a Parent class Includes parent s features May Extend May Modify extends! Parent

More information

Goals. Lecture 7 More GUI programming. The application. The application D&D 12. CompSci 230: Semester JFrame subclass: ListOWords

Goals. Lecture 7 More GUI programming. The application. The application D&D 12. CompSci 230: Semester JFrame subclass: ListOWords Goals By the end of this lesson, you should: Lecture 7 More GUI programming 1. Be able to write Java s with JTextField, JList, JCheckBox and JRadioButton components 2. Be able to implement a ButtonGroup

More information

Packages: Putting Classes Together

Packages: Putting Classes Together Packages: Putting Classes Together 1 Introduction 2 The main feature of OOP is its ability to support the reuse of code: Extending the classes (via inheritance) Extending interfaces The features in basic

More information

Lecture 9. Lecture

Lecture 9. Lecture Layout Components MVC Design PaCern GUI Programming Observer Design PaCern D0010E Lecture 8 - Håkan Jonsson 1 Lecture 8 - Håkan Jonsson 2 Lecture 8 - Håkan Jonsson 3 1 1. GUI programming In the beginning,

More information

G51PGP Programming Paradigms. Lecture 008 Inner classes, anonymous classes, Swing worker thread

G51PGP Programming Paradigms. Lecture 008 Inner classes, anonymous classes, Swing worker thread G51PGP Programming Paradigms Lecture 008 Inner classes, anonymous classes, Swing worker thread 1 Reminder subtype polymorphism public class TestAnimals public static void main(string[] args) Animal[] animals

More information

CS Exam 1 Review Suggestions

CS Exam 1 Review Suggestions CS 235 - Fall 2015 - Exam 1 Review Suggestions p. 1 last modified: 2015-09-30 CS 235 - Exam 1 Review Suggestions You are responsible for material covered in class sessions, lab exercises, and homeworks;

More information

Java JavaBeans JavaBeans Builder (vitsual) Java JavaBeans JavaBeans http://java.sun.com/j2se/1.3/ja/docs/ja/guide/beans/ http://java.sun.com/products/javabeans/software/bdk_download.html Beans Development

More information

Java for Interfaces and Networks (DT3029)

Java for Interfaces and Networks (DT3029) Java for Interfaces and Networks (DT3029) Lecture 3 Threads and Networking Federico Pecora federico.pecora@oru.se Center for Applied Autonomous Sensor Systems (AASS) Örebro University, Sweden Capiscum

More information

RAIK 183H Examination 2 Solution. November 11, 2013

RAIK 183H Examination 2 Solution. November 11, 2013 RAIK 183H Examination 2 Solution November 11, 2013 Name: NUID: This examination consists of 5 questions and you have 110 minutes to complete the test. Show all steps (including any computations/explanations)

More information

Full file at Chapter 2 - Inheritance and Exception Handling

Full file at   Chapter 2 - Inheritance and Exception Handling Chapter 2 - Inheritance and Exception Handling TRUE/FALSE 1. The superclass inherits all its properties from the subclass. ANS: F PTS: 1 REF: 76 2. Private members of a superclass can be accessed by a

More information

PIC 20A GUI with swing

PIC 20A GUI with swing PIC 20A GUI with swing Ernest Ryu UCLA Mathematics Last edited: November 22, 2017 Hello swing Let s create a JFrame. import javax. swing.*; public class Test { public static void main ( String [] args

More information

Programming Language Concepts: Lecture 8

Programming Language Concepts: Lecture 8 Programming Language Concepts: Lecture 8 Madhavan Mukund Chennai Mathematical Institute madhavan@cmi.ac.in http://www.cmi.ac.in/~madhavan/courses/pl2009 PLC 2009, Lecture 8, 11 February 2009 GUIs and event

More information

G51PGP Programming Paradigms. Lecture 009 Concurrency, exceptions

G51PGP Programming Paradigms. Lecture 009 Concurrency, exceptions G51PGP Programming Paradigms Lecture 009 Concurrency, exceptions 1 Reminder subtype polymorphism public class TestAnimals public static void main(string[] args) Animal[] animals = new Animal[6]; animals[0]

More information

Lab 4. D0010E Object-Oriented Programming and Design. Today s lecture. GUI programming in

Lab 4. D0010E Object-Oriented Programming and Design. Today s lecture. GUI programming in Lab 4 D0010E Object-Oriented Programming and Design Lecture 9 Lab 4: You will implement a game that can be played over the Internet. The networking part has already been written. Among other things, the

More information

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written. SOLUTION HAND IN Answers Are Recorded on Question Paper QUEEN'S UNIVERSITY SCHOOL OF COMPUTING CISC212, FALL TERM, 2006 FINAL EXAMINATION 7pm to 10pm, 19 DECEMBER 2006, Jeffrey Hall 1 st Floor Instructor:

More information

Class 16: The Swing Event Model

Class 16: The Swing Event Model Introduction to Computation and Problem Solving Class 16: The Swing Event Model Prof. Steven R. Lerman and Dr. V. Judson Harward 1 The Java Event Model Up until now, we have focused on GUI's to present

More information

1005ICT Object Oriented Programming Lecture Notes

1005ICT Object Oriented Programming Lecture Notes 1005ICT Object Oriented Programming Lecture Notes School of Information and Communication Technology Griffith University Semester 2, 2015 1 20 GUI Components and Events This section develops a program

More information

SE1021 Exam 2. When returning your exam, place your note-sheet on top. Page 1: This cover. Page 2 (Multiple choice): 10pts

SE1021 Exam 2. When returning your exam, place your note-sheet on top. Page 1: This cover. Page 2 (Multiple choice): 10pts SE1021 Exam 2 Name: You may use a note-sheet for this exam. But all answers should be your own, not from slides or text. Review all questions before you get started. The exam is printed single-sided. Write

More information

Sketchpad. Plan for Today. Class 22: Graphical User Interfaces IBM 705 (1954) Computer as Clerk : Augmenting Human Intellect

Sketchpad. Plan for Today. Class 22: Graphical User Interfaces IBM 705 (1954) Computer as Clerk : Augmenting Human Intellect cs2220: Engineering Software Class 22: Graphical User Interfaces Plan for Today History of Interactive Computing Building GUIs in Java Xerox Star Fall 2010 UVa David Evans Design Reviews this week! Univac

More information

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written. HAND IN Answers Are Recorded on Question Paper QUEEN'S UNIVERSITY SCHOOL OF COMPUTING CISC124, WINTER TERM, 2009 FINAL EXAMINATION 7pm to 10pm, 18 APRIL 2009, Dunning Hall Instructor: Alan McLeod If the

More information

Java for Interfaces and Networks (DT3010, HT10)

Java for Interfaces and Networks (DT3010, HT10) Java for Interfaces and Networks (DT3010, HT10) Mouse Events, Timers, Serialization Federico Pecora School of Science and Technology Örebro University federico.pecora@oru.se Federico Pecora Java for Interfaces

More information

Multiple Choice Questions: Identify the choice that best completes the statement or answers the question. (15 marks)

Multiple Choice Questions: Identify the choice that best completes the statement or answers the question. (15 marks) M257 MTA Spring2010 Multiple Choice Questions: Identify the choice that best completes the statement or answers the question. (15 marks) 1. If we need various objects that are similar in structure, but

More information

Introflection. Dave Landers BEA Systems, Inc.

Introflection. Dave Landers BEA Systems, Inc. Introflection Dave Landers BEA Systems, Inc. dave.landers@bea.com Agenda What is Introflection? Primary Classes and Objects Loading Classes Creating Objects Invoking Methods Java Beans Proxy What is Introflection?

More information

Graphical User Interfaces

Graphical User Interfaces Graphical User Interfaces CSCI 136: Fundamentals CSCI 136: Fundamentals of Computer of Science Computer II Science Keith II Vertanen Keith Vertanen Copyright 2011 Overview Command line versus GUI apps

More information

Part 3: Graphical User Interface (GUI) & Java Applets

Part 3: Graphical User Interface (GUI) & Java Applets 1,QWURGXFWLRQWR-DYD3URJUDPPLQJ (( Part 3: Graphical User Interface (GUI) & Java Applets EE905-GUI 7RSLFV Creating a Window Panels Event Handling Swing GUI Components ƒ Layout Management ƒ Text Field ƒ

More information

BBM 102 Introduction to Programming II Spring 2017

BBM 102 Introduction to Programming II Spring 2017 BBM 102 Introduction to Programming II Spring 2017 Exceptions Instructors: Ayça Tarhan, Fuat Akal, Gönenç Ercan, Vahid Garousi Today What is an exception? What is exception handling? Keywords of exception

More information

Together, the appearance and how user interacts with the program are known as the program look and feel.

Together, the appearance and how user interacts with the program are known as the program look and feel. Lecture 10 Graphical User Interfaces A graphical user interface is a visual interface to a program. GUIs are built from GUI components (buttons, menus, labels etc). A GUI component is an object with which

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 34 April 11, 2016 Swing II: Inner Classes and Layout Announcements (Review) Final exam: May 2, 3-5PM If you have two finals at the same time, you can

More information

COMPSCI 230. Software Design and Construction. Swing

COMPSCI 230. Software Design and Construction. Swing COMPSCI 230 Software Design and Construction Swing 1 2013-04-17 Recap: SWING DESIGN PRINCIPLES 1. GUI is built as containment hierarchy of widgets (i.e. the parent-child nesting relation between them)

More information

Welcome to CIS 068! 1. GUIs: JAVA Swing 2. (Streams and Files we ll not cover this in this semester, just a review) CIS 068

Welcome to CIS 068! 1. GUIs: JAVA Swing 2. (Streams and Files we ll not cover this in this semester, just a review) CIS 068 Welcome to! 1. GUIs: JAVA Swing 2. (Streams and Files we ll not cover this in this semester, just a review) Overview JAVA and GUIs: SWING Container, Components, Layouts Using SWING Streams and Files Text

More information

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written. HAND IN Answers Are Recorded on Question Paper QUEEN'S UNIVERSITY SCHOOL OF COMPUTING CISC212, FALL TERM, 2006 FINAL EXAMINATION 7pm to 10pm, 19 DECEMBER 2006, Jeffrey Hall 1 st Floor Instructor: Alan

More information

Virtualians.ning.pk. 2 - Java program code is compiled into form called 1. Machine code 2. native Code 3. Byte Code (From Lectuer # 2) 4.

Virtualians.ning.pk. 2 - Java program code is compiled into form called 1. Machine code 2. native Code 3. Byte Code (From Lectuer # 2) 4. 1 - What if the main method is declared as private? 1. The program does not compile 2. The program compiles but does not run 3. The program compiles and runs properly ( From Lectuer # 2) 4. The program

More information

Exceptions. References. Exceptions. Exceptional Conditions. CSE 413, Autumn 2005 Programming Languages

Exceptions. References. Exceptions. Exceptional Conditions. CSE 413, Autumn 2005 Programming Languages References Exceptions "Handling Errors with Exceptions", Java tutorial http://java.sun.com/docs/books/tutorial/essential/exceptions/index.html CSE 413, Autumn 2005 Programming Languages http://www.cs.washington.edu/education/courses/413/05au/

More information

Introduction to concurrency and GUIs

Introduction to concurrency and GUIs Principles of Software Construction: Objects, Design, and Concurrency Part 2: Designing (Sub)systems Introduction to concurrency and GUIs Charlie Garrod Bogdan Vasilescu School of Computer Science 1 Administrivia

More information

Swing from A to Z Using Focus in Swing, Part 2. Preface

Swing from A to Z Using Focus in Swing, Part 2. Preface Swing from A to Z Using Focus in Swing, Part 2 By Richard G. Baldwin Java Programming, Lecture Notes # 1042 November 27, 2000 Preface Introduction Sample Program Interesting Code Fragments Summary What's

More information

GUI Applications. Let s start with a simple Swing application in Java, and then we will look at the same application in Jython. See Listing 16-1.

GUI Applications. Let s start with a simple Swing application in Java, and then we will look at the same application in Jython. See Listing 16-1. GUI Applications The C implementation of Python comes with Tkinter for writing Graphical User Interfaces (GUIs). The GUI toolkit that you get automatically with Jython is Swing, which is included with

More information

Java reflection. alberto ferrari university of parma

Java reflection. alberto ferrari university of parma Java reflection alberto ferrari university of parma reflection metaprogramming is a programming technique in which computer programs have the ability to treat programs as their data a program can be designed

More information

Example Programs. COSC 3461 User Interfaces. GUI Program Organization. Outline. DemoHelloWorld.java DemoHelloWorld2.java DemoSwing.

Example Programs. COSC 3461 User Interfaces. GUI Program Organization. Outline. DemoHelloWorld.java DemoHelloWorld2.java DemoSwing. COSC User Interfaces Module 3 Sequential vs. Event-driven Programming Example Programs DemoLargestConsole.java DemoLargestGUI.java Demo programs will be available on the course web page. GUI Program Organization

More information

GUI Program Organization. Sequential vs. Event-driven Programming. Sequential Programming. Outline

GUI Program Organization. Sequential vs. Event-driven Programming. Sequential Programming. Outline Sequential vs. Event-driven Programming Reacting to the user GUI Program Organization Let s digress briefly to examine the organization of our GUI programs We ll do this in stages, by examining three example

More information

Java Swing. based on slides by: Walter Milner. Java Swing Walter Milner 2005: Slide 1

Java Swing. based on slides by: Walter Milner. Java Swing Walter Milner 2005: Slide 1 Java Swing based on slides by: Walter Milner Java Swing Walter Milner 2005: Slide 1 What is Swing? A group of 14 packages to do with the UI 451 classes as at 1.4 (!) Part of JFC Java Foundation Classes

More information

Module 5 The Applet Class, Swings. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Module 5 The Applet Class, Swings. OOC 4 th Sem, B Div Prof. Mouna M. Naravani Module 5 The Applet Class, Swings OOC 4 th Sem, B Div 2016-17 Prof. Mouna M. Naravani The layout manager helps lay out the components held by this container. When you set a layout to null, you tell the

More information

CSE Lab 8 Assignment Note: This is the last lab for CSE 1341

CSE Lab 8 Assignment Note: This is the last lab for CSE 1341 CSE 1341 - Lab 8 Assignment Note: This is the last lab for CSE 1341 Pre-Lab : There is no pre-lab this week. Lab (100 points) The objective of Lab 8 is to get familiar with and utilize the wealth of Java

More information

1 Looping Constructs (4 minutes, 2 points)

1 Looping Constructs (4 minutes, 2 points) Name: Career Account ID: Recitation#: 1 CS180 Spring 2011 Final Exam, 3 May, 2011 Prof. Chris Clifton Turn Off Your Cell Phone. Use of any electronic device during the test is prohibited. Time will be

More information

CSE 8B Intro to CS: Java

CSE 8B Intro to CS: Java CSE 8B Intro to CS: Java Winter, 2006 February 23 (Day 14) Menus Swing Event Handling Inner classes Instructor: Neil Rhodes JMenuBar container for menus Menus associated with a JFrame via JFrame.setMenuBar

More information

CSE331 Fall 2014, Final Examination December 9, 2014 Please do not turn the page until 2:30. Rules:

CSE331 Fall 2014, Final Examination December 9, 2014 Please do not turn the page until 2:30. Rules: CSE331 Fall 2014, Final Examination December 9, 2014 Please do not turn the page until 2:30. Rules: The exam is closed-book, closed-note, etc. Please stop promptly at 4:20. There are 156 (not 100) points,

More information

Swing I CHAPTER EVENT-DRIVEN PROGRAMMING 921 Events and Listeners 921

Swing I CHAPTER EVENT-DRIVEN PROGRAMMING 921 Events and Listeners 921 CHAPTER 17 Swing I 17.1 EVENT-DRIVEN PROGRAMMING 921 Events and Listeners 921 17.2 BUTTONS, EVENTS, AND OTHER SWING BASICS 923 Example: A Simple Window 923 Buttons 930 Action Listeners and Action Events

More information

Graphical Interfaces

Graphical Interfaces Weeks 11&12 Graphical Interfaces All the programs that you have created until now used a simple command line interface, which is not user friendly, so a Graphical User Interface (GUI) should be used. The

More information

CS180 Spring 2010 Exam 2 Solutions, 29 March, 2010 Prof. Chris Clifton

CS180 Spring 2010 Exam 2 Solutions, 29 March, 2010 Prof. Chris Clifton CS180 Spring 2010 Exam 2 Solutions, 29 March, 2010 Prof. Chris Clifton Turn Off Your Cell Phone. Use of any electronic device during the test is prohibited. Time will be tight. If you spend more than the

More information

Programming Language Concepts: Lecture 10

Programming Language Concepts: Lecture 10 Programming Language Concepts: Lecture 10 Madhavan Mukund Chennai Mathematical Institute madhavan@cmi.ac.in http://www.cmi.ac.in/~madhavan/courses/pl2009 PLC 2009, Lecture 10, 16 February 2009 Reflection

More information

Graphical User Interfaces in Java - SWING

Graphical User Interfaces in Java - SWING Graphical User Interfaces in Java - SWING Graphical User Interfaces (GUI) Each graphical component that the user can see on the screen corresponds to an object of a class Component: Window Button Menu...

More information

Swing I Event-Driven Programming Buttons, Events, and Other Swing Basics Containers and Layout Managers 946

Swing I Event-Driven Programming Buttons, Events, and Other Swing Basics Containers and Layout Managers 946 17.1 Event-Driven Programming 925 Events and Listeners 925 17.2 Buttons, Events, and Other Swing Basics 926 Example: A Simple Window 927 Buttons 933 Action Listeners and Action Events 934 Example: A Better

More information

Original GUIs. IntroGUI 1

Original GUIs. IntroGUI 1 Original GUIs IntroGUI 1 current GUIs IntroGUI 2 Why GUIs? IntroGUI 3 Computer Graphics technology enabled GUIs and computer gaming. GUI's were 1985 breakout computer technology. Without a GUI there would

More information

CSEN401 Computer Programming Lab. Topics: Graphical User Interface Window Interfaces using Swing

CSEN401 Computer Programming Lab. Topics: Graphical User Interface Window Interfaces using Swing CSEN401 Computer Programming Lab Topics: Graphical User Interface Window Interfaces using Swing Prof. Dr. Slim Abdennadher 22.3.2015 c S. Abdennadher 1 Swing c S. Abdennadher 2 AWT versus Swing Two basic

More information

Exceptions. CSE 142, Summer 2002 Computer Programming 1.

Exceptions. CSE 142, Summer 2002 Computer Programming 1. Exceptions CSE 142, Summer 2002 Computer Programming 1 http://www.cs.washington.edu/education/courses/142/02su/ 12-Aug-2002 cse142-19-exceptions 2002 University of Washington 1 Reading Readings and References»

More information

Exceptions. Readings and References. Exceptions. Exceptional Conditions. Reading. CSE 142, Summer 2002 Computer Programming 1.

Exceptions. Readings and References. Exceptions. Exceptional Conditions. Reading. CSE 142, Summer 2002 Computer Programming 1. Readings and References Exceptions CSE 142, Summer 2002 Computer Programming 1 http://www.cs.washington.edu/education/courses/142/02su/ Reading» Chapter 18, An Introduction to Programming and Object Oriented

More information