Author Archive

AngularJS example for associative arrays

<html ng-app>
<head>
<script src=”libs/angular.js”></script>
<script>
function myController($scope){
$scope.employee=[{“name”:”ajith”,”job”:”manager”,”salary”:”$2000″},    {“name”:”manoj”,”job”:”clerk”,”salary”:”$1000″}    ]    ;
}
</script>
</head>
<body ng-controller=”myController”>
<h2>List Example</h2>
<table>
<tr>
<td>Name</td>
<td>Job</td>
<td>Salary</td>
</tr>

<tr>
<td>{{employee[0].name}}</td>
<td>{{employee[0].job}}</td>
<td>{{employee[0].salary}}</td>
</tr>

<tr>
<td>{{employee[1].name}}</td>
<td>{{employee[1].job}}</td>
<td>{{employee[1].salary}}</td>
</tr>

</table>
<p>{{employee.name}}</p>
</body>
</html>

AngularJS example for handling form with elements

<html ng-app>
<head>
<script src=”libs/angular.js”></script>
<script>
function formController($scope){
$scope.total=0;
$scope.num1=0;
$scope.num2=0;
$scope.calculate = function(){
$scope.total = parseInt($scope.num1) + parseInt($scope.num2);
}

}
</script>
</head>
<body ng-controller=”formController”>
<form>
<table>
<tr>
<td>Enter Number1</td><td>:<input ng-model=”num1″ ng-change=”calculate()”/></td>
</tr>
<tr>
<td>Enter Number2</td><td>:<input ng-model=”num2″ ng-change=”calculate()” /></td>
</tr>
</table>
<button
ng-click=”calculate()”>Calculate</button>
</form>
<p>{{total}}</p>
</body>
</html>

<html ng-app>
<head>
<script src=”libs/angular.js”></script>
<script>
function formController($scope){
$scope.empName=”Rajeev”;
$scope.changeName = function(){
$scope.empName=”ARJUN”;
}

}
</script>
</head>
<body ng-controller=”formController”>
<form>
<table>
<tr>
<td>Name</td><td>:<input ng-model=”empName” ng-change=”hello()” /></td>
</tr>
</table>
<button ng-click=”changeName()”>{{empName}}</button>
</form>
</body>
</html>

AngularJS example script for using objects

<html ng-app>
<head>
<script src=”libs/angular.js”></script>
<script>
function myController($scope){
var person={};
person.name=”James”;
person.age=”32″;
person.salary=”35000″;

$scope.person = person;
}
</script>
</head>
<body ng-controller=”myController”>
<table>
<tr>
<td>Name</td><td>:{{person.name}}</td>
</tr>
<tr>
<td>Age</td><td>:{{person.age}}</td>
</tr>
<tr>
<td>Salary</td><td>:{{person.salary}}</td>
</tr>
</body>
</html>

AngularJS script for handling click event

<html ng-app>
<head>
<script src=”libs/angular.js”></script>
<script>
function myController($scope){
$scope.message = “I like Angular JS”;
$scope.clickMe = function(){
$scope.message = “I hate Angular JS”;
}
}
</script>
</head>
<body ng-controller=”myController”>
<p>{{message}}</p>
<button ng-click=”clickMe()”>Click</button>
</body>
</html>

The following script will Create a Window or Frame in Java with Event Listener for window closing :

public class MainWindowExample {

private Frame myWindow;
public static void main(String[] args) {
myWindow = new Frame("My Window");
myWindow.setSize(400,300);
myWindow.setLayout(new GridLayout(3, 1));
//Adding Window Closing event Listener
myWindow.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
myWindow.setVisible(true);
}
}

Sending email in Java from localhost example

The following Java code snippet will send email:


public class SendEmail {

public static void main(String[] args) {

// Recipient's email ID needs to be mentioned.
String to = "to_address@gmail.com";

// Sender's email ID needs to be mentioned
String from = "from_address@gmail.com";

// Assuming you are sending email from localhost
String host = "smtp.gmail.com";

final String username = "sender_email_address@gmail.com";
final String password = "sender_gmail_password";

// Get system properties
Properties properties = System.getProperties();

// Setup mail server
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.port", "587");



Session session = Session.getInstance(properties, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});

try {

Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
message.setSubject("Test mail");
message.setText("Consider this as a test");
Transport.send(message);
System.out.println("Done");

} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}

Adding action to JButton example

The following code will show you how to add action to JButton:

public class WindowExample {

public static void main (String[] args){
JFrame frame = new JFrame("My Window");
frame.setVisible(true);
frame.setSize(500,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Adding JPanel to frame
JPanel panel = new JPanel();
frame.add(panel);

//Adding buttons to JPanel
JButton button = new JButton("Good Message");
panel.add(button);

//Adding buttons to JPanel
JButton button2 = new JButton("Bad Message");
panel.add(button2);

//Adding TextFieldto JPanel
JTextField txtMessage = new JTextField(20);
panel.add(txtMessage);

botton1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
txtMessage.setText("I like Java!");
}
});

botton2.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
txtMessage.setText("I hate Java!");
}
});

}

}

Using JPanels in Window Example

The following code snippet will show you how to use JPanels:

public static void main (String[] args){
JFrame frame = new JFrame("My Window");
frame.setVisible(true);
frame.setSize(500,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Adding JPanel to frame
JPanel panel = new JPanel();
frame.add(panel);

//Adding buttons to JPanel
JButton button = new JButton("Good Message");
panel.add(button);

//Adding buttons to JPanel
JButton button2 = new JButton("Bad Message");
panel.add(button2);
}

}

Creating a Window or Frame in Java

The following code snippet will create a window in java. You can use either eclipse or other IDE to execute the code.

public class WindowExample {

public static void main (String[] args){
JFrame frame = new JFrame("My Window");
frame.setVisible(true);
frame.setSize(500,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Page 1 of 5912345...102030...Last »