0% found this document useful (0 votes)
34 views

Software Engineering It 53: Using The Jcreator

The document discusses the JComboBox class in Java which creates a drop-down list of options that can be displayed to the user as part of a graphical user interface. It provides examples of how to add, remove, and select items in a JComboBox. It also discusses if, if-else, and if-else if statements in Java for conditional execution of code blocks based on different conditions being true or false.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Software Engineering It 53: Using The Jcreator

The document discusses the JComboBox class in Java which creates a drop-down list of options that can be displayed to the user as part of a graphical user interface. It provides examples of how to add, remove, and select items in a JComboBox. It also discusses if, if-else, and if-else if statements in Java for conditional execution of code blocks based on different conditions being true or false.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Software Engineering IT 53

1

The JComboBox class creates a drop-down list of options that can be displayed to the user as part of
a graphical user interface (GUI).

A combo box is a commonly used graphical user interface widget (or control). Traditionally, it is a
combination of a drop-down list or list boxand a single-line editable textbox, allowing the user to either type
a value directly into the control or choose from a list of existing options by scrolling. Today, the original
distinction between a combo box and a drop-down list has often disappeared.
Combo boxes are typically applied to provide autocomplete or autotypefunctionality in a convenient way to
the user.
Using the JCreator
//Options for the JcomboBox

String[] fruitOptions = {Apple, Apricot, Banana
,Cherry, Date, Kiwi, Orange, Pear, Strawberry};

JcomboBox fruits = new JcomboBox(fruitOptions);

Using NetBeans
Adding an Item in the jComboBox

jComboBox1.addItem("Your Text Here");

Ex.

jComboBox1.addItem("Apple");

The code will allow the jComboBox1 to add an Item

Removing an Item in jComboBox

jComboBox1.removeItem("Item to Remove");

Ex.
jComboBox1.removeItem("Apple");

The code will allow the jComboBox1 to remove an Item

Removing all Items in jComboBox

jComboBox1 .removeAllItems();

The code will allow the jComboBox1 to remove all items
Software Engineering IT 53

2


Ex. Fruits Combobox



















Add Button

jComboBox1.addItem("Apple");
jComboBox1.addItem("Orange");
jComboBox1.addItem("Banana");
jComboBox1.addItem("Grapes");


Selecting an item to display

jtextBox1.setText=jComboBox1.getSelectedItem().toString();

Removing an Item

jComboBox1.removeItem("Orange");

Removing Selected Item

jComboBox1.removeItem(jtxt1.getText());

Removing All items on the jComboBox1

jComboBox1 .removeAllItems();

Select
Add Remove Clear
Software Engineering IT 53

3


The if-then and if-then-else Statements

The if-then Statement
The if-then statement is the most basic of all the control flow statements. It tells your program to execute a
certain section of code only if a particular test evaluates to true.

The simple if-else conditional statement allows the computer to choose one and only one of the given two
alternatives.

The condition is usually a comparison using the relational operators and in some cases it can be any
expression that evaluate to a numeric value as True of False.


The if-else statement has the following form.

if (boolean-expression) {
then-clause
} else {
else-clause
}

Ex. Love Bug















int Love;

Love=Integer.parseInt(jtxt1.getText());

if(Love==14344)
jtxt2.setText("I LOVE YOU VERY MUCH");
else
jtxt2.setText ("I HATE YOU");


Insert Number
Evaluate
Display
Software Engineering IT 53

4


Ex. Positive Negative
















jbtnEvaluate


Integer x = Integer.parseInt(jtxt1.getText());
if(x>=0)
{

jtxt2.setText ("Positive Number");
}
else
{
jtxt2.setText ("Negative Number");
}

Ex. Simple Highest Lowest















Enter Number 1
Positive
Display
Evaluate
Enter Number 1
Num2 is greater than Num1 Display
Evaluate
Enter Number 3
Software Engineering IT 53

5

int num1,num2;

Integer num1 = Integer.parseInt(jtxt1.getText());
Integer num2 = Integer.parseInt(jtxt2.getText());
if(num1>num2)
{
jtxt3.setText("Num1 is greater than Num2");
}
else
{

jtxt3.setText ("Num2 is greater than Num1");
}

