Software Construction LAB 08 Java Programming with SWING GUI Builder.

Slides:



Advertisements
Similar presentations
Chapter 11 Creating User Interfaces F What is JavaBean? F JComponent F JButton  ImageIcon  JLabel  JTextField  JTextArea  JComboBox  JList  JCheckBox.
Advertisements

Graphical User Interfaces (Part IV)
Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
Graphical User Interface Bonus slides Interaction Between Components & Drawing.
Fall 2007CS 225 Graphical User Interfaces Event Handling Appendix C.
 What are the basic GUI components we’ve learned so far? › JFrame › JPanel › JButton › JLabel › JTextArea › JTextField › JCheckBox › JRadioButton › Paint.
1 Chapter 13 Creating User Interfaces. 2 Objectives F To create graphical user interfaces with various user-interface components: JButton, JCheckBox,
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 16 Creating User Interfaces.
© The McGraw-Hill Companies, 2006 Chapter 18 Advanced graphics programming.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 15 Creating User.
1 Chapter 23 Menus, Toolbars, Dialogs, and Internal Frames.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L04 (Chapter 15) Creating.
1 Creating User Interfaces. 2 Motivations A graphical user interface (GUI) makes a system user-friendly and easy to use. Creating a GUI requires creativity.
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.
Advanced Java Class GUI, part 2. JComponent methods addXXXListener(XXXListener) repaint() – [optional arguments: delay and coordinates of sub-area to.
GUI Programming using NetBeans. What is a GUI ? GUI – Graphical User Interface The (visual) interface between humans and computers Ranging from command.
More on Creating GUIs in Java using Swing David Meredith Aalborg University.
Graphic User Interfaces Part 1. Typical GUI Screen from Microsoft Word What GUI “components” can you see? –Menus? Buttons? Labels? What else? –Anything.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
1 TCSS 143, Autumn 2004 Lecture Notes Graphical User Interfaces Koffman/Wolfgang Appendix C, pp
Combo Box, Check Boxes, and Radio Buttons. Radio Buttons User can click radio buttons, just like other buttons BUT Radio buttons are mutually exclusive.
Using Inheritance to Customize Frames Use inheritance for complex frames to make programs easier to understand Design a subclass of JFrame Store the components.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 13 Creating User.
Creating User Interfaces F JComponent F JButton F JLabel F JTextField F JTextArea F JComboBox F JList F JCheckBox F JRadioButton F Dialogs.
1 TCSS 360, Spring 2005 Lecture Notes Model-View-Controller paradigm; Observer pattern; Creating Graphical User Interfaces in Java/Swing Relevant Reading:
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Chapter 9: Creating User Interfaces What is JavaBean? What is JavaBean? JComponent JComponent JButton JButton JLabel JLabel JTextField JTextField JTextArea.
Java Swing. Swing is a set of classes that provides more powerful and flexible components than are possible with the AWT. In addition to the familiar.
SWING. AbstractButton Swing Buttons are subclasses of the AbstractButton class, which extends JComponent. Abstract class javax.swing.AbstractButton AbstractButton.
Chapter 9: Creating User Interfaces What is JavaBean? What is JavaBean? JComponent JComponent Jbutton Jbutton ImageIcon ImageIcon JLabel JLabel JTextField.
CS 112 Introduction to Programming Swing/Android GUI Programming Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone:
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
Java Programming: Advanced Topics 1 Components and Facilities for Rich Graphical User Interfaces Chapter 7.
Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila.
Graphical User Interfaces (Part 2) 1. View  view  presents the user with a sensory (visual, audio, haptic) representation of the model state  a user.
GUI Programming using NetBeans. RHS – SOC 2 GUI construction We have previously talked about elements in a (simple) GUI –Frames, Panes and Dialogs –Text.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 15 Creating User.
Getting Input. Text Fields A text field is a box that the user can type in Use the JTextField class JTextField tf1 = new JTextField(15); 15 is the field.
Graphical User Interfaces. Graphical input and output with JOptionPane.
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus.
Visual Index of Swing GUI Components
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
Ajmer Singh PGT(IP) JAVA IDE Programming - I. Ajmer Singh PGT(IP) GUI (Graphical User Interface) It is an interface that uses a graphic entities along.
Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.
Chapter 14: Introduction to Swing Components. Objectives Understand Swing components Use the JFrame class Use the JLabel class Use a layout manager Extend.
Computer Science [3] Java Programming II - Laboratory Course Lab 4 -1 : Introduction to Graphical user interface GUI Components Faculty of Engineering.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Lecture 33: More Graphical User Interface (GUI) Announcements & Review Read Ch GU1 & GU2 Cohoon & Davidson Ch 14 Reges & Stepp Lab 10 set game due 4/26.
Java Swing Controls. JButton One of the most commonly used swing component is Push Button or simply button which is created through JButton class of Swing.
1 CSE 331 Composite Layouts; Decorators slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Menus and Toolbars CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
View  view  presents the user with a sensory (visual, audio, haptic) representation of the model state  a user interface element (the user interface.
Menus Pull-downs and popups. Tooltips –a ToolTip is a context-sensitive text string that is displayed in a popup window when the mouse rests over a particular.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
Dept. of CSIE, National University of Tainan 10/21/2012 Working with Swing.
CS 112 Introduction to Programming GUI Programming: Swing/Android Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone:
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
GUIs & Event-Driven Programming Chapter 11 Review.
Java Visual Applications CSIS 3701: Advanced Object Oriented Programming.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
CompSci 230 S Programming Techniques
Christopher Budo, Davis Nygren, spencer franks, Luke miller
Advanced User Interfaces
CSE 114 – Computer Science I Event Programming
Chapter 7 Creating User Interfaces
Chapter 13 Creating User Interfaces
Presentation transcript:

Software Construction LAB 08 Java Programming with SWING GUI Builder

2 JButton, JLabel The most common component — a button is a clickable onscreen region that the user interacts with to perform a single command A text label is simply a string of text displayed on screen in a graphical program. Labels often give infor- mation or describe other components public JButton(String text) public JLabel(String text) Creates a new button / label with the given string as its text. public String getText() Returns the text showing on the button / label. public void setText(String text) Sets button / label's text to be the given string.

3 JTextField, JTextArea A text field is like a label, except that the text in it can be edited and modified by the user. Text fields are commonly used for user input, where the user types information in the field and the program reads it A text area is a multi-line text field public JTextField(int columns) public JTextArea(int lines, int columns) Creates a new text field that is the given number of columns (letters) wide. public String getText() Returns the text currently in the field. public void setText(String text) Sets field's text to be the given string.

4 A check box is a toggleable button with two states: checked and unchecked A radio button is a button that can be selected; usually part of a group of mutually-exclusive radio buttons (1 selectable at a time) public JCheckBox / JRadioButton(String text) public JCheckBox(String text, boolean isChecked) Creates checked/unchecked check box with given text. public boolean isSelected() Returns true if check box is checked. public void setSelected(boolean selected) Sets box to be checked/unchecked. JCheckBox, JRadioButton

5 ButtonGroup A logical group of radio buttons that ensures that only one is selected at a time public ButtonGroup() public void add(JRadioButton button) The ButtonGroup is not a graphical component, just a logical group; the RadioButton s themselves are added to the container, not the ButtonGroup

6 Icon/ImageIcon Allows you to put a picture on a button, label or other component public class ImageIcon implements Icon public ImageIcon(String filename) public ImageIcon(URL address) in JButton, JRadioButton, JCheckBox, JLabel, etc... constructor that takes an Icon public void setIcon(Icon) public void setSelectedIcon(Icon) public void setRolloverIcon(Icon)

7 JScrollPane A special container that holds a component, using scrollbars to allow that component to be seen public JScrollPane(Component comp) Wraps the given component with scrollbars. After constructing the scroll pane, add the scroll pane to the container, not the original component. contentPane.add(new JScrollPane(textarea), BorderLayout.CENTER);

8 JFileChooser A special dialog box that allows the user to select one or more files/folders public JFileChooser() public JFileChooser(String currentDir) public int showOpenDialog(Component parent) public int showSaveDialog(Component parent) public File getSelectedFile() public static int APPROVE_OPTION, CANCEL_OPTION Possible result values from showXxxDialog(..). JFileChooser chooser = new JFileChooser(); int result = chooser.showSaveDialog(this); if (result == JFileChooser.APPROVE_OPTION) this.saveData(chooser.getSelectedFile().getName());

9 JColorChooser Another special dialog that lets the user pick from a palette of colors public JColorChooser() public JColorChooser(Color initial) public Color showDialog( Component parent, String title, Color initialColor) returns null if user chose Cancel option

10 JMenuBar The top-level container that holds menus; can be attached to a frame public JMenuBar() public void add(JMenu menu) Usage: in JFrame, the following method exists: public void setJMenuBar(JMenuBar bar)

11 JMenu A menu to hold menu items; menus can contain other menus (Composite) public JMenu(String text) public void add(JMenuItem item) public void addSeparator() public void setMnemonic(int mnemonic)

12 JMenuItem An entry in a frame's Menu bar, which can be clicked to perform commands public JMenuItem(String text) public JMenuItem(String text, Icon icon) public JMenuItem(String text, int mnemonic) public void addActionListener( ActionListener al) public void setAccelerator( KeyStroke ks ) public void setEnabled(boolean b) public void setMnemonic(int mnemonic)

13 JCheckBoxMenuItem / JRadioButtonMenuItem Radio button and checkbox-like menu items public J_____MenuItem(String text) public J_____MenuItem(String text, boolean select) public J_____MenuItem(String text, Icon icon) public J_____MenuItem(String text, Icon icon, boolean selected) public void addActionListener(ActionListener al) public boolean isSelected() public void setSelected(boolean b) Recall: in a ButtonGroup, the following method exists: public void add(AbstractButton button) These two classes extend AbstractButton !

14 mnemonic: menu hotkey assigned to a button or other graphical component usually visible as an underlined key, activated by pressing Ctrl+key (buttons) or Alt+key (menus) only work when input focus is on the appropriate component (affects menus) usage: call setMnemonic(char) method menu items also have constructor that takes mnemonic myQuitButton.setMnemonic('Q'); JMenuItem myNewItem = new JMenuItem("New", 'N'); // or, myNewItem.setMnemonic('N'); Mnemonics

15 accelerator: global hotkey combination that performs an action (ex: Alt-X to exit program) even on components that aren't in focus / visible can be run at any time in the application can optionally include modifiers like Shift, Alt created by calling the getKeyStroke method of the KeyStroke class, and passing this to setAccelerator method of various components (buttons, menus) menuItem.setAccelerator( KeyStroke.getKeyStroke('T',KeyEvent.ALT_MASK)); Accelerators

16 JComboBox public JComboBox() public JComboBox(Vector items) public JComboBox(ComboBoxModel model) Constructs a combo box. Can optionally pass a vector or model of items. (See DefaultComboBoxModel for a model implementation.) public void addActionListener( ActionListener al) Causes an action event to be sent to listener al when the user selects or types a new item in the combo box.

17 JComboBox : Managing Items public void addItem(Object item) public Object getItemAt(int index) public void removeAllItems() public void removeItem(Object item) public void removeItemAt(int index)

18 JComboBox : Selected Item public int getSelectedIndex() public Object getSelectedItem() public void setSelectedItem( Object item ) public void setSelectedIndex(int index) public void setEnabled(boolean enabled) public void setEditable( boolean editable ) If editable, the user can type new arbitrary values into the combo box.

19 JComboBox Code Example final JComboBox box = new JComboBox(); box.addItem("Marty"); box.addItem("Tom"); box.addItem("Jessica"); box.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { JOptionPane.showMessageDialog(null, "You chose " + box.getSelectedItem()); } }); getContentPane().add(box, BorderLayout.NORTH);