-->

Sunday, May 18, 2014

Type of Statements used in JAVA

Statements are roughly equivalent to sentences in natural languages. A statement forms a complete unit of execution. The following types of expressions can be made into a statement by terminating the expression with a semicolon ( ; )
  • Assignment expressions
  • Any use of ++ or - -
  • Method calls
  • Object creation expressions
These kinds of statement are called expression statement. Here are some example of expression statements :

Avalue = 8933.234;                                          // assignment statement
Avalue++ ;                                                        // increment statement
System . out . println (avalue);                         // method call statement
Integer integerobject = new integer (4);           // object creation statement

In addition to these kinds of expression statements, there are two other kinds of statements. A declaration statement declares a variable. You’ve seen many examples of declaration statement.
Double avalue = 8933.234;            // declaration statement

A control flow statement regulates the order in which statements get executed. For loop and  if statements are both examples of control flow statements.

Block

A block is group of zero or more statements between balanced braces and can be used anywhere a single statement is allowed. The following listing shows two blocks.
If  (character . I suppercase(achar))
{
    Labe l1 . settext (“the character” + achar + “is upper case .”) ;
}
Else
{
    Labe l1. Settext (“the character” + achar + “is lower case .”) ;
    Label2. Settext(“thank you”) ;
}

Character.isLowerCase( )    tests whether a character is in lowercase.
Character.isUpperCase( )        tests whether a character is in uppercase.
Character.toLowerCase( )        converts the case of a character to lowercase
Character.toUpperCase( )        converts the case of a character to uppercase


First Block:
 If (character.isUpperCase(achar))
{            // block1 begins
    Label1.Settext (“ The character “ + achar + “ is upper case.”) ;
}            // end of block 1
Else
 
Another Block:
{            // block2 begins
Label1.Settext(“ The character “ + achar + “ is lower case . “) ;
}            // end of block2

See, the beginning and end of blocks have been marked.
A Block is a group of zero or more statements between balanced braces and can be used anywhere a single statement is allowed.
In this book, we shall be following conventional style where opening brace of the block is not put in a separate line, rather it is placed in continuation with the previous statement (whose part the block is).
For instance, rather than showing
If (a > b) { : }
We shall be writing
If (a > b)   {     :  }
Opening brace of the block in continuation with previous statement

Monday, March 3, 2014

Increment and Decrement Operators in Java Programming

Earlier article was about the operators in java with some of the binary operators, example of each. This article is about plus (+) operator and increment/decrement operator in brief.

Operator + with strings

Programmer have used the operator ‘+’ with numbers. When you use + with numbers, the result is also a number. However, if you use operator + with strings, it concatenates them for example:

(5 + 6) results in to 11.
(“5” + “6”) results in to “56”.
(“17” + “A, V. Vihar”) result in to “17 A, V. Vihar”
(“abc” + “123”) results in to “abc 123”
(“” + 5 + “xyz”) results in to “5xyz”
(“” + 5) results in to “5”

(In above two expressions Java would internally convert 5 in to “5” first and then concatenate with “xyz” and “” respectively.)

Increment/Decrement Operators (++, --)

Java includes two useful operators not generally found in other computer languages (expect C and C++). These are the increment and decrement operators, ++ and --. The operators ++ adds 1 to its operand, and – subtracts one.

In other words,
a = a + 1;        same as   ++a ; or a++;
And
a = a – 1          same as --a ; or a --;

However, both the increment and decrement comes into two varieties: they may either precede of=r follow the operand. The prefix version comes before the operand (as in ++a or --a) and the postfix version comes after the operand (as in a++ or a--). The two version have the same effect upon the operand, but they differ when they take place in an expression, that would be discussed in later articles.

Monday, January 6, 2014

How to Create a GUI application Showing Details in I-Card Format, NetBeans: Java Programming

