More on GUI We wrap up our examination of Java GUI programming by looking at several other components and their listeners – The components all come from.

Slides:



Advertisements
Similar presentations
Introduction to Swing Components Chapter 14. Part of the Java Foundation Classes (JFC) Provides a rich set of GUI components Used to create a Java program.
Advertisements

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 17 Creating User Interfaces.
Chapter 11 Creating User Interfaces F What is JavaBean? F JComponent F JButton  ImageIcon  JLabel  JTextField  JTextArea  JComboBox  JList  JCheckBox.
Graphical User Interfaces (Part IV)
Drawing in a frame – Java GUI
CS18000: Problem Solving and Object-Oriented Programming.
Graphic User Interfaces Layout Managers Event Handling.
F27SB2 Programming Languages
TCU CoSc Programming with Java Handling Events.
CMSC 341 Building Java GUIs. 09/26/2007 CMSC 341 GUI 2 Why Java GUI Development? Course is about Data Structures, not GUIs. We are giving you the opportunity.
Graphical User Interfaces CS 2110 Spring Ivan Sutherland: “Sketchpad”,
Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven.
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.
Graphical User Interfaces (GUIs) GUI: An application that uses graphical objects to interact with users GUI applications consist of: –Events: A user or.
Swing CS-328 Dick Steflik John Margulies. Swing vs AWT AWT is Java’s original set of classes for building GUIs Uses peer components of the OS; heavyweight.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 15 Creating User.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L03 (Chapter 15) Creating.
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.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
GUI Basics: Introduction. Creating GUI Objects // Create a button with text OK JButton jbtOK = new JButton("OK"); // Create a label with text "Enter your.
Java Programming Chapter 10 Graphical User Interfaces.
Introduction to GUI Java offers a great number of pre-defined classes to support the development of graphical user interfaces –These are broken down into.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
GUI Programming in Java: Event Handling & More Components Corresponds with Chapter 14, Chapter 15.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 13 Creating User.
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.
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,
GUI Components and Design Here we add one more component to our programs, JButtons –JButtons can only be inserted into JPanels (or JApplets) –Clicking.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
Dale Roberts GUI Programming using Java - GUI Components Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
GUI Programming using NetBeans. RHS – SOC 2 GUI construction We have previously talked about elements in a (simple) GUI –Frames, Panes and Dialogs –Text.
GUI Basics. What is GUI? A graphical user interface (GUI) is a type of user interface item that allows people to interact with programs in more ways than.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
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.
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
GUI Basics. Agenda What GUI How to make in java Creating frames Frequently used GUI components Layout Managers.
More GUI Components We finish off our examination of GUI components today by looking at a few that we haven’t seen yet –we start off with the JSlider the.
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.
GUI Components CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.
Copyright © Curt Hill More Widgets In Abstract Window Toolbox.
Chapter 14: Introduction to Swing Components. Objectives Understand Swing components Use the JFrame class Use the JLabel class Use a layout manager Extend.
 Figure illustrates a hierarchy containing many event classes from the package java.awt.event.  Used with both AWT and Swing components.  Additional.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 17.1 Test-Driving the Student Grades Application.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
1 Lecture 8: User Interface Components with Swing.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
Graphical User Interface (pronounced "gooey")
Graphical User Interface (GUI) Components: Part 1 Java How to Program, 9 th Edition Chapter 14.
Chapter 13: Advanced GUIs and Graphics
Chapter 7 Creating User Interfaces
Chapter 13 Creating User Interfaces
Chapter 17 Creating User Interfaces
Chapter 17 Creating User Interfaces
Advanced GUIs and Graphics
Graphical User Interface
CNT 4714: Enterprise Computing Spring 2010
Java GUI programming with Swing
Presentation transcript:

More on GUI We wrap up our examination of Java GUI programming by looking at several other components and their listeners – The components all come from javax.swing We will look at JCheckBoxes, JRadioButtons, JSliders, JSpinners, JComboBoxes, JLists, JScrollBars, and how to open multiple windows – Most have their own listeners although we do not necessarily have to use these listeners, we could add JButtons to activate an ActionListener which then can obtain information from these components – You can examine other GUI components from the author’s online material (JMenu, JToolBar, JTabbedPane, JSplitPane, JTree, and JTable)

