Java Swing – JLabel with Image Icon

import javax.swing.*;
import java.awt.*;

public class JLabelWithIcon extends JFrame {
public JLabelWithIcon() throws HeadlessException {
initialize();
}

private void initialize() {
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout(FlowLayout.LEFT));

Icon icon = new ImageIcon(“ledgreen.png”);
JLabel label1 = new JLabel(“Full Name :”, icon, JLabel.LEFT);

JLabel label2 = new JLabel(“Address :”, JLabel.LEFT);
label2.setIcon(new ImageIcon(“ledyellow.png”));

getContentPane().add(label1);
getContentPane().add(label2);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new JLabelWithIcon().setVisible(true);
}
});
}
}

Your Ad Here

Java Swing – Format JLabel with HTML

import javax.swing.*;
import java.awt.*;

public class HTMLLabel extends JFrame {
public HTMLLabel() {
setTitle(“JLabel with HTML”);
initComponents();
}

private void initComponents() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 200);
Container container = getContentPane();
container.setLayout(new FlowLayout(FlowLayout.CENTER));

//
// Create a JLabel object that display a string formatted using HTML.
// 14 font size with red and italic.
//
JLabel label = new JLabel(“<html><font size=\”14\” color=\”ff0000\”><i>Hello World</i></font></html>”);
container.add(label);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new HTMLLabel().setVisible(true);
}
});
}
}

Your Ad Here

Java – Convert Map to JSON

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

public class MapToJson {
public static void main(String[] args) {
Map colours = new HashMap();
colours.put(“BLACK”, “#000000″);
colours.put(“RED”, “#FF0000″);
colours.put(“GREEN”, “#008000″);
colours.put(“BLUE”, “#0000FF”);
colours.put(“YELLOW”, “#FFFF00″);
colours.put(“WHITE”, “#FFFFFF”);

//
// Convert a Map into JSON string.
//
Gson gson = new Gson();
String json = gson.toJson(colours);
System.out.println(“json = ” + json);

//
// Convert JSON string back to Map.
//
Type type = new TypeToken>(){}.getType();
Map map = gson.fromJson(json, type);
for (String key : map.keySet()) {
System.out.println(“map.get = ” + map.get(key));
}
}
}

Output:

json = {“WHITE”:”#FFFFFF”,”BLUE”:”#0000FF”,”YELLOW”:”#FFFF00″,”GREEN”:”#008000″,”BLACK”:”#000000″,”RED”:”#FF0000″}
map.get = #FFFFFF
map.get = #0000FF
map.get = #FFFF00
map.get = #008000
map.get = #000000
map.get = #FF0000

Your Ad Here

Java MongoDB – Get Collection from Database

import java.net.UnknownHostException;
import java.util.Set;

import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
import com.mongodb.MongoException;

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

try {

Mongo mongo = new Mongo(“localhost”, 27017);
DB db = mongo.getDB(“yourdb”);

// get entire list of collections
Set<String> collections = db.getCollectionNames();

for (String collectionName : collections) {
System.out.println(collectionName);
}

// get a single collection
DBCollection collection = db.getCollection(“yourCollection”);
System.out.println(collection.toString());

System.out.println(“Done”);

} catch (UnknownHostException e) {
e.printStackTrace();
} catch (MongoException e) {
e.printStackTrace();
}

}
}

Your Ad Here

Java – Convert JSON to MongoDB Object

JSON Data to Convert:

{
‘name’ : ‘javac’,
‘age’ : 6
}

Conversion Code:

import java.net.UnknownHostException;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
import com.mongodb.util.JSON;

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

try {

Mongo mongo = new Mongo(“localhost”, 27017);
DB db = mongo.getDB(“yourdb”);
DBCollection collection = db.getCollection(“dummyColl”);

// convert JSON to DBObject directly
DBObject dbObject = (DBObject) JSON
.parse(“{‘name’:'javac’, ‘age’:6}”);

collection.insert(dbObject);

DBCursor cursorDoc = collection.find();
while (cursorDoc.hasNext()) {
System.out.println(cursorDoc.next());
}

System.out.println(“Done”);

} catch (UnknownHostException e) {
e.printStackTrace();
} catch (MongoException e) {
e.printStackTrace();
}
}
}

Output:

{ “_id” : { “$oid” : “4dc9ebb5237f275c2fe4959f”} , “name” : “javac” , “age” : 6}
Done

Your Ad Here
Page 1 of 1612345...10...Last »