- /*
- Set Multiple Selection Mode For AWT List Example
- This java example shows how to set multiple selection mode for a list
- using Java AWT List class.
- */
- import java.applet.Applet;
- import java.awt.Graphics;
- import java.awt.List;
- /*
- <applet code="SetMultiSelectModeExample" width=200 height=200>
- </applet>
- */
- public class SetMultiSelectModeExample extends Applet{
- List list = null;
- public void init(){
- //create a single selection list
- list = new List(5);
- //add items
- list.add("One");
- list.add("Two");
- list.add("Three");
- list.add("Four");
- list.add("Five");
- list.add("Six");
- list.add("Seven");
- //add list
- add(list);
- /*
- * To set multiple selection mode for a list, use
- * void setMultipleMode(boolean mode)
- * method.
- */
- list.setMultipleMode(true);
- }
- public void paint(Graphics g){
- g.drawString("Is Multiple Selection Allowed? " + list.isMultipleMode(), 10, 120);
- }
- }
Friday, 9 September 2016
Set Multiple Selection Mode For AWT List Example
Get Selected Item Of AWT Choice Or Combobox Example
- /*
- Get Selected Item Of AWT Choice Or Combobox Example
- This java example shows how to get selected item of a choice or combobox
- using Java AWT Choice class.
- */
- import java.applet.Applet;
- import java.awt.Choice;
- import java.awt.Graphics;
- import java.awt.event.ItemEvent;
- import java.awt.event.ItemListener;
- /*
- <applet code="GetSelectedItemExample" width=200 height=200>
- </applet>
- */
- public class GetSelectedItemExample extends Applet implements ItemListener{
- Choice language = null;
- public void init(){
- //create choice or combobox
- language = new Choice();
- //add items to the choice
- language.add("Java");
- language.add("C++");
- language.add("VB");
- language.add("Perl");
- //add choice or combobox
- add(language);
- //add item listener
- language.addItemListener(this);
- }
- public void paint(Graphics g){
- /*
- * To get selected item, use
- * String getSelectedItem()
- * method of AWT Choice class.
- */
- g.drawString(language.getSelectedItem(),10, 70);
- }
- public void itemStateChanged(ItemEvent arg0) {
- repaint();
- }
- }
Set Action Command For AWT Button Example
- /*
- Set Action Command For AWT Button Example
- This java example shows how to set custom action command for AWT button using
- setActionCommand method of Java AWT Button class.
- */
- import java.applet.Applet;
- import java.awt.Button;
- import java.awt.Graphics;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- /*
- <applet code="ButtonSetActionCommandExample" width=200 height=200>
- </applet>
- */
- public class ButtonSetActionCommandExample extends Applet implements ActionListener{
- String actionMessage="";
- public void init(){
- //create Button
- Button Button1 = new Button("I agree with the terms and conditions");
- //add Button
- add(Button1);
- //set action listeners for buttons
- Button1.addActionListener(this);
- /*
- * By default, button's action command is it's label. But in
- * some cases, labels are too long and is not appropriate to use
- * it as an action command. In such situation you would want to
- * define custom short action command for a button.
- *
- * To set custom action command for a button, use
- * void setActionCommand(String command)
- * method of AWT Button class.
- */
- Button1.setActionCommand("Agree");
- }
- public void paint(Graphics g){
- g.drawString(actionMessage,10,50);
- }
- public void actionPerformed(ActionEvent ae){
- /*
- * Get the action command using
- * String getActionCommand() method.
- */
- String action = ae.getActionCommand();
- actionMessage = action + " button pressed!";
- repaint();
- }
- }
Handle Action Events for AWT Button Example
- /*
- Handle Action Events for AWT Button Example
- This java example shows how to handle action event of AWT Button by implementing
- ActionListener interface.
- */
- import java.applet.Applet;
- import java.awt.Button;
- import java.awt.Graphics;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- /*
- <applet code="HandleActionEventExample" width=200 height=200>
- </applet>
- */
- public class HandleActionEventExample extends Applet implements ActionListener{
- String actionMessage="";
- public void init(){
- //create Buttons
- Button Button1 = new Button("Ok");
- Button Button2 = new Button("Cancel");
- //add Buttons
- add(Button1);
- add(Button2);
- //set action listeners for buttons
- Button1.addActionListener(this);
- Button2.addActionListener(this);
- }
- public void paint(Graphics g){
- g.drawString(actionMessage,10,50);
- }
- public void actionPerformed(ActionEvent ae){
- /*
- * Get the action command using
- * String getActionCommand() method.
- */
- String action = ae.getActionCommand();
- if(action.equals("Ok"))
- actionMessage = "Ok Button Pressed";
- else if(action.equals("Cancel"))
- actionMessage = "Cancel Button Pressed";
- repaint();
- }
- }
Get Selected Radio Button Example
- /*
- Get Selected Radio Button Example
- This java example shows how to get selected radio button using
- Java AWT CheckboxGroup class.
- */
- import java.applet.Applet;
- import java.awt.Checkbox;
- import java.awt.CheckboxGroup;
- import java.awt.Graphics;
- import java.awt.event.ItemEvent;
- import java.awt.event.ItemListener;
- /*
- <applet code="GetSelectedRadioButtonExample" width=200 height=200>
- </applet>
- */
- public class GetSelectedRadioButtonExample extends Applet implementsItemListener{
- CheckboxGroup lngGrp = null;
- public void init(){
- //create group
- lngGrp = new CheckboxGroup();
- //create checkboxes and add to group
- Checkbox java = new Checkbox("Java", lngGrp, true);
- Checkbox cpp = new Checkbox("C++", lngGrp, false);
- Checkbox vb = new Checkbox("VB", lngGrp, false);
- //add radio buttons
- add(java);
- add(cpp);
- add(vb);
- //add listeners
- java.addItemListener(this);
- cpp.addItemListener(this);
- vb.addItemListener(this);
- }
- public void itemStateChanged(ItemEvent ie) {
- repaint();
- }
- public void paint(Graphics g){
- /*
- * To get selected radio button, use
- * Checkbox getSelectedCheckbox()
- * method of CheckboxGroup class.
- */
- Checkbox chk = lngGrp.getSelectedCheckbox();
- g.drawString(chk.getLabel() + " is selected", 10 ,70);
- }
- }
Handle Checkbox Event Example
- /*
- Handle Checkbox Event Example
- This java example shows how to handle checkbox event. When checkbox
- is selected and deselected, item event is generated.
- */
- import java.applet.Applet;
- import java.awt.Checkbox;
- import java.awt.Graphics;
- import java.awt.event.ItemEvent;
- import java.awt.event.ItemListener;
- /*
- <applet code="HandleCheckboxEvent" width=200 height=200>
- </applet>
- */
- public class HandleCheckboxEvent extends Applet implements ItemListener{
- Checkbox java = null;
- Checkbox vb = null;
- Checkbox c = null;
- public void init(){
- //create checkboxes
- java = new Checkbox("Java");
- vb = new Checkbox("Visual Basic");
- c = new Checkbox("C");
- add(java);
- add(vb);
- add(c);
- //add item listeners
- java.addItemListener(this);
- vb.addItemListener(this);
- c.addItemListener(this);
- }
- public void paint(Graphics g){
- g.drawString("Java: " + java.getState(),10,80);
- g.drawString("VB: " + vb.getState(), 10, 100);
- g.drawString("C: " + c.getState(), 10, 120);
- }
- public void itemStateChanged(ItemEvent ie) {
- repaint();
- }
- }
Swap Numbers Without Using Third Variable Java Example
- /*
- Swap Numbers Without Using Third Variable Java Example
- This Swap Numbers Java Example shows how to
- swap value of two numbers without using third variable using java.
- */
- public class SwapElementsWithoutThirdVariableExample {
- public static void main(String[] args) {
- int num1 = 10;
- int num2 = 20;
- System.out.println("Before Swapping");
- System.out.println("Value of num1 is :" + num1);
- System.out.println("Value of num2 is :" +num2);
- //add both the numbers and assign it to first
- num1 = num1 + num2;
- num2 = num1 - num2;
- num1 = num1 - num2;
- System.out.println("Before Swapping");
- System.out.println("Value of num1 is :" + num1);
- System.out.println("Value of num2 is :" +num2);
- }
- }
- /*
- Output of Swap Numbers Without Using Third Variable example would be
- Before Swapping
- Value of num1 is :10
- Value of num2 is :20
- Before Swapping
- Value of num1 is :20
- Value of num2 is :10
- */
Subscribe to:
Posts (Atom)