chapter 14: windows and applets ● java provides cross-platform window facilities ● in java 1,...

30
Chapter 14: Windows and applets Java provides cross-platform window facilities In Java 1, this has been done by AWT package However AWT had some problems Main problem was the different look&feel with other windows in the hosting OS Java 2 introduced swing which creates more compatible look & feel with the hosting OS In java we can define two type of GUI Applets which run inside a web browser Applications which run as a stadnalone window The model for applets and normal windows application is different, but with some efforts we can make applications that can be run as both

Upload: anton-crissey

Post on 01-Apr-2015

219 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Chapter 14: Windows and applets ● Java provides cross-platform window facilities ● In Java 1, this has been done by AWT package ● However AWT had some

Chapter 14: Windows and applets● Java provides cross-platform window facilities● In Java 1, this has been done by AWT package● However AWT had some problems

– Main problem was the different look&feel with other windows in the hosting OS

● Java 2 introduced swing which creates more compatible look & feel with the hosting OS

● In java we can define two type of GUI– Applets which run inside a web browser– Applications which run as a stadnalone window

● The model for applets and normal windows application is different, but with some efforts we can make applications that can be run as both

Page 2: Chapter 14: Windows and applets ● Java provides cross-platform window facilities ● In Java 1, this has been done by AWT package ● However AWT had some

Applets● Programs that run inside a browser● When a browser loads a page that includes an applet,

browser downloads the applet and starts it● Because normally an applet comes from another

machine, java runtime environment enforce some restrictions on them:– An applet can’t touch the local disk. Java offers digital signing for

applets. Many applet restrictions are relaxed when you choose to allow signed applets

– Applets can take longer to display, since you must download the whole thing every time, including a separate server hit for each different class.

Page 3: Chapter 14: Windows and applets ● Java provides cross-platform window facilities ● In Java 1, this has been done by AWT package ● However AWT had some

Application framework for applets● There is a JApplet class that all applets inherit from● An applet overrides some of the methods to do some usefull work

Page 4: Chapter 14: Windows and applets ● Java provides cross-platform window facilities ● In Java 1, this has been done by AWT package ● However AWT had some

A simple applet

//: c14:Applet1.java// Very simple applet.import javax.swing.*;import java.awt.*;

public class Applet1 extends JApplet { public void init() { getContentPane().add(new JLabel("Applet!")); }}

Page 5: Chapter 14: Windows and applets ● Java provides cross-platform window facilities ● In Java 1, this has been done by AWT package ● However AWT had some

Running inside a browserBasic taggs (old way!) in a web page:

<applet code=Applet1 width=100 height=50> </applet>Things not as simple as above now, but we can use HTMLConverter to convert above to a form acceptable by current browsers:

Internet explorer display:

Page 6: Chapter 14: Windows and applets ● Java provides cross-platform window facilities ● In Java 1, this has been done by AWT package ● However AWT had some

Running applet as an application//: c14:Applet1c.java// An application and an applet.// <applet code=Applet1c width=100 height=50></applet>import javax.swing.*;import java.awt.*;