JCheckBox and JRadioButton These classes extend the JToggleButton class which itself (along with JButton) extend AbstractButton What these two types of buttons have in common is that they will be in one of two states: selected or not selected – check box allows any number of buttons to be selected – radio button requires buttons be placed into a group where no more than 1 button in a group is selected at a time Both can be created with strings and/or images and have hot keys (mnemonics) associated with them – to respond to a selected button, you can implement either ActionListener or ItemListener to listen for an ItemEvent or you could add a JButton, say “Activate”, and in actionPerformed obtain the status of every button (whether selected or not) – code on the next few slides provides a demo for a program which allows the user to format Graphics text, which is input from a JTextField and is changed based on radio and check box selections

import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; public class GUIEventDemo extends JFrame { private JLabel jlblMessage=new JLabel("Hello", JLabel.CENTER); private JCheckBox jchkBold = new JCheckBox("Bold"); private JCheckBox jchkItalic = new JCheckBox("Italic"); private JRadioButton jrbRed = new JRadioButton("Red"); private JRadioButton jrbGreen = new JRadioButton("Green"); private JRadioButton jrbBlue = new JRadioButton("Blue"); private JTextField jtfMessage = new JTextField(10); public static void main(String[] args) { // details omitted} public GUIEventDemo() { jlblMessage.setBorder(new LineBorder(Color.BLACK, 2)); add(jlblMessage, BorderLayout.CENTER); JPanel jpCheckBoxes = new JPanel(); jpCheckBoxes.setLayout(new GridLayout(2, 1)); jpCheckBoxes.add(jchkBold); jpCheckBoxes.add(jchkItalic); add(jpCheckBoxes, BorderLayout.EAST); JPanel jpRadioButtons = new JPanel();

jpRadioButtons.setLayout(new GridLayout(3, 1)); jpRadioButtons.add(jrbRed); jpRadioButtons.add(jrbGreen); jpRadioButtons.add(jrbBlue); add(jpRadioButtons, BorderLayout.WEST); ButtonGroup group = new ButtonGroup(); group.add(jrbRed); group.add(jrbGreen); group.add(jrbBlue); jrbBlue.setSelected(true); jlblMessage.setForeground(Color.blue); JPanel jpTextField = new JPanel(); jpTextField.setLayout(new BorderLayout(5, 0)); jpTextField.add(new JLabel("Enter a new message"), BorderLayout.WEST); jpTextField.add(jtfMessage, BorderLayout.CENTER); jtfMessage.setHorizontalAlignment(JTextField.RIGHT); add(jpTextField, BorderLayout.NORTH); jchkBold.setMnemonic('B'); jchkItalic.setMnemonic('I'); jrbRed.setMnemonic('E'); jrbGreen.setMnemonic('G'); jrbBlue.setMnemonic('U');

jchkBold.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) { setNewFont(); } }); jchkItalic.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) { setNewFont(); } }); jrbRed.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) { jlblMessage.setForeground(Color.red); } }); jrbGreen.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) { jlblMessage.setForeground(Color.green); } }); jrbBlue.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) { jlblMessage.setForeground(Color.blue); } });

jtfMessage.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) { jlblMessage.setText(jtfMessage.getText()); jtfMessage.requestFocusInWindow(); } }); } private void setNewFont() { int fontStyle = Font.PLAIN; fontStyle += (jchkBold.isSelected() ? Font.BOLD : Font.PLAIN); fontStyle += (jchkItalic.isSelected() ? Font.ITALIC : Font.PLAIN); Font font = jlblMessage.getFont(); jlblMessage.setFont( new Font(font.getName(), fontStyle, font.getSize())); } }

ItemListener, ItemEvent The previous example used anonymous action listeners When you click on (or use a mnemonic hot key) any radio button or check box, an ItemEvent occurs, and then an ActionEvent occurs We could have also implemented ItemListener instead of ActionListener – For each of our buttons, we could replace the addActionListener message with the following buttonname.addItemListener(new ItemListener( ) public void itemStateChanged(ItemEvent e) {…} }); – The … code depends on whether the button is a check box (set foreground color) or radio button (call setNewFont( ); ) we would leave the JTextField listener as is as pressing the enter key in a text field generates an ActionEvent (also a TextEvent) but not an ItemEvent

Implementing ItemListener Recall GUI2 which provided a GUI with JCheckBoxes and JRadioButtons We will finish the GUI by making it respond when the buttons are selected We will use an ItemListener so that for each selection, an ItemEvent will be captured by the method itemStateChanged – We could also implement an ActionListener for a selection or we could add a JButton The code on the next slide includes only the differences from GUI2 – In order for the GUI to respond, we add a JLabel to output what has been selected – Since we can select multiple JCheckBoxes, the JLabel’s output will be a concatenation of all items selected – thus we make this String an instance datum

