Download presentation
Presentation is loading. Please wait.
Published byEleanore Fleming Modified over 8 years ago
1
SWING
2
AbstractButton Swing Buttons are subclasses of the AbstractButton class, which extends JComponent. Abstract class javax.swing.AbstractButton AbstractButton is the template class from which all buttons are defined and their behavior is controlled. This includes push buttons, toggle buttons, check boxes, radio buttons, menu items, and menus themselves. Its direct subclasses are JButton, JToggleButton, and JMenuItem. There are no subclasses of JButton in Swing. JToggleButton has two subclasses: JCheckBox, JRadioButton. JMenuItem has three subclasses: JCheckBoxMenuItem, JRadioButtonMenuItem, and JMenu.
3
JButton class javax.swing.JButton JButton is a basic push-button, one of the simplest Swing components. We can add images, specify text and image alignment, set foreground and background colors, set fonts, etc. Additionally, we can add ActionListeners, ChangeListeners, and ItemListeners to receive ActionEvents, ChangeEvents, and ItemEvents respectively when any properties in its model change value. Constructors:- JButton (Icon i) JButton (String s) JButton (String s, Icon i)
4
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class jbutton extends JApplet implements ActionListener { JTextField jtf; public void init() { Container cp = getContentPane(); cp.setLayout(new FlowLayout()); JButton b1=new JButton("Do Nothing"); b1.setActionCommand("Do Nothing"); b1.addActionListener(this); cp.add(b1); ImageIcon ic = new ImageIcon(“poo.gif"); JButton b2=new JButton(ic); b2.setActionCommand(“Pooh"); b2.addActionListener(this); cp.add(b2); jtf = new JTextField(15); cp.add(jtf); } public void actionPerformed( ActionEvent e) { jtf.setText(e.getActionCommand()); }
5
JToggleButton class javax.swing.JToggleButton JToggleButton provides a selected state mechanism which extends to its children, JCheckBox and JRadioButton We can test whether a toggle button is selected using AbstractButton's isSelected() method. we can set this property with its setSelected() method. JToggleButtons are often used in ButtonGroups. A ButtonGroup manages a set of buttons by guaranteeing that only one button within that group can be selected at any given time. Thus, only JToggleButton and its subclasses are useful in a ButtonGroup (because a JButton does not maintain a selected state).
6
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class togglebuttondemo { public static void main(String args[]) { JFrame frame = new JFrame("Selecting Toggle"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JToggleButton toggleButton = new JToggleButton("Toggle Button"); ActionListener al = new ActionListener() { public void actionPerformed(ActionEvent ae) { AbstractButton ab = (AbstractButton) ae.getSource(); boolean selected = ab.getModel().isSelected(); System.out.println("Action - selected=" + selected + "\n"); } }; toggleButton.addActionListener(al); frame.add(toggleButton, BorderLayout.NORTH); frame.setSize(300, 125); frame.setVisible(true); } }
7
//The event demonstration program for JToggleButton. import java.awt.*; import java.awt.event.*; import javax.swing.*; public class togglebuttondemo2 { public static void main(String[] args) { JToggleButton jtb = new JToggleButton("Press Me"); jtb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ev) { System.out.println ("ActionEvent!"); } }); jtb.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent ev) { System.out.println ("ItemEvent!"); } });
8
jtb.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent ev) { System.out.println ("ChangeEvent!"); } }); JFrame f = new JFrame(); f.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); Container c = f.getContentPane(); c.setLayout (new FlowLayout()); c.add (jtb); f.pack(); f.setVisible(true); }
9
How to Use Check Boxes The JCheckBox class provides support for check box buttons. You can also put check boxes in menus, using the JCheckBoxMenuItem class. Because JCheckBox and JCheckBoxMenuItem inherit from AbstractButton, Swing check boxes have all the usual button characteristics. For example, you can specify images to be used in check boxes. Any number of check boxes in a group — none, some, or all — can be selected. Constructors JCheckBox (Icon i) JCheckBox (Icon i, boolean state) – if state is true, checkbox is initially selected JCheckBox (String s) JCheckBox (String s, Icon i) JCheckBox (String s, boolean state) JCheckBox (String s, Icon i, boolean state) When a checkbox is selected or deselected, an item event is generated, which is handled by itemStateChanged (). Inside itemStateChanged(), getItem () method gets the JCheckBox object that generated the event. getText() method gets the text for the checkbox.
10
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class jcheckbox extends JApplet implements ItemListener { String msg =""; public void init() { Container cp = getContentPane(); cp.setLayout( new FlowLayout()); JCheckBox cb = new JCheckBox("vanila"); cb.addItemListener(this); cp.add(cb); cb = new JCheckBox("Strawberry"); cb.addItemListener(this); cp.add(cb); cb = new JCheckBox("Butterscotch"); cb.addItemListener(this); cp.add(cb); } public void itemStateChanged(ItemEvent ie) { JCheckBox cb = (JCheckBox)ie.getItem(); msg += cb.getText(); repaint(); } public void paint(Graphics g) { g.drawString(msg,100,100);} }
11
How to Use Radio Buttons Radio buttons are groups of buttons in which, only one button at a time can be selected. The Swing supports radio buttons with the JRadioButton and ButtonGroup classes. To put a radio button in a menu, use the JRadioButtonMenuItem class. Because JRadioButton inherits from AbstractButton. Immediate super class is JToggleButton, Swing radio buttons have all the usual button characteristics. For example, you can specify the image displayed in a radio button. Each time the user clicks a radio button (even if it was already selected), the button fires an action event. One or two item events also occur — one from the button that was just selected, and another from the button that lost the selection (if any). Constructors: JRadioButton (Icon i) JRadioButton (Icon i, boolean state) JRadioButton (String s) JRadioButton (String s, Icon i) JRadioButton (String s, boolean state) JRadioButton (String s, Icon i, boolean state)
12
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class jradiobutton extends JApplet implements ActionListener { JTextField tf; public void init() { Container cp = getContentPane(); cp.setLayout( new FlowLayout()); JRadioButton b1 = new JRadioButton("Small"); b1.addActionListener(this); cp.add(b1); JRadioButton b2 = new JRadioButton("Medium"); b2.addActionListener(this); cp.add(b2); JRadioButton b3 = new JRadioButton("Large"); b3.addActionListener(this); cp.add(b3); ButtonGroup bg = new ButtonGroup(); bg.add(b1); bg.add(b2); bg.add(b3); tf = new JTextField(15); cp.add(tf); } public void actionPerformed(ActionEvent ae) { tf.setText(ae.getActionCommand()); } }
13
How to use Combo Boxes Swings provide a combo box( a combination of a text field and a drop-down list) through the JComboBox class, which extends JComponent. A combo box normally displays one entry. It can also displays a drop down list that allows a user to select a different entry. Selection can also be typed inside the text field. Constructors: JComboBox () JComboBox (Vector v) Items are added to the list of choices via the addItem () method. Syntax – void addItem (Object ob)
14
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class jcombobox extends JApplet implements ItemListener { JLabel jl; ImageIcon India,england,usa; public void init() { Container cp = getContentPane(); cp.setLayout( new FlowLayout()); JComboBox jc = new JComboBox(); jc.addItem("India"); jc.addItem("USA"); jc.addItem("UK"); jc.addItemListener(this); cp.add(jc); jl=new JLabel(new ImageIcon("India.gif")); cp.add(jl); } public void itemStateChanged(ItemEvent ie) { String s = (String)ie.getItem(); if (s.equals("India")) jl.setIcon(new ImageIcon("India.gif")); else if (s.equals("USA")) jl.setIcon(new ImageIcon("USA.gif")); else jl.setIcon(new ImageIcon("UK.gif")); }}
15
Menus Java provides the two different kinds of menus – regular and pop-up menus. Java provides the following classes for creating and managing menus: JMenuBar JMenu JMenuItem JCheckBoxMenuItem JRadioButtonMenuItem The menu bar in a window is the horizontal area below the title bar of the window and captions of each of the menus on it. Steps for creating Regular Menus 1. Create an object of JMenuBar class. - Attach a JMenuBar class to a frame. The JMenuBar object is created JMenuBar mb = new JMenuBar(); 2. Call the add method of the frame to attach the menu bar to the frame. frame.getContentPane().add(mb).
16
3. Create objects of the JMenu class for each menu you want in the menu bar. For example JMenu mfile = new JMenu (“File”); JMenu medit = new JMenu (“Option”); 4. Call the add method of the JMenuBar class to add each menu objects to the menu bar. For example mb.add (mfile); mb.add (moption); 5.Create objects of the JMenuItem, JCheckBoxMenuItem and JRadioButtonMenuItem class for each sub-menu item. The check box menu item functions like a check box button. For example JMenuItem opt1 = new JMenuItem(“option1”); JMenuItem opt2 = new JMenuItem(“option2”); 6. Call add method of the menu class to add each item to its appropriate menu. For example: moption.add(opt1); moption.add(opt2);
17
import javax.swing.*; public class jmenu { public static void main(String args[]) { JFrame frame = new JFrame(" Frame with menu"); JMenuBar mb = new JMenuBar(); // frame.getContentPane().add(mb); frame.setJMenuBar(mb); JMenu mfile, medit; mfile = new JMenu("File"); medit = new JMenu("Edit"); mb.add(mfile); mb.add(medit); JMenuItem mnew, mclose,mcopy,mpaste; mnew = new JMenuItem("New"); mclose = new JMenuItem("Close"); mcopy = new JMenuItem("Copy"); mpaste = new JMenuItem("Paste");
18
mfile.add(mnew); mfile.add(mclose); medit.add(mcopy); medit.add(mpaste); // make close disabled mclose.setEnabled(false); // create print check box followed by a separtor JCheckBoxMenuItem mprint; mprint = new JCheckBoxMenuItem("Print"); mfile.add(mprint); mfile.addSeparator(); // create a font submenu JMenu mfont; mfont = new JMenu("Font"); mfile.add(mfont); mfont.add("Arial"); mfont.add("Times New Roman"); frame.setSize(400,400); frame.setVisible(true);} }
19
Pop-up Menus Pop-up Menus are menu that are displayed when a user clicks the right mouse button. They are also known as Short – cut menus Steps for creating a pop-up menu: 1. Create an object of the PopupMenu class. 2. Create objects of the Menu class for each menu you want on the menu bar. 3. Call the add() method of the PopupMenu class to add each menu object to the pop-up menu. 4. Create objects of the MenuItem or CheckBoxMenuItem classes for each item you want to display in the menu. 5. Call the add() methods of the menu in order each item to its appropriate menu.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.