public class Applet1c extends JApplet { public void init() { getContentPane().add(new JLabel("Applet!")); } // A main() for the application: public static void main(String[] args) { JApplet applet = new Applet1c(); JFrame frame = new JFrame("Applet1c"); // To close the application: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(applet); frame.setSize(100,50); applet.init(); applet.start(); frame.setVisible(true); }}

Page 7: Chapter 14: Windows and applets ● Java provides cross-platform window facilities ● In Java 1, this has been done by AWT package ● However AWT had some

A display framework

In the book a display framework is implemented that eases creation of applets and window applications:

public static void run(JFrame frame, int width, int height) { ...}

public static void run(JApplet applet, int width, int height){...}

public static void run(JPanel panel, int width, int height) { ...}

Page 8: Chapter 14: Windows and applets ● Java provides cross-platform window facilities ● In Java 1, this has been done by AWT package ● However AWT had some

Putting buttons

//: c14:Button1.java// Putting buttons on an applet.// <applet code=Button1 width=200 height=50></applet>import javax.swing.*;import java.awt.*;import com.bruceeckel.swing.*;

public class Button1 extends JApplet { private JButton b1 = new JButton("Button 1"), b2 = new JButton("Button 2"); public void init() { Container cp = getContentPane(); cp.setLayout(new FlowLayout()); cp.add(b1); cp.add(b2); } public static void main(String[] args) { Console.run(new Button1(), 200, 50); }}

Page 9: Chapter 14: Windows and applets ● Java provides cross-platform window facilities ● In Java 1, this has been done by AWT package ● However AWT had some

Capturing an event

●When we run previous program if we click on buttons nothing will happen!●For any control (e.g. a button) to do something usefull we need to write event handler code●To do this we first register for an event on a control. For example using addActionListener() on Jbutton to register for pressing button actions●addActionListener needs a class that implements ActionListener interface. This interface has a actionPerformed() method.

Page 10: Chapter 14: Windows and applets ● Java provides cross-platform window facilities ● In Java 1, this has been done by AWT package ● However AWT had some

Example of event capturingpublic class Button2 extends JApplet { private JButton b1 = new JButton("Button 1"), b2 = new JButton("Button 2"); private JTextField txt = new JTextField(10); class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { String name = ((JButton)e.getSource()).getText(); txt.setText(name); } } private ButtonListener bl = new ButtonListener(); public void init() { b1.addActionListener(bl); b2.addActionListener(bl); Container cp = getContentPane(); cp.setLayout(new FlowLayout()); cp.add(b1); cp.add(b2); cp.add(txt); } public static void main(String[] args) { Console.run(new Button2(), 200, 75); }}

Page 11: Chapter 14: Windows and applets ● Java provides cross-platform window facilities ● In Java 1, this has been done by AWT package ● However AWT had some

Runnin the example

Pressing Button1

Page 12: Chapter 14: Windows and applets ● Java provides cross-platform window facilities ● In Java 1, this has been done by AWT package ● However AWT had some

Controlling layout (layout manager) BorderLayoutpublic class BorderLayout1 extends JApplet { public void init() { Container cp = getContentPane(); cp.add(BorderLayout.NORTH, new JButton("North")); cp.add(BorderLayout.SOUTH, new JButton("South")); cp.add(BorderLayout.EAST, new JButton("East")); cp.add(BorderLayout.WEST, new JButton("West")); cp.add(BorderLayout.CENTER, new JButton("Center")); } public static void main(String[] args) { Console.run(new BorderLayout1(), 300, 250); }}

Page 13: Chapter 14: Windows and applets ● Java provides cross-platform window facilities ● In Java 1, this has been done by AWT package ● However AWT had some

Controlling layout (layout manager) FlowLayout

public class FlowLayout1 extends JApplet { public void init() { Container cp = getContentPane(); cp.setLayout(new FlowLayout()); for(int i = 0; i < 20; i++) cp.add(new JButton("Button " + i)); } public static void main(String[] args) { Console.run(new FlowLayout1(), 300, 250); }}

Page 14: Chapter 14: Windows and applets ● Java provides cross-platform window facilities ● In Java 1, this has been done by AWT package ● However AWT had some

Controlling layout (layout manager) GridLayout

public class GridLayout1 extends JApplet { public void init() { Container cp = getContentPane(); cp.setLayout(new GridLayout(7,3)); for(int i = 0; i < 20; i++) cp.add(new JButton("Button " + i)); } public static void main(String[] args) { Console.run(new GridLayout1(), 300, 250); }}

Page 15: Chapter 14: Windows and applets ● Java provides cross-platform window facilities ● In Java 1, this has been done by AWT package ● However AWT had some

Events and listener types

Page 16: Chapter 14: Windows and applets ● Java provides cross-platform window facilities ● In Java 1, this has been done by AWT package ● However AWT had some

