Graphical User Interface Programming

Size: px
Start display at page:

Download "Graphical User Interface Programming"

Transcription

1 Graphical User Interface Programming Michael Brockway January 14, 2015

2 Graphical User Interfaces Users interact with modern application programs using graphical components such as windows, buttons (plain, checkbox, radio buttons), text boxes, menus, etc. Early versions of Java used the abstract windowing toolkit (AWT) for programming these. These are little used now. Instead, we mostly use the Swing classes, aka the Java Foundation Classes. These are little used now; instead we use Swing aka Java Foundation Class components. Swing ships with all recent versions of the Java standard edition toolkit, and is the recommended library of classes for programming graphical user interfaces. This lecture contains a number of Java examples: the source files a available for you to experiment with, in a companion zip archive.

3 Graphical Components A graphical interface consists of graphical components, such as: windows buttons menus text fields drop-down lists A user interacts with the application by actions such as: clicking on a button to choose a program option. making a choice from a menu. entering text in a text field. dragging a scroll bar. clicking on a window s close button

4 Event-driven Programming When you perform an action on a graphical component you generate an event. In event-driven programming the program has functions that respond to events. The program responds to events that the user generates in interacting with GUI components. The order of events is controlled by the user.

5 Java event-driven GUI Programming In Java, you can get the GUI components you want merely by asking for them. Most of the work has already been done and is contained in the Swing package. Swing contains windows, frames, buttons, menus and other components. A user interacts with a GUI application by causing events. The application must respond to events in the order they arrive. Each time the user interacts with a component, an event is sent to the application. Different events are sent to different parts of the application. The application has to work correctly no matter what the user does. Usually an application ignores events that it is not programmed to respond to.

6 Three Parts GUI Program 1. Graphical Components that make up the Graphical User Interface. Swing objects. You either use then as they are defined or extend them to your own classes. 2. Listener methods that receive the events and respond to them. You write these functions. They are called by the event-handling mechanism. Normally they in turn call application methods. 3. Application methods that do useful work Receive data from GUI components. Output data to GUI components for display. Not themselves concerned with the user interface.

7 Container Classes A GUI program consists of a collection of graphical components that are all placed inside one or more windows. You think of most components as being contained by a particular window. A container is an object that can hold other GUI components. Visually, a container is an area of the screen and the objects it contains are shown as smaller areas within it. In Java terminology, a window is a container. The buttons, sliders, icons and other GUI components are contained (by the container).

8 The AWT Hierarchy Object...(many more classes)... Component Button Canvas Container JComponent JPanel (swing)... (many swing components) Window Frame JFrame (swing) Panel Applet JApplet (swing)...(many other AWT Components)......(many classes)...

9 JFrame; JComponent and its subclasses Class JFrame, derived from java.awt.frame, is the usual main container of a GUI. The class JComponent, derived from awt.container is one of the base classes of swing. Here are some of its subclasses: JComponent AbstractButton JButton JLabel JPanel JSlider JTextComponent...(many more classes)...

10 Frames In Java, a frame is an outside window... that has nice borders, various buttons along the top border, and other built-in things. What you usually call a window Java calls a frame. A frame is a container object: GUI components can be placed in it. GUI-based application programs are usually organized around one or more frames.

11 Elements of a Frame

