+ All Categories
Home > Documents > 13 1 Event driven programming with swing

13 1 Event driven programming with swing

Date post: 13-Nov-2014
Category:
Upload: suresh1130
View: 140 times
Download: 5 times
Share this document with a friend
Popular Tags:
46
1 Java Swing JFC
Transcript
Page 1: 13 1 Event driven programming with swing

1Java

SwingJFC

Page 2: 13 1 Event driven programming with swing

2Java

Contents

1 JFC or Swing

2 What is AWT then?

3 Swing features

4 Classes

5 Fundamental steps in creating GUI application in java

6 java.awt.Component

7 JFrame

8 java.awt.Graphics

9 java.awt.Color

10 Look and Feel

Page 3: 13 1 Event driven programming with swing

3Java

Contents

21 Setting look and feel

22 JButton

23 Look and feel with JButton

24 JPanel

25 JLabel

26 Adding Borders

27 JRadioButton

28 With borders

29 JList

30 JCombo

Page 4: 13 1 Event driven programming with swing

4Java

contents

31 JTextField

32 Types of Menus

33 JTabbedPane

Page 5: 13 1 Event driven programming with swing

5Java

Know • JFC / Swing fundamentals• The JFC classes

Page 6: 13 1 Event driven programming with swing

6Java

Be able to• Create rich GUI for Java programs using JFC

Page 7: 13 1 Event driven programming with swing

7Java

JFC or Swing

• JFC: Java Foundation Classes• Light-weight user interface components.• OS independent look and feel.• Uses MVC, model stores the content, view

displays the content and controls the interaction between model and view.

• javax.swing package• 100% pure java components.

Page 8: 13 1 Event driven programming with swing

8Java

What is AWT then?• Abstract window toolkit is also a class library

that contains classes that help to build GUI based application. But they are heavy-weight (uses native screen resources).

• Advantage of swing components over AWT area) Better and uniform look and feelb) Efficient use of resources

Page 9: 13 1 Event driven programming with swing

9Java

Swing features• JComponent is the root of all swing components

(similar to Component in awt).• Only top-level swing classes like JWindow,

JFrame, JApplet and JDialog are heavy weight.• General features of swing components:

• Border property • Tooltips• Automatic scrolling• Keystroke handling that works with nested

components.

Page 10: 13 1 Event driven programming with swing

10Java

Classes

• JPanel• JButton• JLabel• JList• JScrollbar• JScrollPane• JComboBox • JMenuBar • JTextField• JTextArea

java.awt.Container

JComponent

JFrame

java.awt.Frame

JAppletjava.applet.Applet

Abstract class

Page 11: 13 1 Event driven programming with swing

11Java

Fundamental steps in creating GUI application in java

1. Create a class that extends JFrame.2. Set the layout manager (or use the default

layout)3. Create the swing components and place it on the

container.4. Write event handlers if necessary.5. In the main method, create an instance of the

class.

Page 12: 13 1 Event driven programming with swing

12Java

java.awt.Componentvoid setBackground(Color c) Color getBackground()void add(PopupMenu popup) void addXXXListener(XXXListener l) void removeXXXListener(XXXListener l)String getName()void setName(String s)boolean isVisible()void setVisible(boolean b) boolean isEnabled() void setEnabled(boolean b) void setBounds(int x, int y, int width, int height)

What XXX is, we will see in event handling

Page 13: 13 1 Event driven programming with swing

13Java

int getX(),int getY() void setSize(int width, int height) void requestFocus()

void setBounds(int x, int y, int width, int height)

int getX() int getY() void setSize(int width, int height) void paint(Graphics g)

Page 14: 13 1 Event driven programming with swing

14Java

JFrame• Constructor:JFrame([String title])

• Methods:String getTitle() void setTitle(String title)void show() void hide() void dispose()Container getContentPane() void setContentPane(Container cp) void setIconImage(Image image)

Page 15: 13 1 Event driven programming with swing

15Java

void setIconImage(Image image) Image getIconImage()void setResizable(boolean resizable) void setDefaultCloseOperation(int opr) JMenuBar getJMenuBar() void setJMenuBar(JMenuBar m)void pack() void setSize(int width,int height)void setVisible(boolean flag)

DO_NOTHING_ON_CLOSE,HIDE_ON_CLOSE, DISPOSE_ON_CLOSE,EXIT_ON_CLOSE

Necessary for the frame to appear

Page 16: 13 1 Event driven programming with swing

16Java

java.awt.Graphics

• Methods:void drawString(String str, int x, int y) void drawLine(int x1, int y1, int x2, int y2)void drawOval(int x, int y, int width, int height)

void fillOval(int x, int y, int width, int height) void drawRect(int x, int y, int width, int height)

Page 17: 13 1 Event driven programming with swing

17Java

void fillRect(int x, int y, int width, int height) void drawPolygon(int[] xPoints, int[] yPoints, int nPoints)

void fillPolygon(int[] xPoints, int[] yPoints, int nPoints)

