Java Swing Walter Milner Java Swing Walter Milner

  • Slides: 56
Download presentation
Java Swing Walter Milner Java Swing © Walter Milner 2005: Slide 1

Java Swing Walter Milner Java Swing © Walter Milner 2005: Slide 1

Note - this presentation. . • often needs to refer to source code which

Note - this presentation. . • often needs to refer to source code which is too big to put on a slide • So the source code is in a separate Word document • And is also given in within this presentation in the notes Java Swing © Walter Milner 2005: Slide 2

What is Swing? • A group of 14 packages to do with the UI

What is Swing? • A group of 14 packages to do with the UI • 451 classes as at 1. 4 (!) • Part of JFC Java Foundation Classes (compare now defunct MFC) Java Swing © Walter Milner 2005: Slide 3

Swing and the AWT • AWT = abstract windows toolkit (cross platform) • AWT

Swing and the AWT • AWT = abstract windows toolkit (cross platform) • AWT = earliest version of Java GUI • eg Frame AWT not JFrame Swing • Most Swing components are 'lightweight' • Do not mix AWT and Swing • Use Swing Java Swing © Walter Milner 2005: Slide 4

Swing and threads • A thread is a lightweight process • Most Swing components

Swing and threads • A thread is a lightweight process • Most Swing components are not threadsafe • Solution is to make sure all code that creates and modifies Swing components executes in the same 'event-dispatching' thread • Start a Swing application using the following code. . Java Swing © Walter Milner 2005: Slide 5