12 Small GUI Example - TestFrame1.java import java.awt.*; import javax.swing.*; public class TestFrame1 { private JFrame frame; public TestFrame1() { makeframe(); private void makeframe() { frame = new JFrame("Test Frame 1"); frame.setsize(200,100); frame.setvisible( true );

13 Small GUI Example: Comments The AWT is imported since it defines many classes that GUIs use. Not strictly mecessary here but most GUI programs must do this. Events are encapsulated by classes in the AWT package java.awt.* The Swing package is imported since that is where JFrame is defined. Notice the x in javax.swing.*. A JFrame object is constructed. The reference variable frame refers to the JFrame object. The object s setsize() method sets its width and height in pixels. The object s setvisible() method makes it visible on the screen. A call to setvisible(false) makes the frame invisible, but does not destroy the software object

14 Another Example: Extending the Frame Class A common way of writing a GUI is to define your own class by extending the JFrame class In the following example The class MyFrame extends the class JFrame MyFrame objects will be automatically re-painted when needed. The standard parts of the frame the background, the borders, the controls are drawn automatically by the Java system.

15 TestFrame2.java public class TestFrame2 extends JFrame { public TestFrame2() { super("test Frame 2"); makeframe(); private void makeframe() { setsize(200,100); setvisible( true );

16 Closing a Frame (Main Window) By default, when the user closes a frame with the close button, the frame is hidden. The frame is then invisible, the frame still exists and the program can make it visible again. If you want different behaviour, then you need to either register a window listener that handles window-closing events, or you need to specify default close behaviour using the setdefaultcloseoperation method:... public class TestFrame2 extends JFrame { //... (constructor as before) private void makeframe() { setsize(200,100); setvisible( true ); setdefaultcloseoperation(jframe.exit_on_close);

17 setdefaultcloseoperation(...); DO NOTHING ON CLOSE Don t do anything when the user requests that the window close. Instead, the program should probably use a window listener that performs some other action in its windowclosing method. HIDE ON CLOSE (the default for JDialog and JFrame) Hide the window when the user closes it. This removes the window from the screen but leaves it displayable. DISPOSE ON CLOSE (the default for JInternalFrame) Hide and dispose of the window when the user closes it. This removes the window from the screen and frees up any resources used by it. EXIT ON CLOSE (defined in the JFrame class) Exit the application, using System.exit(0).

18 Two special Java methods System.exit(int) causes program to finish a value 0 says no error mywindow.dispose() removes the window from the screen

19 Events and Event Handling When a GUI program is running, the user generates an event by interacting with a GUI component For a program to respond to an event there must be an event listener for it. The event listener object contains listener methods for various types of events If there is no listener for an event, the event is ignored.

20 Event Objects Events in Java are encapsulated in objects A WindowEvent object represents an event sent by a window (resize, close,...); An ActionEvent object represents an event sent by a button, text field, or menu; An ItemEvent object represents an event from a checkbox, choicebox, or list (choice of an item); A TextEvent represents an event from a text component An AdjustmentEvent represents an event from a scrollbar A KeyEvent represents an event from a key (press, release) A MouseEvent or MouseMotionEvent represents events a mouse (click, press, release, drag,...).

21 Event Listener Objects When an event is sent by the GUI component, a method in the listener is to be invoked. An event listener is an object in the program. In particular, for each event the program must respond to, the program must create an event listener object for the type of event. register the listener object with the GUI component that generates the event (or with a component that contains it.)

22 ButtonDemo1.java: A program with a button import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ButtonDemo1 extends JFrame { private JButton bchange; public ButtonDemo1() { // constructor super("button Demo"); makeframe(); private void makeframe() { bchange = new JButton("Click Me!"); // construct a Button add( bchange ); // add it to frame setdefaultcloseoperation(jframe.exit_on_close); setsize(200, 150); setvisible(true);

23 Comments To construct a JButton object, use new. Then add the button to the frame s content pane. The JButton now will be displayed when the frame is displayed, and respond visually when the user clicks on it. Clicking on the button generates events, but so far, there is no event handling. Also the button fills the whole content pane. We shall deal with this first, then return to the event handling.

24 Layout Managers When you add() buttons (and other components) to a content pane, a layout manager automatically decides what size they will be and where they will go. convenient, because you can add components without worrying about the details. You say what components you want and the layout manager lays them out in the picture. The layout manager sometimes makes odd choices. There are several kinds of layout managers; each one has a different style of positioning components. They are all classes in java.awt.

25 Layout Managers FlowLayout Components are set to a reasonable preferred size then positioned within container in the order they are added starting from top left, working from left to right; When available width is used, next row is started. GridLayout Number of rows and number of columns is specified; Components are set to a reasonable preferred size then positioned down the first column, then down the second column, etc. BorderLayout Components are assigned to North, South, East, West and Center regions of the container; they are sized to fill the whole of their region.

26 Setting the Layout Manager The problem with the example is that the content pane of a JFrame has a BorderLayout by default The button expands to fill the whole content pane due to BorderLayout policy. We fix this by using setlayout(new FlowLayout()) to override the default. (See also ButtonDemo2.java in the example sources archive.)... private void makeframe() { setlayout(new FlowLayout()); //set the layout bchange = new JButton("Click Me!"); // construct a Button add( bchange ); // add it to frame setdefaultcloseoperation(jframe.exit_on_close); setsize(200, 150); setvisible(true);

27 Result with Flow Layout Manager Only one item has been added to the flow layout: it is centred in the first (top) row of the flow layout.

28 Action Listener Back to the job of handling the event of a click on the button. We need an ActionListener object. This is a class which implements the java.awt.event.actionlistener interface. Interface ActionListener declares the single method: public void actionperformed( ActionEvent evt) ; The ActionEvent parameter it expects is a java.awt.event.actionevent object that represents an event (a button click). It contains information that can be used in responding to the event. Since ActionListener is an interface, you use it with a class that will implement the actionperformed() method. It is common to implement the interface in the same class that contains the button(s):

29 Button Example with Action Listener (1 of 2) ButtonDemo2.java import java.awt.*; import java.awt.event.*; //need this for ActionListener import javax.swing.*; public class ButtonDemo2 extends JFrame implements ActionListener { private JButton bchange; public ButtonDemo2() { super("button Demo"); makeframe(); // constructor

30 Button Example with Action Listener (2 of 2) private void makeframe() { setlayout(new FlowLayout()); // set the flow layout bchange = new JButton("Click Me!"); // construct a Button add( bchange ); // add it to frame bchange.addactionlistener(this); // Register listener setdefaultcloseoperation(jframe.exit_on_close); setsize(200, 150); setvisible(true); // Method to handle the button click // (required by implments contract) public void actionperformed(actionevent evt) { getcontentpane().setbackground(color.blue);

31 Result with ActionEvent Handling Before: After:

32 Summary so far General form of actionperformed: public void actionperformed(actionevent evt) { //look at data in ActionEvent: this may be useful //send reults to GUI components (invoke methods on them) General structure for GUI code: Declare widget Define attributes of widget Text, Colour, Editable?, Size,... Add to container Register an action listener Click on an ActionEvent source (eg button) creates an ActionEvent object which is sent to the actionperformed() methods of all ActionListeners registered with it.

33 A Further Example Develop a new program: A frame will hold two buttons, one labelled Red, the other Green. Use a FlowLayout layout manager so the buttons will be nicely positioned in the frame. When the button marked Red is clicked, the background will turn red. When the button marked Green is clicked, the background will turn green. There will be an event listener that listens to the clicks from both buttons. When the close button of the frame is clicked, the application will quit.

34 Questions to consider How many frames are there? What GUI components will it/they contain? What objects generate events? What objects receive events?

35 Questions to consider Just one frame, contining the Red button and the Green button. Both buttons send ActionEvents. The frame sends a WindowEvent (handled by default close operation).

36 TwoButtons.java (1 of 3) import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TwoButtons extends JFrame implements ActionListener { private JButton redbutton, grnbutton; public TwoButtons() { super ("Two Buttons"); makeframe();

37 Two Buttons: (2 of 3) private void makeframe() { redbutton = new JButton("Red"); grnbutton = new JButton("Green"); setlayout(new FlowLayout()); add(redbutton); add(grnbutton); redbutton.addactionlistener(this); grnbutton.addactionlistener(this); setdefaultcloseoperation( JFrame.EXIT_ON_CLOSE); setsize( 200, 150 ); setvisible(true);

38 Two Buttons: (3 of 3) public void actionperformed( ActionEvent evt) { // check which command has been sent if (evt.getactioncommand().equals("red")) getcontentpane().setbackground(color.red); else if (evt.getactioncommand().equals("green" )) getcontentpane().setbackground(color.green); Method getactioncommand() returns a string. In case of a button this is by default the label on the button.

39 An Alternative actionperformed Method ActionEvents have a method getsource() which returns an Object: the GUI component which sent the event. The actionperformed method can use getsource() to decide which button was clicked. public void actionperformed( ActionEvent evt) { // check which button sent the event if (evt.getsource() == redbutton) getcontentpane().setbackground(color.red); else if (evt.getsource() == grnbutton) getcontentpane().setbackground(color.green);

40 Output

41 Swing components JTextField and JLabel A JTextField is a box that contains text. The user can type text into the box and the program can get it and then use it as data. The program can write the results of a calculation to a JTextField Constructors JTextField() JTextField(String text) JTextField(int columns) JTextField(String text, int columns) A JTextField is used for input: when ENTER is typed, it issues an ActionEvent. A JLabel object simply displays text Constructor JLabel(String text) Useful for labelling other components: sych as JTextFields!

42 JTextField and JLabel Example (1 of 3) CharCounter.java User types text into upper text field and types ENTER Character count is then calculated and displayed in lower text field.

43 JTextField and JLabel Example (2 of 3) import java.awt.*; import java.awt.event.*; import javax.swing.*; public class CharCounter extends JFrame implements ActionListener { private JLabel inlabel = new JLabel("Enter text:"); private TextField intext = new TextField(15); private JLabel outlabel = new JLabel("Character count:"); private TextField outtext = new TextField(8);

44 JTextField and JLabel Example (3 of 3) public CharCounter() { //constructor super("character Counter"); setlayout(new FlowLayout()); add(inlabel); add(intext); add(outlabel); add(outtext) ; intext.addactionlistener(this); outtext.seteditable(false); setdefaultcloseoperation( JFrame.EXIT_ON_CLOSE); setsize(300, 120); setvisible(true); public void actionperformed(actionevent evt) { String stg = intext.gettext(); outtext.settext(""+stg.length());

45 seteditable() Notice outtext.seteditable(false); above. Use this JTextField method to prevent the user from entering text into the text field: The seteditable() method has one boolean parameter: true field can be edited by user false field cannot be edited by user In summary, common JTextField methods are String gettext() void settext(string s) seteditable(boolean b) boolean iseditable() void setbackground(color c) void addactionlister(actionlistener l)

46 JTextArea Similar to text field, but allows multiple lines Does not generate ActionEvent on ENTER, so not suitable for input Constructors JTextArea() JTextArea(String text) JTextArea(int rows, int columns) JTextArea(String text, int rows, int columns) Common methods String gettext() void settext(string s) append(string s) seteditable(boolean b) boolean iseditable() void setbackground(color c) void setlinewrap(boolean b) - enable/disable wrapping void setwrapstyleword(boolean b) - wrapping on complete words only

47 JTextArea Default behavior is to expand width-wise as text fills the area. setlinewrap(true) to make it wrap text to the following line. setwrapstyleword(true) to make it wrapping on whole words. With these settings the text area does not expand with-wise but does expand length-wise when it fills. You can also give the text area scroll-bars by adding it to the container inside a JScrollPane a research exercise for you!

48 JTextArea Example (1 of 3): TextAppender.java User types text into upper text field and types ENTER The text is appended to the text in the lower text area. The text in the text area wraps on whole words.

49 JTextArea Example (2 of 3) import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TextAppender extends JFrame implements ActionListener { private JLabel inlabel = new JLabel("Enter text:"); private TextField intext = new TextField(15); private JLabel outlabel = new JLabel("Accumulated text:"); private JTextArea outtext = new JTextArea(10, 15); public TextAppender() { //constructor super("text Appender"); setlayout(new FlowLayout()); add(inlabel); add(intext); add(outlabel); add(outtext) ; intext.addactionlistener(this);...

50 JTextArea Example (3 of 3) outtext.seteditable(false); outtext.setlinewrap(true); outtext.setwrapstyleword(true); setdefaultcloseoperation( JFrame.EXIT_ON_CLOSE); setsize(300, 300); setvisible(true); public void actionperformed(actionevent evt) { String stg = intext.gettext(); intext.settext(""); if (outtext.gettext().length() == 0) outtext.settext(stg); else outtext.append("; "+stg);

51 Design of an Application Design and implement a GUI application that converts a temperature expressed in Fahrenheit into Celsius. There are three parts to the design: 1. The Graphical User Interface. 2. Listener methods. 3. Application methods. First look at the application methods. The formula for converting Fahrenheit into Celsius is this: C = (F 32) 5/9

52 Program Architecture We shall have two classes: 1. a temperature class 2. a GUI class The temperature class FTemperature.java models the data: public class FTemperature { private double ftemp; public FTemperature() { //constructors public FTemperature(double ft) { ftemp = ft; public void setftemp(double ft) { ftemp = ft; public double getftemp() { return ftemp; public double convftoc() { return ((ftemp-32.0) * 5.0) / 9.0;

53 The GUI: 1 of 3 FToCGUI.java: preliminary version The user enters text (a String) into the Fahrenheit text field. The string will be converted into double format. The GUI consists of JLabels and JTextFields placed into a JFrame

54 The GUI: 2 of 3 import java.awt.*; import java.awt.event.*; import javax.swing.* ; import java.text.*; public class FToCGUI extends JFrame implements ActionListener { private JLabel title = new JLabel("Convert temp F to C"); private JLabel inlabel = new JLabel("Fahrenheit"); private JLabel outlabel = new JLabel("Celsius"); private JTextField infahr = new JTextField(7); private JTextField outcel = new JTextField(7);...

55 The GUI: 3 of 3 public FToCGUI() { //constructor super("f To C"); setlayout( new FlowLayout() ); add(title); add(inlabel); add(outlabel); add(infahr); add(outcel); infahr.addactionlistener(this); outcel.seteditable(false); setdefaultcloseoperation(jframe.exit_on_close); setsize(180, 150); setvisible(true); public void actionperformed( ActionEvent evt) { String userinput = infahr.gettext() ; double ft = Double.parseDouble(userInput); FTemperature temp = new FTemperature(ft); outcel.settext(string.format("%5.2f", temp.convftoc()));

56 Notes The FTemperature class knows nothing about the programs input, output. The FToCGUI needs to know about the FTemperature class: its actionperformed method constructs an instance and sends messages to it. NB the String.format() function: uses C-style formatting to make a display string with 5 digits and 2-place rounding. There is a problem with the prelminary version of FToCGUI: when the user resizes the window, the layout goes awry, the labels becoming separated from their text boxes. We need panels to fix this.

57 Panels, JPanel and Layout A panel is a container for components that does not create a window of its own. A logical rectangle on the screen. to help organise layout add components to a panel nested them: add components, panels to bigger panels or frame a panel is both a component and a container. JPanel is the swing version of a panel. Components added to a panel become inseparable. A panel corresponds to a rectangular section of the screen. Each panel has its own layout manager that lays out the components inside the panel. The default layout manager of JPanel is flow The content pane s layout manager arranges the panels as if each one were a single component. Panels do not have visible edges.

58 JPanel Example: FToCGUI.java Here is another version of the FToCGUI constructor: public FToCGUI() { //constructor super("f To C"); JPanel p1 = new JPanel(); p1.setlayout(new GridLayout(2,1)); JPanel p2 = new JPanel(); p2.setlayout(new GridLayout(2,1)); p1.add(inlabel); p2.add(outlabel); p1.add(infahr); p2.add(outcel); JPanel p3 = new JPanel(); //default layout = flow p3.add(p1); p3.add(p2); setlayout(new GridLayout(2,1)); add(title); add(p3); infahr.addactionlistener(this); outcel.seteditable(false); setdefaultcloseoperation(jframe.exit_on_close); setsize(180, 150); setvisible(true);

59 JPanel Comments The panel p1 uses a grid layout to hold the input box underneath its label; The panel p2 uses a grid layout to hold the output box underneath its label; These two compound components are added to panel p3 side by side, since default layout is flow; The title and p3 are added to the frame using grid layout. The point of all this is that the layout is more robust against resizing by the user. Compare the resizing behaviour of this with the earlier version.

60 Radio Buttons - JRadioButton Radio buttons generate action events just as do push buttons. Use addactionlistener(actionevent). Use setactioncommand(string) if you want your code to use getactioncommand() to recognise a paticular JRadioButton. Normally radio buttons are placed in groups in two ways: in a JPanel, to control how they are displayed, and also in a javax.swing.buttongroup, to control which buttons may be active simultaneously. Clicking a radio button deselects all other buttons in the group A button group is an object which must be constructed. Radio buttons are then added to it. Use boolean rbtn.isselected() to tell whether JRadioButton rbtn is selected.

61 JRadioButton Example: RadioButtons.java A simple calculator adds, substract, multiplies integers; Input is via two JTextFields; output is to a non-editable JTextField. You choose operation using three JRadioButtons in a ButtonGroup. Displays answer in decimal or hex: choose using two JRadioButtons in a ButtonGroup. See RadioButtons.java in example sources bundle.

62 JRadioButton Example ctd Look at constructor: Operation choice radio buttons are added to a JPanel for display at top; Display format choice radio buttons are added to a JPanel for display at bottom; The text boxes are each put in a JPanel with their label; The five JPanels are put in the frame (using another JPanel!) All JTextFields and JRadioButtons assigned an ActionListener. Look at the actionperformed method: ENTER in a text box, click on a radio button all trigger the same action (otherwise we could use getactioncommand() or getsource() to decide what to do); isselected() is used to see which operation is chosen; isselected() is used to see which output format is active;

63 Border Layout: BorderLayoutEx.java Default for JFrame s ContentPane; Organises window into 5 regions: BorderLayout.NORTH, BorderLayout.SOUTH, BorderLayout.EAST, BorderLayout.WEST, BorderLayout.CENTER Each region holds only one item, but it can be a JPanel or other container holding several components.

64 Border Layout in RadioButtons.java The simple calculator example uses BorderLayout: look again at its constructor. EAST, WEST not used; they shrink to a small (zero!) size. The operation choice radio button panel is NORTH. The output format choice radio button panel is SOUTH. The three text boxe panels are in an outer JPanel in the CENTER.

65 Mouse Event Handling Java provides two event listener interfaces for dealing with mouse events java.awt.event.mouselistener listens for MouseEvents generated when the mouse has a button pressed, released, or clicked, or when it enters or exits the bounds of the component. java.awt.event.mousemotionlistener listens for MouseEvents generated when the mouse is dragged or moved across the component. (Dragged = moved with a button down) MouseListener methods mouseentered(mouseevent evt); mouseexited(mouseevent evt) mousepressed(mouseevent evt) mousereleased(mouseevent evt) mouseclicked(mouseevent evt) MouseMotionListener methods mousemoved(mouseevent evt); mousedragged(mouseevent evt) java.awt.event.mouseevent has methods to report mouse coordinates and button state: refer to API documentation.

66 Example - MouseTracker.java A simple example: implements all mouse methods and report mouse position and button status on console: import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MouseTracker extends JFrame implements MouseListener, MouseMotionListener { public MouseTracker () { super("demonstration of Mouse Events"); addmouselistener (this); addmousemotionlistener(this); setdefaultcloseoperation(exit_on_close); setsize(400, 300); setvisible(true);...

67 Example - continued //helper public void smsg(string legend, MouseEvent evt) { System.out.printf("%s at [%d, %d]; button state = %d\n", legend, evt.getx(), evt.gety(), evt.getbutton()); //MouseListener methods... public void mouseclicked(mouseevent e) {smsg("clicked", e); public void mousepressed(mouseevent e) {smsg("pressed", e); public void mousereleased(mouseevent e) {smsg("released",e); public void mouseentered(mouseevent e) {smsg("entered", e); public void mouseexited (MouseEvent e) {smsg("exited", e); //MouseMotionListener methods public void mousedragged (MouseEvent e) {smsg("dragged", e); public void mousemoved (MouseEvent e) {smsg("moved", e);

68 Another Example; with low-level Graphics A slightly more amusing example is ClickMe.java

69 ClickMe Example ctd A simple reaction-timing game The game window is a single JPanel, in CENTER, plus a status display, SOUTH A string Click me is displayed in a random position on the game window and the time elapsed until a mouse click lands near it is displayed in the status display. A JPanel can be painted on using the low-level graphics methods of the java.awt.graphics object associated with the JPanel. Provide a method PaintComponent(Graphics g) in the game class, which extends the JPanel. It starts with super.paintcomponent(g) to paint the underlying JPanel, then does game-specific painting. Do not usually call super.paintcomponent() directly; rather, call JPanel s repaint() method to queue a repaint of the window.

70 PaintComponent(Graphics) In ClickMe.java... public void paintcomponent(graphics g) { super.paintcomponent(g); if (newpos) { //If target moved, g.setcolor(getbackground()); // erase it g.drawstring("click me!", x, y); x = rng.nextint(getwidth()-60); // new random y = 20+rng.nextInt(getHeight()-25); // coordinates reftime = System.currentTimeMillis(); newpos = false; if (x >=0 && y >=0) { //except for initial paint, g.setcolor(color.red); // show tgt at current g.drawstring("click me!", x, y); // position

71 Dialogues Displayed to give information or solicit input Modal dialogues block all other interaction until dismissed Non-modal dialogues allow other interaction; may be undesirable javax.swing.joptionpane provides some standard dialogues Message dialogue: message text plus an OK button. Confirm dialogue has yes/no/cancel options Input dialogue has a prompt and an input field; OK/cancel buttons

72 Message Dialogue: Dialogues.java... JOptionPane.showMessageDialog(frame, "The message", "Title", JOptionPane.INFORMATION_MESSAGE);... The last parameter chooses the information icon.

73 Confirm Dialogue: Dialogues.java... int cnf = JOptionPane.showConfirmDialog(frame, "Question?"); System.out.printf("The result was %d\n", cnf);... YES 0, NO 1, CANCEL 2

74 Input Dialogue: Dialogues.java... String inp = JOptionPane.showInputDialog(frame, "Input your data", "Input Dialogue", JOptionPane.QUESTION_MESSAGE); System.out.printf("The input was %s \n", inp);... The last parameter chooses the question icon. The return value on OK is the input string; CANCEL null

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics Topic 9: Swing Outline Swing = Java's GUI library Swing is a BIG library Goal: cover basics give you concepts & tools for learning more Assignment 7: Expand moving shapes from Assignment 4 into game. "Programming

More information

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more Swing = Java's GUI library Topic 9: Swing Swing is a BIG library Goal: cover basics give you concepts & tools for learning more Assignment 5: Will be an open-ended Swing project. "Programming Contest"

More information

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun! Swing = Java's GUI library Topic 9: Swing Swing is a BIG library Goal: cover basics give you concepts & tools for learning more Why are we studying Swing? 1. Useful & fun! 2. Good application of OOP techniques

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

Starting Out with Java: From Control Structures Through Objects Sixth Edition

Starting Out with Java: From Control Structures Through Objects Sixth Edition Starting Out with Java: From Control Structures Through Objects Sixth Edition Chapter 12 A First Look at GUI Applications Chapter Topics 12.1 Introduction 12.2 Creating Windows 12.3 Equipping GUI Classes

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

User interfaces and Swing

User interfaces and Swing User interfaces and Swing Overview, applets, drawing, action listening, layout managers. APIs: java.awt.*, javax.swing.*, classes names start with a J. Java Lectures 1 2 Applets public class Simple extends

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

CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT CSE 143 Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia http://www.cs.washington.edu/331/

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

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

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling Frames, GUI and events Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling Introduction to Swing The Java AWT (Abstract Window Toolkit)

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

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

GUI in Java TalentHome Solutions

GUI in Java TalentHome Solutions GUI in Java TalentHome Solutions AWT Stands for Abstract Window Toolkit API to develop GUI in java Has some predefined components Platform Dependent Heavy weight To use AWT, import java.awt.* Calculator

More information

Graphics programming. COM6516 Object Oriented Programming and Design Adam Funk (originally Kirill Bogdanov & Mark Stevenson)

Graphics programming. COM6516 Object Oriented Programming and Design Adam Funk (originally Kirill Bogdanov & Mark Stevenson) Graphics programming COM6516 Object Oriented Programming and Design Adam Funk (originally Kirill Bogdanov & Mark Stevenson) Overview Aims To provide an overview of Swing and the AWT To show how to build

More information

Introduction to Graphical User Interfaces (GUIs) Lecture 10 CS2110 Fall 2008

Introduction to Graphical User Interfaces (GUIs) Lecture 10 CS2110 Fall 2008 Introduction to Graphical User Interfaces (GUIs) Lecture 10 CS2110 Fall 2008 Announcements A3 is up, due Friday, Oct 10 Prelim 1 scheduled for Oct 16 if you have a conflict, let us know now 2 Interactive

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

Java. GUI building with the AWT

Java. GUI building with the AWT Java GUI building with the AWT AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many applications Uses the controls defined by your OS therefore

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

Programming graphics

Programming graphics Programming graphics Need a window javax.swing.jframe Several essential steps to use (necessary plumbing ): Set the size width and height in pixels Set a title (optional), and a close operation Make it

More information

Introduction. Introduction

Introduction. Introduction Introduction Many Java application use a graphical user interface or GUI (pronounced gooey ). A GUI is a graphical window or windows that provide interaction with the user. GUI s accept input from: the

More information

Computer Science 210: Data Structures. Intro to Java Graphics

Computer Science 210: Data Structures. Intro to Java Graphics Computer Science 210: Data Structures Intro to Java Graphics Summary Today GUIs in Java using Swing in-class: a Scribbler program READING: browse Java online Docs, Swing tutorials GUIs in Java Java comes

More information

17 GUI API: Container 18 Hello world with a GUI 19 GUI API: JLabel 20 GUI API: Container: add() 21 Hello world with a GUI 22 GUI API: JFrame: setdefau

17 GUI API: Container 18 Hello world with a GUI 19 GUI API: JLabel 20 GUI API: Container: add() 21 Hello world with a GUI 22 GUI API: JFrame: setdefau List of Slides 1 Title 2 Chapter 13: Graphical user interfaces 3 Chapter aims 4 Section 2: Example:Hello world with a GUI 5 Aim 6 Hello world with a GUI 7 Hello world with a GUI 8 Package: java.awt and

More information

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

Graphics. Lecture 18 COP 3252 Summer June 6, 2017 Graphics Lecture 18 COP 3252 Summer 2017 June 6, 2017 Graphics classes In the original version of Java, graphics components were in the AWT library (Abstract Windows Toolkit) Was okay for developing simple

More information

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011 GUI DYNAMICS Lecture July 26 CS2110 Summer 2011 GUI Statics and GUI Dynamics 2 Statics: what s drawn on the screen Components buttons, labels, lists, sliders, menus,... Containers: components that contain

More information

Graphical User Interfaces 2

Graphical User Interfaces 2 Graphical User Interfaces 2 CSCI 136: Fundamentals CSCI 136: Fundamentals of Computer of Science Computer II Science Keith II Vertanen Keith Vertanen Copyright 2011 Extending JFrame Dialog boxes Overview

More information

Datenbank-Praktikum. Universität zu Lübeck Sommersemester 2006 Lecture: Swing. Ho Ngoc Duc 1

Datenbank-Praktikum. Universität zu Lübeck Sommersemester 2006 Lecture: Swing. Ho Ngoc Duc 1 Datenbank-Praktikum Universität zu Lübeck Sommersemester 2006 Lecture: Swing Ho Ngoc Duc 1 Learning objectives GUI applications Font, Color, Image Running Applets as applications Swing Components q q Text

More information

Building a GUI in Java with Swing. CITS1001 extension notes Rachel Cardell-Oliver

Building a GUI in Java with Swing. CITS1001 extension notes Rachel Cardell-Oliver Building a GUI in Java with Swing CITS1001 extension notes Rachel Cardell-Oliver Lecture Outline 1. Swing components 2. Building a GUI 3. Animating the GUI 2 Swing A collection of classes of GUI components

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

CSE 331. Event- driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

CSE 331. Event- driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT CSE 331 Event- driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT Lecturer: Michael Hotan slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer,

More information

KF5008 Program Design & Development. Lecture 1 Usability GUI Design and Implementation

KF5008 Program Design & Development. Lecture 1 Usability GUI Design and Implementation KF5008 Program Design & Development Lecture 1 Usability GUI Design and Implementation Types of Requirements Functional Requirements What the system does or is expected to do Non-functional Requirements

More information

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages.

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages. 1 CS257 Computer Science I Kevin Sahr, PhD Lecture 14: Graphical User Interfaces Command-Line Applications 2 The programs we've explored thus far have been text-based applications A Java application is

More information

CSE 331. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

CSE 331. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT CSE 331 Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia http://www.cs.washington.edu/331/

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

Lecture 5: Java Graphics

Lecture 5: Java Graphics Lecture 5: Java Graphics CS 62 Spring 2019 William Devanny & Alexandra Papoutsaki 1 New Unit Overview Graphical User Interfaces (GUI) Components, e.g., JButton, JTextField, JSlider, JChooser, Containers,

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

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

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

Graphical User Interfaces 2

Graphical User Interfaces 2 Graphical User Interfaces 2 CSCI 136: Fundamentals of Computer Science II Keith Vertanen Copyright 2014 2011 Extending JFrame Dialog boxes Overview Ge

More information

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming Objectives: Last revised 1/15/10 1. To introduce the notion of a component and some basic Swing components (JLabel, JTextField, JTextArea,

More information

BASICS OF GRAPHICAL APPS

BASICS OF GRAPHICAL APPS CSC 2014 Java Bootcamp Lecture 7 GUI Design BASICS OF GRAPHICAL APPS 2 Graphical Applications So far we ve focused on command-line applications, which interact with the user using simple text prompts In

More information

2110: GUIS: Graphical User Interfaces

2110: GUIS: Graphical User Interfaces 2110: GUIS: Graphical User Interfaces Their mouse had a mean time between failure of a week it would jam up irreparably, or... jam up on the table--... It had a flimsy cord whose wires would break. Steve

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

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

Part I: Learn Common Graphics Components

Part I: Learn Common Graphics Components OOP GUI Components and Event Handling Page 1 Objectives 1. Practice creating and using graphical components. 2. Practice adding Event Listeners to handle the events and do something. 3. Learn how to connect

More information

CHAPTER 2. Java Overview

CHAPTER 2. Java Overview Networks and Internet Programming (0907522) CHAPTER 2 Java Overview Instructor: Dr. Khalid A. Darabkh Objectives The objectives of this chapter are: To discuss the classes present in the java.awt package

More information

CS 251 Intermediate Programming GUIs: Components and Layout

CS 251 Intermediate Programming GUIs: Components and Layout CS 251 Intermediate Programming GUIs: Components and Layout Brooke Chenoweth University of New Mexico Fall 2017 import javax. swing.*; Hello GUI public class HelloGUI extends JFrame { public HelloGUI ()

More information

Graphical User Interfaces. Swing. Jose Jesus García Rueda

Graphical User Interfaces. Swing. Jose Jesus García Rueda Graphical User Interfaces. Swing Jose Jesus García Rueda Introduction What are the GUIs? Well known examples Basic concepts Graphical application. Containers. Actions. Events. Graphical elements: Menu

More information

Lecture 3: Java Graphics & Events

Lecture 3: Java Graphics & Events Lecture 3: Java Graphics & Events CS 62 Fall 2017 Kim Bruce & Alexandra Papoutsaki Text Input Scanner class Constructor: myscanner = new Scanner(System.in); can use file instead of System.in new Scanner(new

More information

Graphical User Interfaces 2

Graphical User Interfaces 2 Graphical User Interfaces 2 CSCI 136: Fundamentals of Computer Science II Keith Vertanen Copyright 2011 Extending JFrame Dialog boxes Ge?ng user input Overview Displaying message or error Listening for

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

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

Previously, we have seen GUI components, their relationships, containers, layout managers. Now we will see how to paint graphics on GUI components

Previously, we have seen GUI components, their relationships, containers, layout managers. Now we will see how to paint graphics on GUI components CS112-Section2 Hakan Guldas Burcin Ozcan Meltem Kaya Muge Celiktas Notes of 6-8 May Graphics Previously, we have seen GUI components, their relationships, containers, layout managers. Now we will see how

More information

JAVA NOTES GRAPHICAL USER INTERFACES

JAVA NOTES GRAPHICAL USER INTERFACES 1 JAVA NOTES GRAPHICAL USER INTERFACES Terry Marris 24 June 2001 5 TEXT AREAS 5.1 LEARNING OUTCOMES By the end of this lesson the student should be able to understand how to get multi-line input from the

More information

Parts of a Contract. Contract Example. Interface as a Contract. Wednesday, January 30, 13. Postcondition. Preconditions.

Parts of a Contract. Contract Example. Interface as a Contract. Wednesday, January 30, 13. Postcondition. Preconditions. Parts of a Contract Syntax - Method signature Method name Parameter list Return type Semantics - Comments Preconditions: requirements placed on the caller Postconditions: what the method modifies and/or

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

Graphical User Interface (GUI) components in Java Applets. With Abstract Window Toolkit (AWT) we can build an applet that has the basic GUI

Graphical User Interface (GUI) components in Java Applets. With Abstract Window Toolkit (AWT) we can build an applet that has the basic GUI CBOP3203 Graphical User Interface (GUI) components in Java Applets. With Abstract Window Toolkit (AWT) we can build an applet that has the basic GUI components like button, text input, scroll bar and others.

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

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

Chapter 1 GUI Applications

Chapter 1 GUI Applications Chapter 1 GUI Applications 1. GUI Applications So far we've seen GUI programs only in the context of Applets. But we can have GUI applications too. A GUI application will not have any of the security limitations

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

DM503 Programming B. Peter Schneider-Kamp.

DM503 Programming B. Peter Schneider-Kamp. DM503 Programming B Peter Schneider-Kamp petersk@imada.sdu.dk! http://imada.sdu.dk/~petersk/dm503/! ADVANCED OBJECT-ORIENTATION 2 Object-Oriented Design classes often do not exist in isolation from each

More information

Basicsof. JavaGUI and SWING

Basicsof. JavaGUI and SWING Basicsof programming3 JavaGUI and SWING GUI basics Basics of programming 3 BME IIT, Goldschmidt Balázs 2 GUI basics Mostly window-based applications Typically based on widgets small parts (buttons, scrollbars,

More information

3 Combining Widgets to create Graphical User Interfaces

3 Combining Widgets to create Graphical User Interfaces (Graphical User Interfaces - 41) 3 Combining Widgets to create Graphical User Interfaces 3.1 The Ressource Concept Goal: How to compose widgets to create complete user interfaces Problem: Composition has

More information

CS 2113 Software Engineering

CS 2113 Software Engineering CS 2113 Software Engineering Java 5 - GUIs Import the code to intellij https://github.com/cs2113f18/template-j-5.git Professor Tim Wood - The George Washington University Class Hierarchies Abstract Classes

More information

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming Objectives: Last revised March 2, 2017 1. To introduce the notion of a component and some basic Swing components (JLabel, JTextField,

More information

All the Swing components start with J. The hierarchy diagram is shown below. JComponent is the base class.

All the Swing components start with J. The hierarchy diagram is shown below. JComponent is the base class. Q1. If you add a component to the CENTER of a border layout, which directions will the component stretch? A1. The component will stretch both horizontally and vertically. It will occupy the whole space

More information

Unit 6: Graphical User Interface

Unit 6: Graphical User Interface Faculty of Computer Science Programming Language 2 Object oriented design using JAVA Dr. Ayman Ezzat Email: ayman@fcih.net Web: www.fcih.net/ayman Unit 6: Graphical User Interface 1 1. Overview of the

More information

Chapter 6: Graphical User Interfaces

Chapter 6: Graphical User Interfaces Chapter 6: Graphical User Interfaces CS 121 Department of Computer Science College of Engineering Boise State University April 21, 2015 Chapter 6: Graphical User Interfaces CS 121 1 / 36 Chapter 6 Topics

More information

Example: Building a Java GUI

Example: Building a Java GUI Steven Zeil October 25, 2013 Contents 1 Develop the Model 3 2 Develop the layout of those elements 4 3 Add listeners to the elements 12 4 Implement custom drawing 15 1 The StringArt Program To illustrate

More information

Chapter 7: A First Look at GUI Applications

Chapter 7: A First Look at GUI Applications Chapter 7: A First Look at GUI Applications Starting Out with Java: From Control Structures through Objects Fourth Edition by Tony Gaddis Addison Wesley is an imprint of 2010 Pearson Addison-Wesley. All

More information

Agenda. Container and Component

Agenda. Container and Component Agenda Types of GUI classes/objects Step-by-step guide to create a graphic user interface Step-by-step guide to event-handling PS5 Problem 1 PS5 Problem 2 Container and Component There are two types of

More information

Chapter 17 Creating User Interfaces

Chapter 17 Creating User Interfaces Chapter 17 Creating User Interfaces 1 Motivations A graphical user interface (GUI) makes a system user-friendly and easy to use. Creating a GUI requires creativity and knowledge of how GUI components work.

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

Example: Building a Java GUI

Example: Building a Java GUI Steven Zeil October 25, 2013 Contents 1 Develop the Model 2 2 Develop the layout of those elements 3 3 Add listeners to the elements 9 4 Implement custom drawing 12 1 The StringArt Program To illustrate

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

INTRODUCTION TO (GUIS)

INTRODUCTION TO (GUIS) INTRODUCTION TO GRAPHICAL USER INTERFACES (GUIS) Lecture 10 CS2110 Fall 2009 Announcements 2 A3 will be posted shortly, please start early Prelim 1: Thursday October 14, Uris Hall G01 We do NOT have any

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

Swing UI. Powered by Pentalog. by Vlad Costel Ungureanu for Learn Stuff

Swing UI. Powered by Pentalog. by Vlad Costel Ungureanu for Learn Stuff Swing UI by Vlad Costel Ungureanu for Learn Stuff User Interface Command Line Graphical User Interface (GUI) Tactile User Interface (TUI) Multimedia (voice) Intelligent (gesture recognition) 2 Making the

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

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

11/6/15. Objec&ves. RouleQe. Assign 8: Understanding Code. Assign 8: Bug. Assignment 8 Ques&ons? PROGRAMMING PARADIGMS

11/6/15. Objec&ves. RouleQe. Assign 8: Understanding Code. Assign 8: Bug. Assignment 8 Ques&ons? PROGRAMMING PARADIGMS Objec&ves RouleQe Assign 8: Refactoring for Extensibility Programming Paradigms Introduc&on to GUIs in Java Ø Event handling Nov 6, 2015 Sprenkle - CSCI209 1 Nov 6, 2015 Sprenkle - CSCI209 2 Assign 8:

More information

Unit 7: Event driven programming

Unit 7: Event driven programming Faculty of Computer Science Programming Language 2 Object oriented design using JAVA Dr. Ayman Ezzat Email: ayman@fcih.net Web: www.fcih.net/ayman Unit 7: Event driven programming 1 1. Introduction 2.

More information

Chapter 12 Advanced GUIs and Graphics

Chapter 12 Advanced GUIs and Graphics Chapter 12 Advanced GUIs and Graphics Chapter Objectives Learn about applets Explore the class Graphics Learn about the classfont Explore the classcolor Java Programming: From Problem Analysis to Program

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

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

Graphical User Interface (GUI)

Graphical User Interface (GUI) Graphical User Interface (GUI) An example of Inheritance and Sub-Typing 1 Java GUI Portability Problem Java loves the idea that your code produces the same results on any machine The underlying hardware

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

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

Dr. Hikmat A. M. AbdelJaber

Dr. Hikmat A. M. AbdelJaber Dr. Hikmat A. M. AbdelJaber JWindow: is a window without a title bar or move controls. The program can move and resize it, but the user cannot. It has no border at all. It optionally has a parent JFrame.

More information

Graphical User Interfaces (GUIs)

Graphical User Interfaces (GUIs) CMSC 132: Object-Oriented Programming II Graphical User Interfaces (GUIs) Department of Computer Science University of Maryland, College Park Model-View-Controller (MVC) Model for GUI programming (Xerox

More information

G51PRG: Introduction to Programming Second semester Applets and graphics

G51PRG: Introduction to Programming Second semester Applets and graphics G51PRG: Introduction to Programming Second semester Applets and graphics Natasha Alechina School of Computer Science & IT nza@cs.nott.ac.uk Previous two lectures AWT and Swing Creating components and putting

More information

Java Swing. Recitation 11/(20,21)/2008. CS 180 Department of Computer Science, Purdue University

Java Swing. Recitation 11/(20,21)/2008. CS 180 Department of Computer Science, Purdue University Java Swing Recitation 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University Announcements Project 8 is out Milestone due on Dec 3rd, 10:00 pm Final due on Dec 10th, 10:00 pm No classes,

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

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp DM550 / DM857 Introduction to Programming Peter Schneider-Kamp petersk@imada.sdu.dk http://imada.sdu.dk/~petersk/dm550/ http://imada.sdu.dk/~petersk/dm857/ GRAPHICAL USER INTERFACES 2 HelloWorld Reloaded

More information

Graphical User Interface (GUI) and Object- Oriented Design (OOD)

Graphical User Interface (GUI) and Object- Oriented Design (OOD) Chapter 6,13 Graphical User Interface (GUI) and Object- Oriented Design (OOD) Objectives To distinguish simple GUI components. To describe the Java GUI API hierarchy. To create user interfaces using frames,

More information

encompass a group of features for building Graphical User Interfaces (GUI).

encompass a group of features for building Graphical User Interfaces (GUI). Java GUI (intro) JFC Java Foundation Classes encompass a group of features for building Graphical User Interfaces (GUI). javax.swing.* used for building GUIs. Some basic functionality is already there

More information

Programming Mobile Devices J2SE GUI

Programming Mobile Devices J2SE GUI Programming Mobile Devices J2SE GUI University of Innsbruck WS 2009/2010 thomas.strang@sti2.at Graphical User Interface (GUI) Why is there more than one Java GUI toolkit? AWT write once, test everywhere

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