Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

How to demonstrate working of CheckBox and adding ActionListener to it in Java's Swing Framework ?.

Program to demonstrate working of CheckBox and adding ActionListener to it in Java's Swing Framework.


package com.hubberspot.swing;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class CheckBoxDemo {

 public static void main(String[] args) {

  // 1. Create a simple frame by creating an object 
  // of JFrame. 
  JFrame frame = new JFrame();

  // 2. Give the frame a title "CheckBox Demo" by calling 
  // setTitle method on frame object
  frame.setTitle("CheckBox Demo");

  // 3. Give frame a size in pixels as say 300,300 
  frame.setSize(300, 200);

  // 4. set a operation when a user close the frame, here it 
  // closes the frame and exits the application
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  // 5. Create a checkbox by creating an Object of class 
  // JCheckBox. 
  final JCheckBox checkBox1 = new JCheckBox();

  // 6. set the text of checkbox1 as "check box 1"
  checkBox1.setText("check box 1");

  // 7. Create another checkbox by creating an Object of class 
  // JCheckBox.
  final JCheckBox checkBox2 = new JCheckBox();

  // 8. set the text of checkBox2 as "check box 2"
  checkBox2.setText("check box 2");

  // 9. Make it selected that is (checked)
  checkBox2.setSelected(true);

  // 10. Create a label by creating an Object of class 
  // JLabel
  final JLabel label = new JLabel();

  // 11. set the text of label as "check box 2 selected"
  // as initially we have made checkbox2 selected
  label.setText("check box 2 selected");

  // 12. We add an ActionListener to the checkbox1
  // which will listen about the action user performs
  // over the checkbox1. Here user only checks and unchecks
  // it. As soon as user checks checkbox1 the text of the 
  // label changes from check box 2 selected to check box 1 selected
  // and checkbox2 is unchecked
  checkBox1.addActionListener(new ActionListener() {

   @Override
   public void actionPerformed(ActionEvent event) {

    JCheckBox checkBox = (JCheckBox) event.getSource();
    if (checkBox.isSelected()) {
     checkBox.setSelected(true);
     checkBox2.setSelected(false);
     label.setText("check box 1 selected");
    }

   }
  });


  // 13. We add an ActionListener to the checkbox2
  // which will listen about the action user performs
  // over the checkbox2. Here user only checks and unchecks
  // it. As soon as user checks checkbox2 the text of the 
  // label changes from check box 1 selected to check box 2 selected
  // and checkbox1 is unchecked

  checkBox2.addActionListener(new ActionListener() {

   @Override
   public void actionPerformed(ActionEvent event) {

    JCheckBox checkBox = (JCheckBox) event.getSource();
    if (checkBox.isSelected()) {
     checkBox.setSelected(true);
     checkBox1.setSelected(false);
     label.setText("check box 2 selected");
    }

   }
  });


  // 14. for adding all these swing components to frame
  // we first get the content pane which returns a 
  // Container
  Container container = frame.getContentPane();

  // 15. We create a Layout for Swing Components
  // here we are using FlowLayout with value as "center'
  // it will make the swing components float to center
  FlowLayout layout = new FlowLayout(FlowLayout.CENTER);

  // 16. We set the layout for container
  container.setLayout(layout);

  // 17. We add the checkboxes and label to it
  container.add(checkBox1);
  container.add(checkBox2);
  container.add(label);

  // 18. after adding checkboxes and label, we make it 
  // visible on the frame by calling the method as 
  // setVisible and passing value as true.
  frame.setVisible(true);


 }

}




Output of the program :



















How to use and set ToolTip for Swing components in Java ?.

Program to demonstrate how to use and set ToolTip for Swing components in Java

package com.hubberspot.example;

import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class ToolTipDemo {

 public static void main(String[] args) {

  // 1. Create a simple frame by creating an object 
  // of JFrame. 
  JFrame frame = new JFrame();

  // 2. Give the frame a title "Tool Tip Frame" by calling 
  // setTitle method on frame object
  frame.setTitle("Tool Tip Frame");

  // 3. Give frame a size in pixels as say 300,300 
  frame.setSize(300, 300);

  // 4. set a operation when a user close the frame, here it 
  // closes the frame and exits the application
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  // 5. Create a button by creating an Object of class 
  // JButton. 
  JButton button = new JButton();

  // 6. set the text of button as "Button"
  button.setText("Button");

  // 7. Create a label by creating an Object of class 
  // JLabel
  JLabel label = new JLabel();

  // 8. set the text of label as "Label"
  label.setText("Label");

  // 9. In order to create a tool tip for label, 
  // button or any swing components, we call setToolTipText
  // method and passing it the information we want to display 
  // when user hovers over the swing components
  button.setToolTipText("Tool Tip for Button");
  label.setToolTipText("Tool Tip for Label");

  // 10. for adding all these swing components to frame
  // we first get the content pane which returns a 
  // Container
  Container container = frame.getContentPane();

  // 11. We create a Layout for Swing Components
  // here we are using FlowLayout with value as "center'
  // it will make the swing components float to center
  FlowLayout layout = new FlowLayout(FlowLayout.CENTER);

  // 12. We set the layout for container
  container.setLayout(layout);

  // 13. We add the button and label to it
  container.add(button);
  container.add(label);

  // 14. after adding button and label, we make it 
  // visible on the frame by calling the method as 
  // setVisible and passing value as true.
  frame.setVisible(true);
 }

}



Output of the program :





















 


How to calculate Exponential of a number through a Java program ?.

Program to demonstrate how to calculate Exponential of a number in Java.






Click here to download complete source code



package com.hubberspot.code;

import java.util.Scanner;

public class ExponentialCalculation {

 public static void main(String[] args) {

  // Create a Scanner object which will read 
  // values from the console which user enters
  Scanner scanner = new Scanner(System.in);

  // Getting input from user from the console
  System.out.println("Enter value of number ");

  // Calling nextDouble method of scanner for
  // taking a double value from user and storing
  // it in number variable
  double number = scanner.nextDouble();

  System.out.println();

  System.out.println("Calculating exponential of a number ... ");

  // In order to calculate exponential of a number 
  // we use Math class exp() static method which takes in a 
  // number and returns back the exponential of a number
  double result = Math.exp(number);

  // printing the result on the console
  System.out.println("Exponential of " + number + " is : " + result); 

 }

}


Click here to download complete source code

Output of the program :



 
© 2021 Learn Java by Examples Template by Hubberspot