Dialogs
Download
1 / 29

Dialogs - PowerPoint PPT Presentation


  • 123 Views
  • Uploaded on

Dialogs. Displaying Text in a Dialog Box. Windows and dialog boxes Up to this our output has been to the screen Many Java applications use these to display output Package is javax.swing – contains classes for creation of GUIs.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Dialogs' - xandy


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

Displaying text in a dialog box
Displaying Text in a Dialog Box

Windows and dialog boxes

Up to this our output has been to the screen

Many Java applications use these to display output

Package is javax.swing – contains classes for creation of GUIs.

JOptionPane class provides prepackaged dialog boxes called message dialogs

showMessageDialog() – displays a dialog box displaying a message. Two parameters , position of the dialog box and the message.


Displaying text in a dialog box1
Displaying Text in a Dialog Box

// Printing multiple lines in dialog box.

import javax.swing.JOptionPane; // import class JOptionPane

public class Dialog1

{

public Dialog1() {

// display a dialog with the message

JOptionPane.showMessageDialog( null, "Welcome\nto\nJava" );

}

public static void main( String args[] )

{

new Dialog1();

} // end main

} // end class Dialog1

Import class JOptionPane

Show a message dialog with text


Displaying text in a dialog box2
Displaying Text in a Dialog Box

showMessageDialog() – displays a dialog box displaying a message. Two parameters , position of the dialog box and the message.

showMessageDialog() is a classmethod of JOptionPane class , it is a static method.

Such methods often define frequently used tasks that do not explicitly require the creation of an object.

A static method typically is called by using its class name dot operator and the method name.

JOptionPane.showMessageDialog( null, "Welcome\nto\nJava" );

Note we did not have to create an instance of the class JoptionPane to use the showMessageDialog() method.


Entering text in a dialog box
Entering Text in a Dialog Box

Input dialog

Allows user to input information

Created using method showInputDialog from class JOptionPane


Entering text in a dialog box1
Entering Text in a Dialog Box

// Basic input with a dialog box.

import javax.swing.JOptionPane;

public class NameDialog

{

public NameDialog()

{

// prompt user to enter name

String name =JOptionPane.showInputDialog( "What is your name?" );

// create the message

String message = String.format( "Welcome, %s, to Java Programming!", name );

// display the message to welcome the user by name

JOptionPane.showMessageDialog( null, message );

}

public static void main( String args[] )

{

new NameDialog();

}

} // end class NameDialog


Entering text in a dialog box2
Entering Text in a Dialog Box

showInputDialog() – parameter is the prompt

The user types characters in the text field,

User enters OK - showInputDialog() returns a String.

User enters Cancel - showInputDialog() returns null

static String method format() –returns a formatted String similar to printf


Displaying text in a dialog box3
Displaying Text in a Dialog Box

Windows and dialog boxes

Up to this our output has been to the screen

Many Java applications use these to display output

Package is javax.swing – contains classes for creation of GUIs.

JOptionPane class provides prepackaged dialog boxes called message dialogs

showMessageDialog() – displays a dialog box displaying a message. Two parameters , position of the dialog box and the message.


Displaying text in a dialog box4
Displaying Text in a Dialog Box

// Printing multiple lines in dialog box.

import javax.swing.JOptionPane; // import class JOptionPane

public class Dialog1

{

public Dialog1() {

// display a dialog with the message

JOptionPane.showMessageDialog( null, "Welcome\nto\nJava" );

}

public static void main( String args[] )

{

new Dialog1();

} // end main

} // end class Dialog1

Import class JOptionPane

Show a message dialog with text


Displaying text in a dialog box5
Displaying Text in a Dialog Box

showMessageDialog() – displays a dialog box displaying a message. Two parameters , position of the dialog box and the message.

showMessageDialog() is a classmethod of JOptionPane class , it is a static method.

Such methods often define frequently used tasks that do not explicitly require the creation of an object.

A static method typically is called by using its class name dot operator and the method name.

JOptionPane.showMessageDialog( null, "Welcome\nto\nJava" );

Note we did not have to create an instance of the class JoptionPane to use the showMessageDialog() method.


Entering text in a dialog box3
Entering Text in a Dialog Box

Input dialog

Allows user to input information

Created using method showInputDialog from class JOptionPane


Entering text in a dialog box4
Entering Text in a Dialog Box

// Basic input with a dialog box.

import javax.swing.JOptionPane;

public class NameDialog