NetBeans provides a simple and easy way to create a new project and input some information in sql programming. The article will let the programmer do the same thing.
  • Start NetBeans and create a new project and add four labels and four textfields from the palette tab as explained in earlier article.

  • Set following font properties for all the labels and textfields.
    Type: Comic Sans MS (you may choose any other font if you wish)
    Style:
    Bold
    Size:
    12

  • Adjust the placement of Heading, whose properties you set just now, by dragging it, so that it appears in the middle horizontally. Set name property of the labels respectively:
    jLabel1   to    titlelabel1
    jLabel2   to    firstNameLabel
    jLabel3   to    lastNameLabel
    jLabel4    to   classLabel
    jTextField1    to        firstNameTextField
    jTextField2    to        lastNameTextField
    jTextField3    to        classTextField
    jTextField4    to        sectionTextField

  • Set text property for these labels as given below:
    titleLabel1  to  Title(Mr/Ms)
    firstnameLabel  to  First Name
    lastnameLabel    to    Last Name
    classLabel  to  Class

  • Also set the text property of each of null string i.e. “ “ and resize them as per the screenshot. Now your frame should look like the one shown in below
How to Create a GUI application Showing Details in I-Card Format, NetBeans: Java Programming
  • Add a Button control by dragging it from Palette to frame. Name it as okButton. Set its text property to Generate and its font remain as is.
Before you add text area to frame, you may need to increase the size of the frame. For this drag the boundary of the frame in desired direction to get its desired size.
  • Just as you added other controls, add a text field control to your frame and give it a size according to I-Card. Name it as repTextArea. To name the text area field, you need to click the jScroolPane control in inspector window. A Scroll pane automatically gets added to your frame when you add a text area.

  • Adding Functionality to Frame
    Now double click on the boundary of the push button i.e., the okButton in design space. The code editor window will get opened. In it, without touching the cursor, simply type the following code. For now you won’t understand it. But it will be clear to you later when we’ll be talking about java concepts.
When you use getText() with a control’s name followed by a dot(.). It returns the text stored in the control. So the code will give the text stored in the control whose name is titlenameField.

How to Create a GUI application Showing Details in I-Card Format, NetBeans: Java Programming

Now save your work by pressing Ctrl + S and press F6 to run your project. You may also click Run icon on the toolbar. See your application run. After entering details in text fields, click on Generate. Wow, isn’t it similar to one shown in screenshot?

How to Create a GUI application Showing Details in I-Card Format, NetBeans: Java Programming

Saturday, January 4, 2014

How to Create a GUI application displaying a String in NetBeans: Java Programming

NetBeans provides a simple and easy way to create a new project and display a string in sql programming. The article will let the programmer do the same thing.
  • Start NetBeans and create a new project as explained in earlier article. You can change the name of the project otherwise it will automatically set. Add new frame in the same way discussed earlier. By default the name of frame will be NewJFrame.
  • Draw a Label control on your frame by dragging it from the Swing Controls section of the Palette.
  • To change the name of this Label, right click on its default name, which is JLabel1, under the Inspector window and select Change Variable Name……. command as shown in the image.
How to Create a GUI application displaying a String in NetBeans: Java Programming
  • Keeping the label control selected in its design space, do in the Properties window :

    Click the ellipsis button of the font property and selected font as:
    Type: Comic Sans MS (you may choose any other font if you wish)
    Style: Bold
    Size: 18
  • Type "Computer Programming" (without quotes) in the text property's box or by clicking at its ellipsis button. Now your project window will appear something like:
How to Create a GUI application displaying a String in NetBeans: Java Programming
  • Adjust the placement of the title label, whose properties you set just now, by dragging it, so that it appears in the middle horizontally.
  • On the same lines, add one more Label to your frame by dragging it from the Palette. Set its font as per you wish. Select the colour of the font by clicking the ellipsis of the foreground property in Properties window. Now specify its text as: "A Complete Programming Blog with SQL, JAVA, C, C#".
  • Adjust its placement as per you wishes. Now your project window will appear somewhat like:
How to Create a GUI application displaying a String in NetBeans: Java Programming
  • Right click on the jFrame you have created and click on Run File, it will show the frame which will look like:
How to Create a GUI application displaying a String in NetBeans: Java Programming

In this workshop you have learnt about following properties of jLabel control.
  • font: The property used to specify the display font, style and size for the display text of the jlabel. Font is selected by clicking ellipsis button in font property's box.
  • foreground: The property used to specify foreground color of jLabel, you can select desired colors by clicking of the foreground property.
  • text: The property used to specify the display text. You can either type the display text directly in front of text property’s name click to type it in a separate box. Small texts you can type directly, for big texts, its better them separately.