Swing and Threads - starting up public static void main(String[] args) { Swing. Utilities.

Swing and Threads - starting up public static void main(String[] args) { Swing. Utilities. invoke. Later(new Runnable() { public void run() { create. And. Show. GUI(); // << method to start it } }); } Java Swing © Walter Milner 2005: Slide 6

create. And. Show. GUI private static void create. And. Show. GUI() { //Create and

create. And. Show. GUI private static void create. And. Show. GUI() { //Create and set up the window. JFrame frame = new JFrame("Hi. . "); frame. set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE); //Add a label. JLabel label = new JLabel("Hello World"); frame. get. Content. Pane(). add(label); //Display the window. frame. pack(); frame. set. Visible(true); } Try this out Java Swing © Walter Milner 2005: Slide 7

Layout Managers • Most Swing UIs utilise a Layout. Manager to control positioning of

Layout Managers • Most Swing UIs utilise a Layout. Manager to control positioning of items • There is a choice of these which work in different ways • Initially we do without one, and position items ourselves: • frame. set. Layout(null); Java Swing © Walter Milner 2005: Slide 8

Absolute positioning JFrame frame = new JFrame("I am a JFrame"); frame. set. Default. Close.

Absolute positioning JFrame frame = new JFrame("I am a JFrame"); frame. set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE); frame. set. Bounds(20, 300, 100); frame. set. Layout(null); JButton butt=new JButton("Click me"); frame. get. Content. Pane(). add(butt); butt. set. Bounds(20, 200, 20); frame. set. Visible(true); Try this out - start with last example and put this in Createand. Show. GUI() Java Swing © Walter Milner 2005: Slide 9

Responding to user actions • Based on an event-handling model • New component eg

Responding to user actions • Based on an event-handling model • New component eg a button should have a Listener specified • The Listener object is programmed to respond to Event objects coming from the component • The Listener object needs to implement the appropriate interface Java Swing © Walter Milner 2005: Slide 10

Event-handling Event object interface eg Action. Listener the listener eg JFrame when clicked component

Event-handling Event object interface eg Action. Listener the listener eg JFrame when clicked component eg button during initialisation, component selects another object eg a JFrame, to be the listener executes appropriate interface method ie action. Performed Java Swing © Walter Milner 2005: Slide 11

Interfaces • An interface is a set of methods • eg the Action. Listener

Interfaces • An interface is a set of methods • eg the Action. Listener interface has just one method public void action. Performed(Action. Event e) • A class can declare that it implements it eg public class Main implements Action. Listener • Then it must actually define the methods in that interface • Or the compiler will complain • Classes can implement multiple interfaces Java Swing © Walter Milner 2005: Slide 12

Button click demo • See source code in Word • JButton and JLabel •

Button click demo • See source code in Word • JButton and JLabel • click. Counts remembers the number of clicks • Class implements Action. Listener • Make JFrame, JButton and JLabel • Instantiate application object • Set to be the listener of the button Java Swing © Walter Milner 2005: Slide 13

Which button? • If have several buttons, all must link to action. Performed •

Which button? • If have several buttons, all must link to action. Performed • How to know which button was clicked? • Use the. get. Source method of the Action. Event object Java Swing © Walter Milner 2005: Slide 14

Example which button butt 1=new JButton("Button 1"); . . butt 2 = new JButton("Button

Example which button butt 1=new JButton("Button 1"); . . butt 2 = new JButton("Button 2"); . . public void action. Performed(Action. Event e) { if (e. get. Source()==butt 1) label. set. Text("Butt 1 clicked"); else label. set. Text("Butt 2 clicked"); } Try this out Java Swing © Walter Milner 2005: Slide 15

Look and feels CDE/Motif Windows Metal Available look and feels depend on implementation Java

Look and feels CDE/Motif Windows Metal Available look and feels depend on implementation Java Swing © Walter Milner 2005: Slide 16

Setting a laf try { UIManager. set. Look. And. Feel( "com. sun. java. swing.

Setting a laf try { UIManager. set. Look. And. Feel( "com. sun. java. swing. plaf. motif. Motif. Look. And. Feel" ); } catch (Exception e) { System. out. println("Cant get laf"); }. . JFrame frame = new JFrame(); This in main() - set laf as first step try. . catch. . because could fail UIManager is in java. lang Java Swing © Walter Milner 2005: Slide 17

Finding installed lafs Object a[]= UIManager. get. Installed. Look. And. Feels(); for (int i=0;

Finding installed lafs Object a[]= UIManager. get. Installed. Look. And. Feels(); for (int i=0; i<a. length; i++) System. out. println(a[i]); Java Swing © Walter Milner 2005: Slide 18

Decorated JFrame. set. Default. Look. And. Feel. Decorated(true); . . call JFrame constructor Java

Decorated JFrame. set. Default. Look. And. Feel. Decorated(true); . . call JFrame constructor Java Swing © Walter Milner 2005: Slide 19

Swing has a lot of classes containers things that hold other things eg JFRame

Swing has a lot of classes containers things that hold other things eg JFRame controls User I/O widgets eg JButton Java Swing © Walter Milner 2005: Slide 20

Containers top level containers - JFrame JApplet JDialog general purpose containers panel scroll pane

Containers top level containers - JFrame JApplet JDialog general purpose containers panel scroll pane split pane tabbed pane tool bar Java Swing © Walter Milner 2005: Slide 21

JPanel ( in create. And. Show. GUI) JFrame. set. Default. Look. And. Feel. Decorated(true);

JPanel ( in create. And. Show. GUI) JFrame. set. Default. Look. And. Feel. Decorated(true); JFrame frame = new JFrame("I am a JFrame"); frame. set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE); frame. set. Bounds(20, 300, 100); frame. set. Layout(null); //Create a panel JPanel my. Panel = new JPanel(); my. Panel. set. Background(new Color(255, 3, 25)); my. Panel. set. Opaque(true); //Make it the content pane. frame. set. Content. Pane(my. Panel); frame. set. Visible(true); Java Swing © Walter Milner 2005: Slide 22

JPanel • Is a subclass of JComponent • So are all the other Swing

JPanel • Is a subclass of JComponent • So are all the other Swing components except the top-level containers • You can add a border • And a tool-tip Java Swing © Walter Milner 2005: Slide 23

Tooltip and border . . my. Panel. set. Opaque(true); my. Panel. set. Tool. Tip.

Tooltip and border . . my. Panel. set. Opaque(true); my. Panel. set. Tool. Tip. Text("I'm a JPanel"); my. Panel. set. Border(Border. Factory. create. Line. Border(Color. white)); frame. set. Content. Pane(my. Panel); . . Java Swing © Walter Milner 2005: Slide 24

JSplit. Pane. . set. Layout(null); //Create a split pane JSplit. Pane my. Pane =

JSplit. Pane. . set. Layout(null); //Create a split pane JSplit. Pane my. Pane = new JSplit. Pane(); my. Pane. set. Opaque(true); frame. set. Content. Pane(my. Pane); frame. set. Visible(true); Java Swing © Walter Milner 2005: Slide 25

JSplit. Pane with JPanels //Create a split pane JSplit. Pane my. Pane = new

JSplit. Pane with JPanels //Create a split pane JSplit. Pane my. Pane = new JSplit. Pane(); my. Pane. set. Opaque(true); my. Pane. set. Divider. Location(150); // make two panels JPanel right = new JPanel(); right. set. Background(new Color(255, 0, 0)); JPanel left = new JPanel(); left. set. Background(new Color(0, 255, 0)); // set as left and right in split my. Pane. set. Right. Component(right); my. Pane. set. Left. Component(left); Java Swing © Walter Milner 2005: Slide 26

Exercise • Program this • The buttons set the colour of the left hand

Exercise • Program this • The buttons set the colour of the left hand pane Java Swing © Walter Milner 2005: Slide 27

JText. Field • For single-line text input • Methods get. Text, set. Text •

JText. Field • For single-line text input • Methods get. Text, set. Text • Can use Action. Listener, triggered when Enter pressed Java Swing © Walter Milner 2005: Slide 28

Example of JText. Field • • See source in Word doc Check Main object

Example of JText. Field • • See source in Word doc Check Main object fields for label and textfield Make a panel, set as content pane Make and add text field Add actionlistener Make and add a label Program action. Performed Java Swing © Walter Milner 2005: Slide 29

JText. Area JPanel my. Panel = new JPanel(); app. text. Area = new JText.

JText. Area JPanel my. Panel = new JPanel(); app. text. Area = new JText. Area("Type here", 5, 20); my. Panel. add(app. text. Area); Text. Area expands rows and columns as needed Java Swing © Walter Milner 2005: Slide 30

JScroll. Pane JText. Area text. Area = new JText. Area("Type here", 5, 20); JScroll.

JScroll. Pane JText. Area text. Area = new JText. Area("Type here", 5, 20); JScroll. Pane scroll. Pane = new JScroll. Pane(text. Area); frame. set. Content. Pane(scroll. Pane); Java Swing © Walter Milner 2005: Slide 31

Exercise • Program this • Use the select. All and cut methods of JText.

Exercise • Program this • Use the select. All and cut methods of JText. Component, which JText. Area inherits Java Swing © Walter Milner 2005: Slide 32

. . Timer t = new Timer(1000, app); t. start(); app. label = new

. . Timer t = new Timer(1000, app); t. start(); app. label = new JLabel("Time"); app. label. set. Bounds(20, 200, 20); frame. get. Content. Pane(). add(app. label); . . public void action. Performed(Action. Event e) { String now = (new java. util. Date()). to. String(); label. set. Text(now); } Timer Java Swing © Walter Milner 2005: Slide 33

Images JFrame frame = new JFrame("I am Celsius"); frame. set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE);

Images JFrame frame = new JFrame("I am Celsius"); frame. set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE); frame. set. Bounds(20, 30, 200); frame. get. Content. Pane(). set. Layout(null); Image. Icon icon = new Image. Icon("c: /celsius. jpg", "Celsius"); JLabel label = new JLabel(icon); label. set. Bounds(20, 150, 150); frame. get. Content. Pane(). add(label); frame. set. Visible(true); Java Swing © Walter Milner 2005: Slide 34

JScroll. Bar See source code JScroll. Bar and JLabel Constructor arguments implements Adjustment. Listener

JScroll. Bar See source code JScroll. Bar and JLabel Constructor arguments implements Adjustment. Listener adjustment. Value. Changed e. get. Value() Java Swing © Walter Milner 2005: Slide 35

Exercise • Program this • The scroll bars determine the red, green and blue

Exercise • Program this • The scroll bars determine the red, green and blue components of the background of the panel Java Swing © Walter Milner 2005: Slide 36

JCheck. Box • See source code • implements Action. Listener • is. Selected() Java

JCheck. Box • See source code • implements Action. Listener • is. Selected() Java Swing © Walter Milner 2005: Slide 37

Exercise • Program this • The checkbox determines if the text in the label

Exercise • Program this • The checkbox determines if the text in the label is left or right aligned Java Swing © Walter Milner 2005: Slide 38

Radio. Button • Come in groups – only 1 selected per group • See

Radio. Button • Come in groups – only 1 selected per group • See demo code • Make radiobuttons • Make group • Add radiobuttons to group • Action. Listener Java Swing © Walter Milner 2005: Slide 39

Radio. Button Exercise • Modify the demo by adding more colour options Java Swing

Radio. Button Exercise • Modify the demo by adding more colour options Java Swing © Walter Milner 2005: Slide 40

Radio. Button group border. . JPanel group. Panel = new JPanel(); group. Panel. set.

Radio. Button group border. . JPanel group. Panel = new JPanel(); group. Panel. set. Bounds(10, 100, 60); group. Panel. set. Border(Border. Factory. create. Line. Border( Color. black)); frame. get. Content. Pane(). add(group. Panel); group. Panel. add(app. choice 1); group. Panel. add(app. choice 2); . . Java Swing © Walter Milner 2005: Slide 41

List. Box • • • See source code Data held in array List box

List. Box • • • See source code Data held in array List box shows array List box inside scroll pane my. List. get. Model(). get. Element. At(. . Java Swing © Walter Milner 2005: Slide 42

Two JList. Boxes • • See source code We want to add items to

Two JList. Boxes • • See source code We want to add items to list So use a Vector not an array to hold data Check methods to delete items and copy to other listbox Java Swing © Walter Milner 2005: Slide 43

Exercise • Add a button to the last example which deletes selected items in

Exercise • Add a button to the last example which deletes selected items in the second list box Java Swing © Walter Milner 2005: Slide 44

Layout Managers • A layout manager controls the positioning of components • Components have

Layout Managers • A layout manager controls the positioning of components • Components have a 'preferred size' so can avoid sizing them • . pack() adjusts size of a container to fit components Java Swing © Walter Milner 2005: Slide 45

Some Layout. Managers from Swing tutorial on java. sun. com Java Swing © Walter

Some Layout. Managers from Swing tutorial on java. sun. com Java Swing © Walter Milner 2005: Slide 46

Flow. Layout JFrame. set. Default. Look. And. Feel. Decorated(true); JFrame frame = new JFrame("Flow.

Flow. Layout JFrame. set. Default. Look. And. Feel. Decorated(true); JFrame frame = new JFrame("Flow. Layout"); frame. set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE); frame. get. Content. Pane(). set. Layout(new Flow. Layout()); JButton b 1 = new JButton("Hello"); frame. get. Content. Pane(). add(b 1); JButton b 2 = new JButton("Two"); frame. get. Content. Pane(). add(b 2); JText. Field t 1 = new JText. Field("Text here"); frame. get. Content. Pane(). add(t 1); Try this frame. pack(); Try re-sizing the frame at runtime frame. set. Visible(true); Add more buttons Add frame. set. Bounds Remove pack(); Java Swing © Walter Milner 2005: Slide 47

Border. Layout JFrame. set. Default. Look. And. Feel. Decorated(true); JFrame frame = new JFrame("Border");

Border. Layout JFrame. set. Default. Look. And. Feel. Decorated(true); JFrame frame = new JFrame("Border"); frame. set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE); JButton b 1 = new JButton("At the top"); frame. get. Content. Pane(). add(b 1, Border. Layout. PAGE_START ); JButton b 2 = new JButton("Bottom"); frame. get. Content. Pane(). add(b 2, Border. Layout. PAGE_END); JText. Field t 1 = new JText. Field("Left"); frame. get. Content. Pane(). add(t 1, Border. Layout. LINE_START); JText. Field t 2 = new JText. Field("Right"); frame. get. Content. Pane(). add(t 2, Border. Layout. LINE_END); JButton b 3 = new JButton("Centre"); frame. get. Content. Pane(). add(b 3, Border. Layout. CENTER ); Try this frame. pack(); frame. set. Visible(true); Java Swing © Walter Milner 2005: Slide 48

Grid JFrame. set. Default. Look. And. Feel. Decorated(true); JFrame frame = new JFrame("Grid"); frame.

Grid JFrame. set. Default. Look. And. Feel. Decorated(true); JFrame frame = new JFrame("Grid"); frame. set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE); frame. get. Content. Pane(). set. Layout(new Grid. Layout(4, 3, 5, 5)); for (int i=0; i<10; i++) frame. get. Content. Pane(). add(new JButton(""+i)); frame. pack(); frame. set. Visible(true); Java Swing © Walter Milner 2005: Slide 49

Combination layouts • • • See source code Frame is null layout Frame has

Combination layouts • • • See source code Frame is null layout Frame has an upper and lower panel Upper panel null layout Lower panel is grid layout Note font of display Java Swing © Walter Milner 2005: Slide 50

Menus JMenu. Bar JMenu. Item Java Swing © Walter Milner 2005: Slide 51

Menus JMenu. Bar JMenu. Item Java Swing © Walter Milner 2005: Slide 51

Menu Main app = new Main(); . . JMenu. Bar my. Menu. Bar =

Menu Main app = new Main(); . . JMenu. Bar my. Menu. Bar = new JMenu. Bar(); JMenu menu 1 = new JMenu("File"); JMenu. Item item = new JMenu. Item("Exit"); item. add. Action. Listener(app); menu 1. add(item); my. Menu. Bar. add(menu 1); frame. set. JMenu. Bar(my. Menu. Bar); . . public void action. Performed(Action. Event e) { System. exit(0); } Java Swing © Walter Milner 2005: Slide 52

Menu Options • See source code Exercise Copy this Add a second option 'Edit'

Menu Options • See source code Exercise Copy this Add a second option 'Edit' after 'File' Put choices Undo, Redo, Cut Copy and Paste in it Use appropriate icons if possible Java Swing © Walter Milner 2005: Slide 53

JTool. Bar. . frame is Border. Layout. . JTool. Bar tool. Bar = new

JTool. Bar. . frame is Border. Layout. . JTool. Bar tool. Bar = new JTool. Bar("Test"); JButton butt 1 = new JButton(new Image. Icon("icon. gif")); tool. Bar. add(butt 1); . . frame. add(tool. Bar, Border. Layout. PAGE_START); Java Swing © Walter Milner 2005: Slide 54

paint • JComponents have a paint() method • This is called by the system

paint • JComponents have a paint() method • This is called by the system when it needs to display the object • Initially and eg after a re-size • You can over-ride paint() to control the appearance of the component • This implies you sub-class the component • The paint method has a Graphics object as a parameter • This is a context eg color, font etc • You tell the Graphics object to show things Java Swing © Walter Milner 2005: Slide 55

public class My. Frame extends JFrame { public My. Frame() { super("Some title"); set.

public class My. Frame extends JFrame { public My. Frame() { super("Some title"); set. Default. Close. Operation(EXIT_ON_CLOSE); set. Bounds(20, 30, 230, 180); my. Panel = new My. Panel(); my. Panel. set. Opaque(true); set. Content. Pane(my. Panel); set. Visible(true); } My. Panel my. Panel; } Example public class My. Panel extends JPanel { public void paint(Graphics g) { g. draw. Line(0, 0, get. Width(), get. Height()); g. draw. String("Hello", get. Width()/2, get. Height()/2); } } Java Swing © Walter Milner 2005: Slide 56