F27SB2 Software Development 2 Lecture 9: Java GUIs 6.

Slides:



Advertisements
Similar presentations
Graphical User Interfaces (Part IV)
Advertisements

Graphical User Interface Bonus slides Interaction Between Components & Drawing.
Building Applications Dialogs Passing data between windows Validating Input using FocusListeners.
F27SB2 Software Development 2 Lecture 10: Java GUIs 7.
F27SB2 Software Development 2 Lecture 7: State Diagrams.
GUI and Swing, part 2 The illustrated edition. Scroll bars As we have previously seen, a JTextArea has a fixed size, but the amount of text that can be.
1 Chapter 23 Menus, Toolbars, Dialogs, and Internal Frames.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
CPSC150 Week 12 Graphical User Interfaces Chapter 11.
Scott Grissom, copyright 2006Ch 11: GUI Slide 1 Graphical User Interfaces (Ch 11) Careful design of a graphical user interface is key to a viable software.
GUI and Event-Driven Programming Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.
CC1007NI: Further Programming Week 5 Dhruba Sen Module Leader (Islington College)
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
SWING. Split Panes JSplitPane is used to divide two (and only two) Components. The two Components are graphically divided based on the look and feel implementation,
Intro to GUIs (Graphical User Interfaces) Section 2.5Intro. to GUIs: a GUI Greeter Section 3.7Graphical/Internet Java: Einstein's Equation.
Creating User Interfaces F JComponent F JButton F JLabel F JTextField F JTextArea F JComboBox F JList F JCheckBox F JRadioButton F Dialogs.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog.
Java Programming, Second Edition Chapter Five Input and Selection.
Java Programming 1 Java Programming II Events, AWT, and Swing.
CS Lecture 01 Frames and Components and events Lynda Thomas
SWING. AbstractButton Swing Buttons are subclasses of the AbstractButton class, which extends JComponent. Abstract class javax.swing.AbstractButton AbstractButton.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
GUI Clients 1 Enterprise Applications CE00465-M Clients with Graphical User Interfaces.
CIS Intro to JAVA Lecture Notes Set June-05 GUI Programming – Assignment 5 Notes.
Graphical User Interfaces (Part 2) 1. View  view  presents the user with a sensory (visual, audio, haptic) representation of the model state  a user.
Event Driven Programming. Event-driven Programming In the early days of computing communication with the outside world was accomplished using a technique.
OOP (Java): GUI Intro/ OOP Objectives – –use an image viewer application to introduce Java's GUI features Semester 2,
Programming with Java’s Swing API February 4, 2003 CMPS Advanced Programming Graphical User Interfaces.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 15 Creating User.
CS1054: Lecture 21 - Graphical User Interface. Graphical User Interfaces vs. Text User Interface.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
Option Panes CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
GUI Graphical User Interface Each onscreen component and window is an object Object interaction makes communication and scoping challenging Event-driven.
Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus.
SD2054 Software Development. By the end of this lecture you should be able to: Advanced Graphics Programming create pull down menus create combo boxes.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Basics of GUI Programming Chapter 11 and Chapter 22.
Software Construction LAB 08 Java Programming with SWING GUI Builder.
Computer Science [3] Java Programming II - Laboratory Course Lab 4 -1 : Introduction to Graphical user interface GUI Components Faculty of Engineering.
JOptionPane Class JOptionPane makes it easy to pop up a standard dialog box that prompts users for a value or informs them of something. While the JOptionPane.
SE-1020 Dr. Mark L. Hornick 1 Graphical User Interfaces.
Advanced Java Class Events. change in state initiated by system or user java.util.EventObject java.awt.event java.swing.event.
Creating and Using Dialogs ● A dialog is a box that pops up and prompts the user for a value or informs them of something ● One way: directly create objects.
1 IM103 week 10 Enhancing the user interface By the end of lecture you should be able to:  distinguish between heavyweight and lightweight components;
1 CSE 331 Composite Layouts; Decorators slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
View  view  presents the user with a sensory (visual, audio, haptic) representation of the model state  a user interface element (the user interface.
Objects First With Java A Practical Introduction Using BlueJ Building Graphical User Interfaces (GUIs) Week
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.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
제 14 장 고급 스윙 컴포넌트.
1 A Quick Java Swing Tutorial. 2 Introduction Swing – A set of GUI classes –Part of the Java's standard library –Much better than the previous library:
Dept. of CSIE, National University of Tainan 10/21/2012 Working with Swing.
Java Visual Applications CSIS 3701: Advanced Object Oriented Programming.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
A Quick Java Swing Tutorial
Building a Swing Interface
Object-Oriented Programming (Java), Unit 22
A Quick Java Swing Tutorial
CSE 114 – Computer Science I Event Programming
Ellen Walker Hiram College
Message, Input, Confirm, and Specialized Dialogs
JOptionPane Dialogs javax.swing.JOptionPane is a class for creating dialog boxes. Has both static methods and instance methods for dialogs. Easy to create.
CS18000: Problem Solving and Object-Oriented Programming
Message, Input, and Confirm Dialogs
1 Graphical User Interfaces
A Quick Java Swing Tutorial
CS431 ws99 Half Text Half Graphics
Presentation transcript:

F27SB2 Software Development 2 Lecture 9: Java GUIs 6

Overview rolling example of a text editor INITIALEDITING select open/ file contents editable select new/ empty contents editable select edit/ contents changed select save/ contents written to file select close/ contents written to file select exit/ return to host system

Overview will now focus on new methods to: – display/manipulate scrollable text – open/save files from directory – conduct simple standard dialogs

Editable text JTextField is one line of text JTextArea extends javax.swing.text.JTextComponent multiple lines of plain, unformatted text user can edit text JTextArea (String text, int rows, int columns) – text ==> text to be edited – rows ==> number of displayed rows – columns ==> number of displayed columns

Editable text public String setText(String text) overrides all text with text public String append(String text) adds text at end public String getText() returns text as single String NB no drag & drop NB no right button menu

Editable text import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; class Edit11 extends JFrame { JTextArea ta; Edit11() { ta = new JTextArea("",24,80); add(BorderLayout.CENTER,ta); }

Editable text class TestEdit11 { static BufferedReader fin; public static void main(String [] args) throws IOException { Edit11 e = new Edit11(); e.setTitle("edit1"); e.setSize(400,200); e.setVisible(true); e.addWindowListener (new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } });

Editable text fin = new BufferedReader (new FileReader("test.txt")); String next = fin.readLine(); while(next!=null) { e.ta.append(next); next = fin.readLine(); }

Editable text test.txt: Once upon a time there were three little computers called Boris, Doris and Horace. One day Boris was having difficulty opening a file. "I just can't open this file!" said Boris sadly. "Maybe there's something wrong with my H: drive." "Maybe it's a network problem," said Doris. "I can't access the printer." "And I can't get onto the Internet," said Horace. Just then, along came the Nice Computer Manager.

Editable text "Hello computers!" said the Nice Computer Manager. "Hello Nice Computer Manager!" said the computers. "How are you all today?" asked the Nice Computer Manager. "I can't open a file," said Boris, sadly. "And I can't access the printer," said Doris, glumly. "And I can't get onto the Internet," said Horace, gloomily. "Don't worry," said the Nice Computer Manager. "You're all being replaced by a shiny new laptop!" The End.

Line wrap all text on one line need to wrap lines public void setLineWrap(Boolean wrap)

Line wrap Edit12() { ta = new JTextArea("",24,80); ta.setLineWrap(true); add(BorderLayout.CENTER,ta); }

Line termination need to add line termination public static void main(String [] args) throws IOException { Edit13 e = new Edit13();... String next = fin.readLine(); while(next!=null) { e.ta.append(next+"\n"); next = fin.readLine(); }

Line termination can add/delete text using the mouse & keyboard

Scrolling text doesn’t scroll JScrollPane extends JComponent implements Accessible, ScrollPaneConstants public JScrollPane() public JScrollPane(Component view) constructs a JScrollPane container for Component view allows horizontal & vertical scrolling

Scrolling can change presence of these with: public setHorizontalScrollBarPolicy(int policy) public setVerticalScrollBarPolicy(int policy) where policy is a ScrollPaneConstants : – HORIZONTAL_SCROLLBAR_ or – VERTICAL_SCROLLBAR_ followed by: – ALWAYS AS_NEEDED or NEVER

Scrolling add Component with: public void setViewportView(Component view) change Edit1 to provide vertical scrolling have wrap round so don’t need horizontal scrolling horizontal scrolling is poor for text manipulation

Scrolling... class Edit2 extends JFrame { JScrollPane sp; JTextArea ta; Edit2() { sp = new JScrollPane(); sp.setHorizontalScrollBarPolicy (ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); ta = new JTextArea("",24,80); ta.setLineWrap(true); sp.setViewportView(ta); add(BorderLayout.CENTER,sp); }

Scrolling class TestEdit2 { static BufferedReader fin; public static void main(String [] args) throws IOException { Edit2 e = new Edit2();... } }

Menus all contemporary windows interfaces provide menu bars with: selectable pop-up/pull-down menus of: selectable menu items JMenuBar extends JComponent implements Accessible, MenuElement JMenuBar() creates a new JMenuBar can be added to any component

Menus setJMenuBar(JMenuBar menubar ) places menubar at the top of a JFrame JMenu extends JMenuItem implements Accessible, MenuElementJMenu(String s) creates a new JMenu identified by s jmenubar.add(jmenu) adds jmenu to jmenubar

Menus e.g. add menu bar to editor with a menu for File with options for Open, New, Close and Exit... class Edit3 extends JFrame { JScrollPane sp; JTextArea ta; JMenuBar jb; JMenu file; JMenuItem MNew,MOpen,MClose,MExit;

Menus Edit31() { sp = new JScrollPane(); sp.setHorizontalScrollBarPolicy (ScrollPaneConstants. HORIZONTAL_SCROLLBAR_NEVER); ta = new JTextArea("",24,80); ta.setLineWrap(true); sp.setViewportView(ta); add(BorderLayout.CENTER,sp); jb = new JMenuBar(); file = new JMenu("File");

Menus MNew = new JMenuItem("New"); MOpen = new JMenuItem("Open"); MClose = new JMenuItem("Close"); MExit = new JMenuItem("Exit"); file.add(MNew); file.add(MOpen); file.add(MClose); file.add(MExit); jb.add(file); setJMenuBar(jb); }

Menus class TestEdit31 { static BufferedReader fin; public static void main(String [] args) throws IOException { Edit31 e = new Edit31();... }

Menu now need to associate actions with the options AbstractButton causes ActionEvent class Edit32 extends JFrame implements ActionListener {... Edit32() {... MNew.addActionListener(this); MOpen.addActionListener(this); MClose.addActionListener(this); MExit.addActionListener(this);... }

Menu public void actionPerformed(ActionEvent e) { if(e.getSource()==MNew) System.out.println("New"); else if(e.getSource()==MOpen) System.out.println("Open"); else if(e.getSource()==MClose) System.out.println("Close"); else if(e.getSource()==MExit) System.out.println("Exit"); } } class TestEdit32{... }

Menu

State machine next develop state machine within actionPerformed introduce state variable: boolean editing; setEditable(boolean e) changes Component editable status initially, not editing and text area is not editable

State machine class Edit33 extends JFrame implements ActionListener {... Edit33() {... ta.setEditable(false); editing = false;... }

State machine public void actionPerformed(ActionEvent e) { if(!editing) { if(e.getSource()==MNew) { System.out.println("New"); editing = true; ta.setEditable(true); } else if(e.getSource()==MOpen) { System.out.println("Open"); editing = true; ta.setEditable(true); } else

State machine if(e.getSource()==MExit) System.out.println("Exit"); } else if(e.getSource()==MClose) { System.out.println("Close"); editing = false; ta.setEditable(false); }... now add actions

File chooser Swing provides a standard GUI for file system navigation JFileChooser extends JComponent implements Accessible JFileChooser() create JFileChooser component for current directory standard dialogues

File chooser showOpenDialog(Component parent) to open file

File chooser showSaveDialog(Component parent) to save file

File chooser both return constants: – APPROVE_OPTION for Open/Save buttons – CANCEL_OPTION for Cancel button File getSelectedFile() returns selected file from JFileChooser

File chooser... class Edit34 extends JFrame implements ActionListener {... JFileChooser files;... files = new JFileChooser();... public void doOpen() { try{ int response = files.showOpenDialog(this); if(response==JFileChooser.APPROVE_OPTION) { File f = files.getSelectedFile(); BufferedReader fin = new BufferedReader(new FileReader(f));

File chooser String next = fin.readLine(); while(next!=null) { ta.append(next+"\n"); next = fin.readLine(); } fin.close(); editing = true; ta.setEditable(true); } }catch(IOException e){}; }

File chooser public void doClose() { try { int response = files.showSaveDialog(this); if(response==JFileChooser.APPROVE_OPTION) { File f = files.getSelectedFile(); BufferedWriter fout = new BufferedWriter(new FileWriter(f)); fout.write(ta.getText()); fout.close(); editing = false; ta.setEditable(false); } catch(IOException e){};

File chooser public void doExit() { System.exit(0); } public void doNew() { editing = true; ta.setEditable(true); ta.setText(""); }

File chooser public void actionPerformed(ActionEvent e) { if(!editing) { if(e.getSource()==MNew) doNew(); else if(e.getSource()==MOpen) doOpen(); if(e.getSource()==MExit) doExit(); } else if(e.getSource()==MClose) doClose(); }

File chooser class TestEdit34{... }

Dialogs JFileChooser is an example of a dialog common restricted specialised frame – e.g. OK button – e.g. YES/NO option – e.g. simple text entry

Dialogs base dialogs on: JOptionPane extends JComponent implements Accessible JOptionPane() creates new dialog

Dialogs OK dialog public static void showMessageDialog (Component parent, Object message, String title, int messagetype, Icon icon) 1, 2, 3 & 4 parameter versions icon ==> icon to display with message message ==> string for display

Dialogs OK dialog public static void showMessageDialog (Component parent, Object message, String title, int messagetype, Icon icon) messagetype ==> – WARNING_MESSAGE – QUESTION_MESSAGE – INFORMATION_MESSAGE – ERROR_MESSAGE – PLAIN_MESSAGE affect look and feel

Dialogs class JOP extends JFrame { JOptionPane jop; JOP() { jop = new JOptionPane(); jop.showMessageDialog (this,"Proceed?","Proceed", JOptionPane.QUESTION_MESSAGE); }

Dialogs Yes/No dialog public static int showConfirmDialog (Component parent, Object message, String title, int optiontype, int messagetype, Icon icon) 2, 4 & 5 parameter versions optiontype ==> – DEFAULT_OPTION (Okay) – YES_NO_OPTION – YES_NO_CANCEL_OPTION – OK_CANCEL_OPTION

Dialogs Yes/No dialog public static int showConfirmDialog (Component parent, Object message, String title, int optiontype, int messagetype, Icon icon) returns: – OK_OPTION – CANCEL_OPTION – YES_OPTION – NO_OPTION – CLOSED_OPTION (window closed without selection)

Dialogs... jop.showConfirmDialog (this,"Danger!","Danger", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);...

Dialogs text input public static String showInputDialog (Component parent, Object message, String title, int messagetype) 1 & 2 parameter versions returns entered String

Dialogs... String input = jop.showInputDialog(this,"Choice:","Choice", JOptionPane.INFORMATION_MESSAGE);... also a version with pull down menu of options

Dialogs modify editor to allow exit without save... class Edit4 extends JFrame implements ActionListener {... public void doExit() { JOptionPane op = new JOptionPane(); int response = op.showConfirmDialog (this,"Exit without Close?","Exit", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE); if(response==JOptionPane.OK_OPTION) System.exit(0); }... }

Dialogs class TestEdit4 { static BufferedReader fin; public static void main(String [] args) { Edit4 e = new Edit4();... } }