{

public NameDialog()

{

// prompt user to enter name

String name =JOptionPane.showInputDialog( "What is your name?" );

// create the message

String message = String.format( "Welcome, %s, to Java Programming!", name );

// display the message to welcome the user by name

JOptionPane.showMessageDialog( null, message );

}

public static void main( String args[] )

{

new NameDialog();

}

} // end class NameDialog


Entering text in a dialog box5
Entering Text in a Dialog Box

showInputDialog() – parameter is the prompt

The user types characters in the text field,

User enters OK - showInputDialog() returns a String.

User enters Cancel - showInputDialog() returns null

static String method format() –returns a formatted String similar to printf


  • Exercises

  • Adapt our Hello World and Leap Year programs to include Swing Input and Output


Java packages
Java packages

Java Packages available with the JDK

java.io – for system input and output

java.math – for integer and decimal arithmetic

java.text - for handling text, dates, numbers …

java.util – for collections, date & time facilities also new class Scanner for Data input

java.net – for implementing networking apps

java.sql – for accessing data in a data source

and many more…


Class fields and class methods
Class fields and class methods

A class field is associated with the class in which it is defined rather than with each instance of the class.

public static final double PI = 3.14159

The static modifier says that the field is a class field.

The final modifier indicates that the value in the field does not change ie a constant


Using math class
Using Math Class

All methods of the Math class are class methods

Must use the class name to invoke the methods

Math.round(x), Math.pow(x,y), Math.sqrt(y)

A utility type class

A class field is associated with the class in which it is defined rather than with each instance of the class


Class fields and class methods1
Class fields and class methods

A class field is associated with the class in which it is defined rather than with each instance of the class.

public static final double PI = 3.14159

The static modifier says that the field is a class field.

The final modifier indicates that the value in the field does not change ie a constant


Class fields and class methods2
Class fields and class methods

A class methods are declared with the static modifier, they are associated with the class, rather than the instance.

Public static double radinsToDegrees(double rads)

{ return rads *180/PI;}

System.out.println(c+"\t"+ Math.round(c*9.0/5 + 32));

A class method can use any class fields and other class methods of its own class.


Static methods in class math
static Methods in Class Math

static method (or class method)

Applies to the class as a whole instead of a specific object of the class

Call a static method by using the method call:ClassName.methodName( arguments )

All methods of the Math class are static

example: Math.sqrt( 900.0 )


Temperaturetable
TemperatureTable

class TemperatureTable {

TemperatureTable() {System.out.println("Temperature Conversion Table");System.out.println("============================");System.out.println();System.out.println("C\tF");

for (int c = 5; c <= 20; c++) {

System.out.println(c+"\t"+ Math.round(c*9.0/5 + 32));

} }

public static void main ( String[] args) { new TemperatureTable (); }

}


Static fields and class math
static Fields and Class Math

Constants

Keyword final

Cannot be changed after initialization

static fields (or class variables)

Are fields where one copy of the variable is shared among all objects of the class

Math.PI and Math.E are final static fields of the Math class



Static method main
static Method main

Method main

main is declared static so it can be invoked without creating an object of the class containing main

Any class can contain a main method

The JVM invokes the main method belonging to the class specified by the first command-line argument to the java command


Account example
Account example

Account class

Interest rate – have the same interest rate for all accounts

Declare it as a class field / attribute

public class Account {

private int accountno;

private String name;

private String address;

private double balance;

static double interestrate = 3.5; //class field

static void changeRate(double rate){ // class method

interestrate = rate;}

To change the interest rate , we need to create a class method to update it.


Account example1
Account example

AccountTest class

To change the interest rate , we use the class method .

Use the Class name . Class method name

Account.changeRate(4.5); //class method

How would you update the account class to automatically allocate a new

account number for each account created.



  • import java.io.*;

  • import java.util.*;

  • class VectorApp{ public static void main(String[] args){

  • Vector v = new Vector(20);

  • Integer j = null;

  • int i;


  • System.out.println("Generating random integers between 1 and 100, wrapping it as integer objects and adding to a vector object");

  • for(i=0;i<10;i++){

  • j = new Integer((int)(Math.random() * 100)); v.addElement(j);

  • System.out.println("Added Integer object elements " + j);

  • }

  • System.out.println("size of the vector "+v.size()); System.out.println("capacity of the vector "+v.capacity()); Enumeration enum = v.elements();

  • while(enum.hasMoreElements())

  • System.out.println("Enumeration of the elements in the vector container "+(Integer)enum.nextElement());

  • System.out.println("");

  • }

  • }


ad