Swing components - Buttonspublic class Buttons extends JApplet { private JButton jb = new JButton("JButton"); private BasicArrowButton up = new BasicArrowButton(BasicArrowButton.NORTH), down = new BasicArrowButton(BasicArrowButton.SOUTH), right = new BasicArrowButton(BasicArrowButton.EAST), left = new BasicArrowButton(BasicArrowButton.WEST); public void init() { Container cp = getContentPane(); cp.setLayout(new FlowLayout()); cp.add(jb); cp.add(new JToggleButton("JToggleButton")); cp.add(new JCheckBox("JCheckBox")); cp.add(new JRadioButton("JRadioButton")); JPanel jp = new JPanel(); jp.setBorder(new TitledBorder("Directions")); jp.add(up); jp.add(down); jp.add(left); jp.add(right); cp.add(jp); } public static void main(String[] args) { Console.run(new Buttons(), 350, 100); }}

Page 17: Chapter 14: Windows and applets ● Java provides cross-platform window facilities ● In Java 1, this has been done by AWT package ● However AWT had some

Icons and tooltips-changing Jbutton and other components look and feelfaces = new Icon[] { new ImageIcon(getClass().getResource("Face0.gif")), new ImageIcon(getClass().getResource("Face1.gif")), new ImageIcon(getClass().getResource("Face2.gif")), new ImageIcon(getClass().getResource("Face3.gif")), new ImageIcon(getClass().getResource("Face4.gif")), }; jb = new JButton("JButton", faces[3]); ...jb.setRolloverIcon(faces[1]); jb.setPressedIcon(faces[2]); jb.setDisabledIcon(faces[4]); jb.setToolTipText("Yow!");

Page 18: Chapter 14: Windows and applets ● Java provides cross-platform window facilities ● In Java 1, this has been done by AWT package ● However AWT had some

Diffent looks

Page 19: Chapter 14: Windows and applets ● Java provides cross-platform window facilities ● In Java 1, this has been done by AWT package ● However AWT had some

Other components

●TextFields – For one line text entry●Borders – For adding a border to a component●JscrollPane – Provides a scrollable view for a component:

Page 20: Chapter 14: Windows and applets ● Java provides cross-platform window facilities ● In Java 1, this has been done by AWT package ● However AWT had some

Other components (cont.)

●JtextPane – A mini editor●Check Boxes ●Radio buttons●Combo boxes (drop-down list)●List boxes – allows multiple selection●JoptionPane – Message boxes●Menus●etc.

Page 21: Chapter 14: Windows and applets ● Java provides cross-platform window facilities ● In Java 1, this has been done by AWT package ● However AWT had some