public static class GuiPanel extends JPanel implements ItemListener { // Buttons declared private JLabel lab;// as with GUI2 private String message; public GuiPanel(){ message=""; lab=new JLabel(" "); b1.addItemListener(this); //... for all 7 buttons JPanel whole=new JPanel(new BorderLayout()); whole.add(p1,BorderLayout.WEST); whole.add(p2,BorderLayout.EAST); whole.add(lab,BorderLayout.SOUTH); add(whole); } public void itemStateChanged(ItemEvent e) { if(e.getSource()==b1) message+="Option 1 selected "; if(e.getSource()==b2) message+="Option 2 selected "; if(e.getSource()==b3) message+="Option 3 selected "; if(e.getSource()==b4) message+="Option 4 selected "; if(e.getSource()==b5) message+="Selection a picked"; else if(e.getSource()==b6) message+="Selection b picked"; else if(e.getSource()==b7) message+="Selection c picked"; lab.setText(message); }

Scroll Bars and Scroll Panes JScrollBars are added to JFrames – When you instantiate a scroll bar you specify its orientation (JScrollBar.HORIZONTAL or JScrollBar.VERTICAL) – You can also specify the starting value, its min and max values (if not specified, starting value by default is in the middle) Scroll bars will generate AdjustmentEvents which are listened for by an AdjustmentListener – To implement an AdjustmentListener, implement the method adjustmentValueChanged The problem with a scroll bar added to the JFrame is moving the view within the JFrame upon changing the scroll bar’s “knob” Instead, we can also place our JPanel inside a scrollable component called a JScrollPane – this will be easier – Instantiate a JPanel for instance MyPanel p1 = new MyPanel( ); – Instantiate a JScrollPane using p1, JScrollPane jsp=new JScrollPane(p1); – Now the JPanel will have its own scroll bars if needed

import java.awt.*; import javax.swing.*; public class ScrollExample1 { public static void main(String[] args) { // JFrame code omitted ScrollPanel p1=new ScrollPanel(); p1.setPreferredSize(new Dimension(250,100)); JScrollPane jsp=new JScrollPane(p1); frame.add(jsp); } public static class ScrollPanel extends JPanel { // code goes here for the JPanel // for instance, maybe we want to draw something: public void paintComponent(Graphics g) { // Graphics code goes here }

JTextBox and JTextArea Both extend JTextComponent They have methods to setText, getText, isEditable and setEditable – You can also set lineWrap to allow line wrapping (if true) and use a wrapStyleWord to indicate if lines should be wrapped by word (if true) or character (if false) The difference between these components is that the text area has multiple rows You construct the text area using one of four constructors – empty – row, column size – String – the area defaults to roughly the size of the String – String, row, column size Additional methods to a text area are to append a String, insert a String at a given index using jta.insert(String, index), replace a portion of the text with a new String using jta.replaceRange(String, start, end) and getLineCount

public static class ScrollPanel extends Jpanel { private JLabel lab; private JTextArea jta; public ScrollPanel() { lab=new JLabel(""); JTextArea jta=new JTextArea(5,50); JScrollPane jsp=new JScrollPane(jta); jta.setLineWrap(true); jta.setWrapStyleWord(true); jta.getDocument().addDocumentListener(new public void changedUpdate(DocumentEvent e){ lab.setText("changing text"); } public void insertUpdate(DocumentEvent e) { lab.setText("inserting"); } public void removeUpdate(DocumentEvent e) { lab.setText("deleting"); } }); // insert jta and // lab into JPanel } A JTextArea inside a JScrollPanel

JComboBox Drop down menus must be added to a JFrame rather than a JPanel so they are somewhat unusual and we will skip covering them But Java does contain an alternate to a drop down menu – a JComboBox – The JComboBox allows you to select one item from a list of items – You define the list in an array of Objects when you instantiate the JComboBox Upon selection, an ItemEvent and an ActionEvent are created so you can either implement an ItemListener (itemStateChanged) or ActionListener – The JComboBox can be implemented so that the entire list appears, only part of the list appears (along with a scroll bar) or a drop-down box appears

public static class JCBPanel extends JPanel implements ItemListener{ private JLabel lab; private JComboBox box; private String[] list={//…}; private String[] descr={//…}; public JCBPanel() { lab=new JLabel(" "); box=new JComboBox (list); box.addItemListener(this); setLayout(new BorderLayout()); add(box,BorderLayout.CENTER); add(lab,BorderLayout.EAST); } public void itemStateChanged(ItemEvent e) { lab.setText(descr[box.getSelectedIndex()]); } Other JComboBox methods are: addItem(Object) getItemAt(int) getItemCount( ) getSelectedItem( ) – returns an Object removeItem(Object) removeItemAt(index)

JList The JList is similar to the JComboBox except that the List appears in a box (no drop down action needed) – The JList allows for multiple selections instead of the single selection of a JComboBox the JList has an instance datum selectionMode which can be one of SINGLE_SELECTION, SINGLE_INTERVAL_SELECTION (you can select multiple items as long as they are contiguous in the list) and MULTIPLE_INTERVAL_SELECTION (multiple items whether contiguous or not) the default is for multiple items (to select multiple items, hold down the control key while clicking, the shift key selects all items between last and current item in list) If your JList is longer than the space in your JPanel, add the JList to a JScrollPane and insert the JScrollPane into your JPanel instead of the JList – This provides you a scroll bar as needed to view the whole list

More on JList The JList will use a ListSelectionListener (defined in javax.swing.event instead of java.awt.event) for any changes to the list (selecting or unselecting items) – Such a change generates a ListSelectionEvent – The ListSelectionListener must implement a valueChanged method Since a JList can have multiple selected items, there are methods that return single Objects or ints (selected items, selected item indices) or arrays – single value methods: getMaxSelectionIndex, getMinSelectionIndex, getLeadSelectionIndex, getSelectedValue (returns the smallest index of the selected) – array methods: getSelectedIndices, getSelectedValues public void valueChanged(ListSelectionEvent e) { int[] x=list.getSelectedIndices(); for(int i=0;i<x.length;i++) // do something with x[i] }

JSlider The JSlider, which can be positioned horizontally or vertically, gives you an input for an int value between a min an max value – The JSlider is usually instantiated with min and max values, or min, max and the starting value of the slider (e.g., 0,100,50 would set the slider at 50) – Orientation is specified using JSlider.HORIZONTAL (the default) or JSlider.VERTICAL you can also specify if tick marks should appear or not by setting paintTicks to true (defaults to false) and setting paintLabels to true (to print labels with the tick marks) – The JSlider creates a ChangeEvent which is handled by a ChangeListener through the stateChanged method – In this method, to determine the location of the slider, use slider.getValue( ) (where slider is the name of your object)

import java.awt.*; import javax.swing.event.*; import javax.swing.*; public class JSL { public static void main(String[] args) { // omitted } public static class SliderPanel extends JPanel implements ChangeListener{ private JSlider s1, s2, s3; private int red, green, blue; public SliderPanel() { red=0;green=0;blue=0; s1=new JSlider(0,255,0);s2=new JSlider(0,255,0); s3=new JSlider(0,255,0);s1.addChangeListener(this); s2.addChangeListener(this);s3.addChangeListener(this); JPanel p1=new JPanel(new GridLayout(3,1)); p1.add(s1); p1.add(s2); p1.add(s3); add(p1); } public void stateChanged(ChangeEvent e){ if(e.getSource()==s1) red=s1.getValue(); if(e.getSource()==s2) green=s2.getValue(); if(e.getSource()==s3) blue=s3.getValue(); repaint(); } public void paintComponent(Graphics g) {…} }

Controlling Multiple Windows You can create and open/close other windows easily by declaring additional JFrames – Once a JFrame is created, use frame.setVisible(true); to open it and frame.setVisible(false); to close it Let’s imagine that you want a program that can open another window on command (we will use JButtons to open and close a second window) – Create an instance datum in your JPanel (inner class) of type JFrame – In this inner JFrame class, define another JPanel this JPanel will display the contents of the newly opened window, whatever that will be – In your JPanel, add two JButtons – For actionPerformed, one button will set the inner class’ JFrame to visible and the other will set the inner class’ JFrame to not visible

public static class ButtonPanel extends JPanel implements ActionListener { private JFrame second=new JFrame(); private SecondPanel sp=new SecondPanel(); private JButton b1, b2; public ButtonPanel(){ second.setSize(new Dimension(200,200)); second.add(sp); b1=new JButton("open second window"); b2=new JButton("close second window"); b1.addActionListener(this); b2.addActionListener(this); add(b1); add(b2); } public void actionPerformed(ActionEvent e){ if(e.getSource()==b1) second.setVisible(true); else if(e.getSource()==b2) second.setVisible(false); } private class SecondPanel extends JPanel{ public void paintComponent(Graphics g){ g.setColor(Color.green); g.fillRect(0,0,200,200); }