Ex. TWO inputted Values Greater than and Less Than
Using Java Creator

import java.io.*;

public class GreaterLessthan
{
public static void main(String []args)
{
try
{
byte n1[]=new byte[255];
byte n2[]=new byte[255];

System.out.print("Enter Num1: ");
System.in.read(n1);
System.out.print("Enter Num2: " );
System.in.read(n2);

Integer num1 = Integer.parseInt(new String(n1).trim());
Integer num2 = Integer.parseInt(new String(n2).trim());

if(num1>num2)
{
System.out.println("Num1 is greater than Num2");
}
else
{
System.out.println("Num2 is greater than Num1");
}
}catch (IOException e){
}
}
}
Software Engineering IT 53

6

Age Bracket
Allow to vote for National Election is 18
If not it will display not allowed to vote
Ex.










First is to add a Declaration

int num1;

Integer num1 = Integer.parseInt(jtxt1.getText());

if(num1>=18)
{
jtxt2.setText("Allowed to Vote");
}
else
{

jtxt2.setText ("Not allowed to Vote");
}


If else if Statement

The if else if control construct allows the computer to evaluate three or more conditional
statements, but to execute only one associated statements
Enter Number 1
Not allowed to Vote
Display
Evaluate
Software Engineering IT 53

7

Conditional statements are used to perform different actions based on different conditions.
use this statement to select one of many blocks of code to be executed

An if statement can be followed by an optional else if...else statement, which is very useful to test
various conditions using single if...else if statement.
When using if , else if , else statements there are few points to keep in mind.
An if can have zero or one else's and it must come after any else if's.
An if can have zero to many else if's and they must come before the else.
Once an else if succeeds, none of the remaining else if's or else's will be tested.

If...else if...else Statement
Use the if....else if...else statement to select one of several blocks of code to be executed.

Syntax
if (condition1)
{
code to be executed if condition1 is true
}
else if (condition2)
{
code to be executed if condition2 is true
}
else
{
code to be executed if neither condition1 nor condition2 is true
}




Software Engineering IT 53

8

Example

public class Test {
public static void main(String args[]){
int x = 40;
if( x == 10 ){
System.out.print("Value of X is 10");
}else if( x == 20 ){
System.out.print("Value of X is 20");
}else if( x == 30 ){
System.out.print("Value of X is 30");
}else{
System.out.print("This is else statement");
}
Using GUI ang Input value







Code is like these
int x = Integer.parseInt(jtxt1.getText());
if( x == 10 ){
jtxt2.setText("Value of X is 10");
}else if( x == 20 ){
jtxt2.setText ("Value of X is 20");
Enter Number 10
Value of X is 10
Display
Evaluate
Software Engineering IT 53

9

}else if( x == 30 ){
jtxt2.setText ("Value of X is 30");
}else{
jtxt2.setText ("This is else statement");
}
Positive Negative with Zero Value







int num;
num=Integer.parseInt(jtxt1.getText());
if(num>=1)
{
jtxtoutput.setText("Postive");
}
else if(num==0)
{
jtxtoutput.setText("Neutral");
}
else
{
jtxtoutput.setText("Negative");
}


Enter Number -2
Negative
Display
Evaluate
Software Engineering IT 53

10

Love Forever
int num;
num = Integer.parseInt(jtxt1.getText());
if (num == 143)
jtxtoutput.setText ("I LOVE YOU");
else if (num == 14344)
jtxtoutput.setText ("I LOVE YOU VERY MUCH");
else if (num == 143444)
jtxtoutput.setText ("I LOVE YOU VERY VERY MUCH");
else if (num == 14313)
jtxtoutput.setText "(I LOVE YOU A LOT");
else if (num == 14324)
jtxtoutput.setText ("I LOVE YOU SO MUCH");
else
jtxtoutput.setText ("I HATE YOU");

Enter Number 14324
I LOVE YOU SO MUCH
Display
Evaluate

You might also like