Swing. Component overview. Java UI, summer semester 2017/2018 1

Similar documents
JAVA V GUI Java, winter semester

Basics of programming 3. Java GUI and SWING

CS111: PROGRAMMING LANGUAGE II

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

1.1 GUI. JFrame. import java.awt.*; import javax.swing.*; public class XXX extends JFrame { public XXX() { // XXX. init() main() public static

Introduction p. 1 JFC Architecture p. 5 Introduction to JFC p. 7 The JFC 1.2 Extension p. 8 Swing p. 9 Drag and Drop p. 16 Accessibility p.

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

CS410G: GUI Programming. The Model/View/Controller Pattern. Model. Controller. View. MVC is a popular architecture for building GUIs

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

Chapter 12 GUI Basics

Advanced Java Programming. Swing. Introduction to Swing. Swing libraries. Eran Werner, Tel-Aviv University Summer, 2005

Chapter 6: Graphical User Interfaces

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

JList. Displays a series of items The user can select one or more items Class JList extends directly class Jcomponent Class Jlist supports

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

Chapter 17 Creating User Interfaces

Java IDE Programming-I

Swing. By Iqtidar Ali

John Zukowski's Definitive Guide to Swing for Java 2

Chapter 8: GUI Dialog & Table. Informatics Practices Class XII. By- Rajesh Kumar Mishra. KV No.1, AFS, Suratgarh

Workaround Swing Bugs

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

Swing JTable. JTable

Tool Kits, Swing. Overview. SMD158 Interactive Systems Spring Tool Kits in the Abstract. An overview of Swing/AWT

Java 11 Swing with Eclipse Index

Is image everything?

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

Java 11 Swing Index. Section Title Page

12/22/11. Copyright by Pearson Education, Inc. All Rights Reserved.

Java 1.9 Swing Index

Rizvi College of Arts, Science & Commerce Bandra (W), Mumbai Teaching Plan Academic Year

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

Programming graphics

Chapter 13 GUI Basics. Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved.

GUI Event Handlers (Part II)

GUI Components Continued EECS 448

Introduction to the JAVA UI classes Advanced HCI IAT351

Java 1.8 Swing with Eclipse Oxygen Index

Java 1.8 Swing with Eclipse Mars Index

Java 1.8 Swing with Eclipse Neon Index

Widgets. Overview. Widget. Widgets Widget toolkits Lightweight vs. heavyweight widgets Swing Widget Demo

Chapter 12 GUI Basics. Motivations. The design of the API for Java GUI programming

Lecture 6: Components. CS480: Graphical User Interfaces. Dario Salvucci, Drexel University.

Chapter 4. Swing 18 marks

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

EVENTS, EVENT SOURCES AND LISTENERS

JTcl and Swank. Bruce A. Johnson, Tom Poindexter, & Dan Bodoh. What s new with Tcl and Tk on the JVM. Wednesday, October 26, 11

UNIT-V 1 KNREDDY JAVA PROGRAMMING

Graphical User Interface

To follow the Deitel publishing program, sign-up now for the DEITEL BUZZ ON-

Packages: Putting Classes Together

More Swing. CS180 Recitation 12/(04,05)/08

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

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

Object-Oriented Programming Design. Topic : User Interface Components with Swing GUI Part III

Uppsala University. Assignment 3. Separation into Model-View TableModel ListModel ( multiple inheritance or adapter) Renderer (delegation)

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

11/27/2007 TOPICS CHAPTER TOPICS LISTS READ ONLY TEXT FIELDS. Advanced GUI Applications. This module discusses the following main topics:

Summary Chapter 25 GUI Components: Part 2

Contents Chapter 1 Introduction to Programming and the Java Language

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

Java Swing. Lists Trees Tables Styled Text Components Progress Indicators Component Organizers

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

Static Detection of Brittle Parameter Typing

Lecture 18 Java Graphics and GUIs

user-friendly and easy to use.

Unit 6: Graphical User Interface

Contents Introduction 1

8/23/2014. Chapter Topics. Chapter Topics. Lists. Read Only Text Fields. Chapter 13: Advanced GUI Applications

Supporting Materials

Introduction to Graphical Interface Programming in Java. Introduction to AWT and Swing

Graphical User Interfaces. Comp 152

MVC Table / Delegation

Index SELF-STUDY. Symbols

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

Graphical Applications

Practical Scala. Dianne Marsh Emerging Technology for the Enterprise Conference 03/26/2009

Window Interfaces Using Swing Objects

Chapter 18. Advanced graphics programming

GUI Design. Overview of Part 1 of the Course. Overview of Java GUI Programming

Outline. Widgets. Categories of Widgets. What is a Widget?

Learn Java Programming, Dr.Hashamdar. Getting Started with GUI Programming

Chapter 12 Advanced GUIs and Graphics

DM550 Introduction to Programming part 2. Jan Baumbach.

) / Java ( )

Widgets. Widgets Widget Toolkits. User Interface Widget

2110: GUIS: Graphical User Interfaces

Widgets. Widgets Widget Toolkits. 2.3 Widgets 1

Outline. More on the Swing API Graphics: double buffering and timers Model - View - Controller paradigm Applets

Outline CSE 3461 F10. What is a Widget? Properties of Widgets. A self-contained screen object Also called a control Examples of widgets:

CONTENTS. Chapter 1 Getting Started with Java SE 6 1. Chapter 2 Exploring Variables, Data Types, Operators and Arrays 13

Trees CSCI 201 Principles of Software Development

CSE 331 Software Design & Implementation

Summary. Section 14.1 Introduction. Section 14.2 Java s New Nimbus Look-and-Feel. 618 Chapter 14 GUI Components: Part 1

Graphical User Interfaces

Announcements. Introduction. Lecture 18 Java Graphics and GUIs. Announcements. CSE 331 Software Design and Implementation

Project 3. For this set of projects for the course, we wish to simulate some of the aspects of a number of Sea Ports.

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Event Driven Programming

Table of Contents. Chapter 1 Getting Started with Java SE 7 1. Chapter 2 Exploring Class Members in Java 15. iii. Introduction of Java SE 7...

Transcription:

Swing Component overview 1

Label class JLabel for displaying short text image both 2

Buttons many kinds of buttons all of them extends AbstractButton regular button (JButton) "click" button toggle button (JToggleButton) two-state button (on/off) check box (JCheckBox) selected / deselected box radio button (JRadioButton) typically only one button in a group can be selected event ActionEvent listener ActionListener 3

Groups of buttons a group of buttons selected can be only one button typically for radio buttons the ButtonGroup class JRadioButton buttons[] = new JRadioButton [4]; for (int i=0; i<4; i++) { pane.add(buttons[i] = new JRadioButton("Button "+(i+1))); } ButtonGroup bg = new ButtonGroup(); for (int i=0; i<4; i++) { bg.add(buttons[i]); } 4

Buttons and HTML a text of buttons and other components (label,...) can be set in HTML older JDKs may support HTML only partially or not at all not recommended to use new JButton("<html><b><u>T</u>wo<br>lines</b></html>") new JLabel("<html><font color=#ff0000>hello</font></ html>") 5

Icons the interface Icon can be used with labels, buttons, menus,... the class ImageIcon implements Icon an icon created from an image loaded from file, URL,... jpg, png, gif new JButton("Click", new ImageIcon("ystar.png")); new JLabel("Hello", new ImageIcon("gstar.png"), SwingConstants.CENTER); 6

Tool tips "small" help a "bubble" with a text displays when the cursor lingers over the component can be set to components, which extends JComponent button.settooltiptext("click here"); 7

Text fileds the class JTextField an editable single line of text after the ENTER key is pressed ActionEvent methods String gettext() returns the contained text void settext(string text) sets the text the class JTextArea a multi-line editable area have to be inserted to the JScrollPane in order to show scrollbars new JScrollPane(new JTextArea) JScrollPane works with anything that implements Scrollable 8

Combo box the class JComboBox a button with selection of choices can be edited seteditable(boolean b) generates the ActionEvent when changed String[] list = { "aaaa", "bbbb",... }; JComboBox cb = new JComboBox(list); cb.seteditable(true); 9

List box the class JList a list of items items can be selected a single one or several of them setselectionmode(int mode) methods int getselectedindex() Object getselectedvalue() ListSelectionEvent ListSelectionListener 10

Menu frame.setjmenubar(createmenu());... private static JMenuBar createmenu() { JMenuBar mb = new JMenuBar(); JMenu menu = new JMenu("File"); JMenuItem item = new JMenuItem("Quit"); menu.add(item); mb.add(menu); menu = new JMenu("Help"); item = new JMenuItem("Content"); menu.add(item); menu.add(new JSeparator());... mb.add(menu); } return mb; 11

Trees javax.swing.jtree displaying hierarchical data JTree does not hold data directly only displays data data are hold by a model (model-view concept) in general all more complex components have a model JTree, JTable, JList, JButton,... the model determines how the data are stored and retrieved a single component can have multiple models e.g. JList ListModel holds a content of the list ListSelectionModel manages current selection 12

JTree: static content DefaultMutableTreeNode top = new DefaultMutableTreeNode("Root"); createnodes(top); tree = new JTree(top);... private void createnodes(defaultmutabletreenode top) { DefaultMutableTreeNode node = null; DefaultMutableTreeNode leaf = null; node = new DefaultMutableTreeNode("Node1"); top.add(node); leaf = new DefaultMutableTreeNode("Leaf1"); node.add(leaf); leaf = new DefaultMutableTreeNode("Leaf2"); node.add(leaf); node = new DefaultMutableTreeNode("Node2"); top.add(node); 13

JTree: dynamic changes rootnode = new DefaultMutableTreeNode("Root Node"); treemodel = new DefaultTreeModel(rootNode); treemodel.addtreemodellistener(new MyTreeModelListener()); tree = new JTree(treeModel); tree.seteditable(true); tree.getselectionmodel().setselectionmode (TreeSelectionModel.SINGLE_TREE_SELECTION);... class MyTreeModelListener implements TreeModelListener { public void treenodeschanged(treemodelevent e) { } public void treenodesinserted(treemodelevent e) { } public void treenodesremoved(treemodelevent e) { } public void treestructurechanged(treemodelevent e) { } } 14

JTree: dynamic changes public DefaultMutableTreeNode addobject(defaultmutabletreenode parent, Object child, boolean shouldbevisible) { DefaultMutableTreeNode childnode = new DefaultMutableTreeNode(child);... treemodel.insertnodeinto(childnode, parent, parent.getchildcount()); } if (shouldbevisible) { tree.scrollpathtovisible(new TreePath(childNode.getPath())); } return childnode; 15

JTree: own model model-view Model describes data (e.g. DefaultTreeModel) View defines how to display data (JTree) default model DefaultTreeModel if not suitable own model e.g., by default, nodes in the tree are DefaultMutableTreeNode and implements the TreeNode interface own model can have nodes of a completely different type the model must implement TreeModel interface 16

TreeModel void addtreemodellistener(treemodellistener l); Object getchild(object parent, int index); int getchildcount(object parent); int getindexofchild(object parent, Object child); Object getroot(); boolean isleaf(object node); void removetreemodellistener(treemodellistener l); void valueforpathchanged(treepath path, Object; newvalue); 17

Icons in JTree TreeCellRenderer interface setcellrenderer(treecellrenderer r) method of JTree class MyRenderer extends DefaultTreeCellRenderer { public Component gettreecellrenderercomponent(jtree tree,object value,boolean sel,boolean expanded, boolean leaf,int row,boolean hasfocus) { super.gettreecellrenderercomponent(tree, value, sel, expanded, leaf, row, hasfocus); if (...) { seticon(someicon); settooltiptext("..."); } else {...} return this; 18

Icons in JTree ImageIcon leaficon = createimageicon(".."); if (leaficon!= null) { DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer(); } renderer.setleaficon(leaficon); tree.setcellrenderer(renderer); 19

JTable table constructors (some of them) JTable(Object[][] rowdata, Object[] columnnames) JTable(TableModel dm) 20

TableModel void addtablemodellistener(tablemodellistener l) Class<?> getcolumnclass(int columnindex) int getcolumncount() String getcolumnname(int columnindex) int getrowcount() Object getvalueat(int rowindex, int columnindex) boolean iscelleditable(int rowindex, int columnindex) void removetablemodellistener(tablemodellistener l) void setvalueat(object avalue, int rowindex, int columnindex) 21

AbstractTableModel prepared implementation of a model only the following methods have to be implemented public int getcolumncount() public int getrowcount() public Object getvalueat(int row, int col) 22

JToolBar a bar with buttons can be dragged to other place can be drag out 23

JSplitPane displays 2 components horizontally vertically the separator between components can be moved 24

JDesktopPane a window in a window JDesktopPane desktop JInternalFrame inner window 25

Swing Dialogs 26

Overview JDialog a dialog = a window similar to the frame dialogs depend on a frame a dialog is modal if it is displayed, input to other windows of an application is blocked non-modal dialogs can be created also managing the dialog almost the same as for frame JOptionPane a component simplifying creation of standard dialogs predefined dialogs 27

JOptionPane 28

JOptionPane predefined dialogs but can be configured a set of static methods creating dialogs (always several variants of the single method) showmessagedialog() a dialog with message showinputdialog() a dialog with an input line returns String showconfirmdialog() a dialog with a question (Yes/No/Cancel) returns int showoptiondialog() selection of several choices (Yes-No-Maybe-Cancel) 29

JOptionPane can be also used directly by creating an instance of JOptionPane several constructors the created object can inserted to a dialog 30

JFileChooser a standard dialog for file selection JFileChooser chooser = new JFileChooser(); chooser.setdialogtype(jfilechooser.open_dialog) FileNameExtensionFilter filter = new FileNameExtensionFilter( "Images", "jpg", "gif"); chooser.setfilefilter(filter); int returnval = chooser.showopendialog(parent); if (returnval == JFileChooser.APPROVE_OPTION) { System.out.println("Selected file: " + chooser.getselectedfile().getname()); } 31

JColorChooser choosing colors can be used as a dialog as a component 32

Assignment Creates a simple text editor use JTextArea and JScrollPane name of the file is set as a command-line parameter if the file exists, it is loaded menu saving ending Extension 1 extend the editor by a panel with tools as in the figure Extension 2 menu item for file loading 33

Java UI, summer Slides semester version JU2.en.2018.01 2017/2018 This slides are licensed under a Creative Commons Attribution-NonCommercial 4.0 International License. 34