void setColor(Color c)

Color getColor()

void setFont(Font font) ,Font getFont()

boolean drawImage(Image img, int x, int y, ImageObserver observer)

Page 18: 13 1 Event driven programming with swing

18Java

java.awt.Color• Static constants: black, red, green, blue, yellow, cyan, gray, orange, pink, magenta, white (same in upper case also)

• Constructor :Color(int r, int g, int b) • Methods:static Color getColor(String nm) Color darker()

Page 19: 13 1 Event driven programming with swing

19Java

Simple Frame Exampleimport javax.swing.*;import java.awt.*;

public class FrameSimple extends JFrame{public FrameSimple(){super("Simple Frame");setBackground(Color.pink);setSize(200,150);}

public void paint(Graphics g){g.setColor(Color.yellow);g.fillRect(40,80,50,30);

Example 1

Page 20: 13 1 Event driven programming with swing

20Java

g.setColor(Color.red) ;g.drawString("Hello", 50,100);

}public static void main(String str[]){

new FrameSimple().setVisible(true);}}

Page 21: 13 1 Event driven programming with swing

21Java

Look and Feel• UIManager class’s setLookAndFeel(String newfeel) throws UnsupportedLookAndFeelException

is used to set look and feel where the string argument is the class name for look and feel.

com.sun.java.swing.plaf.motif.MotifLookAndFeel com.sun.java.swing.plaf.windows.WindowsLookAndF

eel javax.swing.plaf.metal.MetalLookAndFeel are the strings that can be used to set motif, windows

and metal look and feel.

Page 22: 13 1 Event driven programming with swing

22Java

Setting look and feel

public class TestJFC extends JFrame {TestJFC() throws Exception{…UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");

…}} Throws ClassNotFOundException, InstantiationException.

•The default look and feel depends on the JRE.•For the JRE that sun provides the default look and feel is ‘Metal’ or also called ‘Java look and feel’.

Page 23: 13 1 Event driven programming with swing

23Java

JButton• Constructors:

JButton() JButton(Icon icon) JButton(String text)