Drawingclass SineDraw extends JPanel {...public void paintComponent(Graphics g) { super.paintComponent(g); int maxWidth = getWidth(); double hstep = (double)maxWidth/(double)points; int maxHeight = getHeight(); pts = new int[points]; for(int i = 0; i < points; i++) pts[i] = (int)(sines[i] * maxHeight/2 * .95 + maxHeight/2); g.setColor(Color.RED); for(int i = 1; i < points; i++) { int x1 = (int)((i - 1) * hstep); int x2 = (int)(i * hstep); int y1 = pts[i-1]; int y2 = pts[i]; g.drawLine(x1, y1, x2, y2); } } ....

Page 22: Chapter 14: Windows and applets ● Java provides cross-platform window facilities ● In Java 1, this has been done by AWT package ● However AWT had some

Result

Page 23: Chapter 14: Windows and applets ● Java provides cross-platform window facilities ● In Java 1, this has been done by AWT package ● However AWT had some

Other components (cont.)

●Dialog boxes - A dialog box is a window that pops up out of another window●Sliders and progrss bars●Trees●Tables

Page 24: Chapter 14: Windows and applets ● Java provides cross-platform window facilities ● In Java 1, this has been done by AWT package ● However AWT had some

Selecting look and feel

Try { UIManager.setLookAndFeel(UIManager. getSystemLookAndFeelClassName());} catch(Exception e) { throw new RuntimeException(e);}

Page 25: Chapter 14: Windows and applets ● Java provides cross-platform window facilities ● In Java 1, this has been done by AWT package ● However AWT had some

Binding events dynamically

It is possible to attache more than one listener to each Button:b1.addActionListener(new B()); b1.addActionListener(new B1());

During the execution of the program, it is possible to dynamically adde and remove listeners: txt.append("Button2 pressed\n"); int end = list.size() - 1; if(end >= 0) { b2.removeActionListener( (ActionListener) list.get(end)); list.remove(end); }

Page 26: Chapter 14: Windows and applets ● Java provides cross-platform window facilities ● In Java 1, this has been done by AWT package ● However AWT had some

Separating business logic from UI logicclass BusinessLogic { private int modifier; public BusinessLogic(int mod) { modifier = mod; } public void setModifier(int mod) { modifier = mod; } public int getModifier() { return modifier; } // Some business operations: public int calculation1(int arg){ return arg * modifier;}}public class Separation extends JApplet { private JTextField t = new JTextField(15), mod = new JTextField(15); private Jbutton calc1 = new JButton("Calculation 1"), private BusinessLogic bl = new BusinessLogic(2); public static int getValue(JTextField tf) { try { return Integer.parseInt(tf.getText()); } catch(NumberFormatException e) { return 0; } } class Calc1L implements ActionListener { public void actionPerformed(ActionEvent e) { t.setText(Integer.toString( bl.calculation1(getValue(t)))); } }

Page 27: Chapter 14: Windows and applets ● Java provides cross-platform window facilities ● In Java 1, this has been done by AWT package ● However AWT had some

Visual builder tools and JavaBeans

● Starting with Visual Basic, visual builder tools become very famous● They help in creating visual interfaces more easily and with more speed● In java, with the use of JavaBeans we can create components that can be handled by Java visual builders● In a typical visual builder tool you can drag and drop a component into a form and then modify its properties and write handlers for handling events on that component.

Page 28: Chapter 14: Windows and applets ● Java provides cross-platform window facilities ● In Java 1, this has been done by AWT package ● However AWT had some

What is a JavaBean

A bean is a Class with some conventions applied for naming methods:1. For a property named xxx, you typically create two methods: getXxx( ) and setXxx( ). 2. For a boolean property, you can use the “get” and “set” approach above, but you can also use “is” instead of “get.” 3. Ordinary methods of the Bean don’t conform to the above naming convention, but they’re public. 4. For events, you use the Swing “listener” approach. It’s exactly the same as you’ve been seeing: addActionListener(ActionLister eListener) and removeActionLister(ActionListener) to handle a ActionEvent.

Page 29: Chapter 14: Windows and applets ● Java provides cross-platform window facilities ● In Java 1, this has been done by AWT package ● However AWT had some

JavaBean examplepublic class BangBean extends JPanel implements Serializable { private int xm, ym; private int cSize = 20; // Circle size private String text = "Bang!"; private int fontSize = 48; private Color tColor = Color.RED; private ActionListener actionListener; public BangBean() { addMouseListener(new ML()); addMouseMotionListener(new MML()); } public int getCircleSize() { return cSize; } public void setCircleSize(int newSize) { cSize = newSize; } public String getBangText() { return text; } public void setBangText(String newText) { text = newText; } public int getFontSize() { return fontSize; } public void setFontSize(int newSize) { fontSize = newSize; }

Page 30: Chapter 14: Windows and applets ● Java provides cross-platform window facilities ● In Java 1, this has been done by AWT package ● However AWT had some

JavaBean example (cont.) public Color getTextColor() { return tColor; } public void setTextColor(Color newColor) { tColor = newColor; } public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.BLACK); g.drawOval(xm - cSize/2, ym - cSize/2, cSize, cSize); } // This is a unicast listener, which is // the simplest form of listener management: public void addActionListener(ActionListener l) throws TooManyListenersException { if(actionListener != null) throw new TooManyListenersException(); actionListener = l; } public void removeActionListener(ActionListener l) { actionListener = null; }

...