Friday, December 13, 2013

Role of Basic Graphical Controls of Programming: Introduction to Java

The Palette tab of a Graphical controls offered by Java Swing contains the tools that you can use to draw controls on your forms/windows/ frames while programming. The area on the frame where GUI components are placed is called content pane.

Role of Basic Graphical Controls of Programming: Introduction to Java

In earlier article, we have discussed about what are components. You have seen many controls listed on the Swing control palette. Here’s a list with briefly description about role and purpose of some commonly used controls.

  • JFrame provides the basic attributes and behavior of a window. A frame is displayed as a separate window with its own tittle bar.
  • JLable allows un-editable text (i.e., that user cannot change) or icons to be displayed.
  • JTextField allows user input, can also be used to be display text. As it can be edited you can also call it edit field.
  • JButton provides button capabilities. An action Event is generated when button is pushed.
  • JCheckBox provides check box. Checkboxes are used to allow a user select multiple choices, e.g., a student can select 5 subject out of 17 subject-choices. A check boxes shows an X in box in front of it when selected.
  • JList is a list of items from which a selection can be made. From a list, multiple elements can be selected.
  • JComboBox provides a drop-down list of items from which a selection can be made or new items can be added. It is a combination of text field and list.
    Note: The area on the frame where GUI components are placed is called content pane.
  • JPanel is a supporting container that cannot be displayed on its own but must be added to another container. Panels can be added to the content pane of a frame. They help to organize the components in a GUI.
  • JRadioButton provides radio buttons. Radio buttons are option buttons that can be turned on or off. The radio buttons provide mutually exclusive options.

Note: Though radio buttons and checkboxes appear to function similarly, yet there is an important difference-when a radio button is selected, all other radio buttons in its same group are automatically unselected. On the other hand, any no. of checkboxes can be selected.

JFC, Toolkit and Software Tiers used in GUI Programming: Introduction to JAVA

In JAVA, GUI features are supported via JFC. JFC is short for Java Foundation Classes, which encompass a group of features for building graphical user interfaces (GUI's) and adding rich graphics functionally and interactivity to Java applications. One major feature of JFC is Swing API, which includes everything from buttons to split panes to tables and every other functionally to create a GUI application.

Our later articles will describe, how to build GUI programs using Swing API’s features. The GUI programs that will create in Java will contain three tiers of software.
  • Graphical components make up the GUI such as a button or text box or a menu etc. Each graphical components has certain Properties. A property is a characteristic of an object such as its size, colour, title etc. You’ll learn about basics graphical components that Java’s Swing API offers, later in the articles.
  • Events Listeners that get notified about the events and respond to them.
  • The graphical Event Handler that do the work for the user in case the event occurs.
The graphical components are those like the one listed above. You can use them as they are, or extend them into your own classes. Event listeners are registered with GUI objects. In case of occurrence of an event, the objects notifies its event listeners them invokes event-handler method then executes the Java code written in it.

Java GUI toolkit

You know that a GUI in Java is created with at least three kinds of objects: components, events and listeners. A components is an object that defines a screen elements such as a push button, text field, scroll bar, menu, etc. the components that you can use for creating GUI in Java are available through Swing API.
A component is an object that defines a screen element such as a push button, text field, scroll bar, menu etc.

Types of Graphical Components

The graphical controls that you put in GUI applications are broadly of two types: the Container control and the Child control.

The container is control that can hold other control within it e.g. a Panel (there can be multiple controls inside a Panel) or Panes or simply frame (you can put so many controls on it). Control inside a container are known as a child controls. The child controls can exist completely inside their containers. That means you can’t move them outside their container and if try to drag them beyond and the boundary of their container, part of the control gets hidden. When you delete a container control, all its child controls automatically get deleted.

Do you know that Java GUI programming demands that all swing components s must contained in a container? A GUI application must contain a top-level container that can hold other controls in it.
A control is also known as a widget (short for window gadget). You can think of widget/ control as an element of a GUI that can be seen and that display an information and with user, e.g., a window, text field, frame etc. are widgets.
Graphical Controls of Swing
© Copyright 2013 Computer Programming | All Right Reserved