• JButton b1 = new JButton ("Submit",new ImageIcon(“s.jpg"));

Page 24: 13 1 Event driven programming with swing

24Java

Look and feel with JButton

• void setRolloverIcon(Icon rollIcon) • void setPressedIcon(Icon pressedIcon)• void setToolTipText(String text)

Method in JComponent

Page 25: 13 1 Event driven programming with swing

25Java

import javax.swing.*;public class TestJFC extends JFrame{TestJFC() throws Exception{setVisible(true); UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");JButton b1 = new JButton ("submit“, new ImageIcon("bicon.gif"));

JButton Example

Page 26: 13 1 Event driven programming with swing

26Java

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

getContentPane().add(b1);pack();}public static void main(String s[]) throws Exception{

new TestJFC();}}

Example 2

Page 27: 13 1 Event driven programming with swing

27Java

JPanel• JPanel is a component in which several

components can be placed.• This component is fundamentally used for

arranging the component in a frame. We will see more of this in the layout managers.

• JPanel Constructor: JPanel()• void setOpaque(boolean isOpaque) • void setBorder(Border border)

Page 28: 13 1 Event driven programming with swing

28Java

JLabel

JLabel Constructor and methods: JLabel([String s],[Icon image],[int horizontalAlignment ])

Setter and getters for the above parameters

Page 29: 13 1 Event driven programming with swing

29Java

Adding Borders• Borders can be set for every JComponent (JPanel,

JButton, JLabel) using setBorder() method.• BorderFactory class has many methods that help

in creating different types of border like a beveled border, etched border, a matte-look border, titled border etc.

• BorderFactory.createXXXBorder() methods can be used where XXX determine the type of border. This method returns Border.

Page 30: 13 1 Event driven programming with swing

30Java

JRadioButtonJRadioButton male, female;male = new JRadioButton("Male");female = new JRadioButton("Female;");JPanel panel= new JPanel();panel.add(male);panel.add(female);

ButtonGroup bgroup =new ButtonGroup();bgroup.add(male); bgroup.add(female);

Constructor

Create and add to button groupExample 3

Page 31: 13 1 Event driven programming with swing

With borderspanel.setBorder(BorderFactory.createLineBorder(java.awt.Color.red));

panel.setBorder(BorderFactory.createTitledBorder("Entry form"));

panel.setBorder(BorderFactory.createEtchedBorder());

Border b=BorderFactory.createEtchedBorder();

panel.setBorder(BorderFactory.createTitledBorder(b,"Entry form"));

Example 4

Page 32: 13 1 Event driven programming with swing

32Java

JListConstructors:

JList()JList(Object[] ob)JList(Vector<?> vec)

Method:int getSelectedIndex() Object getSelectedValue() void clearSelection() Object[] getSelectedValues()

Page 33: 13 1 Event driven programming with swing

33Java

JComboConstructors:

JComboBox(), JComboBox(Object[] ob)JComboBox(Vector<?> vec)

Method:void addItem(Object anObject) Object getItemAt(int index) void insertItemAt(Object anObject, int index)

int getItemCount() void removeAllItems() void removeItemAt(int anIndex)void removeItem(Object anObject)

Page 34: 13 1 Event driven programming with swing

34Java

Examplejava.util.Vector a= new java.util.Vector();

a.add("Rose");a.add("Lilly");a.add("Lotus");a.add("Jasmine");JList flowers= new JList(a);

JComboBox season= new JComboBox();season.addItem("Autumn");season.addItem("Winter");season.addItem("Spring");season.addItem("Summer"); Example 5

Page 35: 13 1 Event driven programming with swing

35Java

JTextFieldJTextField([String text],[int columns]) void setText(String t),String getText() boolean isEditable()void setEditable(boolean b)

JPasswordField JTextField([String text],[int columns]) void setEchoChar(char c) char[] getPassword() boolean echoCharIsSet()

* is the default echo char

Page 36: 13 1 Event driven programming with swing

36Java

import javax.swing.*;import java.awt.*;public class TestJFC extends JFrame {public static void main(String s[]) {new TestJFC();}TestJFC() {setVisible(true);setSize(200,120);JPanel p= new JPanel();p.setBackground(Color.white);

Example 6Example

Page 37: 13 1 Event driven programming with swing

37Java

p.setBorder(BorderFactory.createMatteBorder(-1,-1,-1,-1,new ImageIcon("bullet.gif")));

p.add(new JLabel("Login"));JTextField login= new JTextField(10);p.add(login);

p.add(new JLabel("Password"));JPasswordField pass= new JPasswordField(10);p.add(pass);

Page 38: 13 1 Event driven programming with swing

38Java

String roles[]={"Student","Teacher","HOD","Management"};p.add(new JLabel("Role"));JComboBox role= new JComboBox(roles);p.add(role);

getContentPane().add(p, BorderLayout.CENTER);validate();}}We need to learn layout managers to layout the components properly.

Page 39: 13 1 Event driven programming with swing

39Java

Types of Menus

JMenu

JMenuItem

JCheckBoxMenuItem

JRadioButtonMenuItemseperator

Page 40: 13 1 Event driven programming with swing

40Java

Example demonstrating use of Menus

import javax.swing.*;import java.awt.*;public class MenuJFC extends JFrame{public static void main(String s[]){new MenuJFC ();}MenuJFC() {setVisible(true);setSize(300,300);JMenuBar mbar = new JMenuBar();

Create menu bar Example 7

Page 41: 13 1 Event driven programming with swing

41Java

JMenu mFile = new JMenu("File");mbar.add(mFile); JMenuItem open = new JMenuItem("Open", new ImageIcon("open.gif"));

JMenuItem exit = new JMenuItem("Close",new ImageIcon("close.gif"));

mFile.add(open);mFile.add(exit);mFile.addSeparator();

Create menu

Create menu items

add menu items to menu

Page 42: 13 1 Event driven programming with swing

JMenu options = new JMenu("Options"); JCheckBoxMenuItem readonly = new JCheckBoxMenuItem("Read-only");

JRadioButtonMenuItem toolbar1= new JRadioButtonMenuItem("Status Bar");

JRadioButtonMenuItem toolbar2= new JRadioButtonMenuItem("Toolbar");

ButtonGroup bg= new ButtonGroup();bg.add(toolbar1);bg.add(toolbar2);options.add(readonly);options.addSeparator();options.add(toolbar1);options.add(toolbar2);

Create check box and radio button menu items

Grouping the radio button toolbar

Page 43: 13 1 Event driven programming with swing

43Java

mFile.add(options);setJMenuBar(mbar);validate();}}

add menu (option) to menu

Page 44: 13 1 Event driven programming with swing

44Java

JTabbedPane• Provides tabbed view that lets the user switch

several tabs. Components can be added to each tab.• Constructors: JTabbedPane() JTabbedPane(int p)

• Methods:void addTab(String title, [Icon icon,] Component component[,String tip])

void remove(int ind), void removeAll()int getSelectedIndex()

JTabbedPane.TOP, JTabbedPane.BOTTOM, JTabbedPane.LEFT, JTabbedPane.RIGHT.

Page 45: 13 1 Event driven programming with swing

45Java

import javax.swing.*;import java.awt.Color;public class TabPaneEx extends JFrame{public static void main(String s[]) {new TabPaneEx();}

TabPaneEx(){setVisible(true);setSize(200,120);final JTabbedPane tab= new JTabbedPane();ImageIcon i= new ImageIcon("bullet.gif");

JPanel p= new JPanel();p.setBackground(Color.white);

Example 8

Page 46: 13 1 Event driven programming with swing

46Java

JPanel p1= new JPanel();p1.setBackground(Color.yellow);JPanel p3= new JPanel();tab.addTab("Panel 1",i,p);tab.addTab("Panel 2",i,p1);getContentPane().add(tab);validate();}}


Recommended