Created
March 29, 2013 02:34
-
-
Save dulichan/5268354 to your computer and use it in GitHub Desktop.
A small scala app with Swing interface that interact with MongoDB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.dchan.awesomebank | |
import scala.swing.SimpleSwingApplication | |
import scala.swing.MainFrame | |
import scala.swing.Button | |
import javax.swing.JPanel | |
import java.awt.BorderLayout | |
import scala.swing.FlowPanel | |
import scala.swing.TextField | |
import scala.swing.Label | |
import scala.swing.TextArea | |
import scala.swing.Action | |
import scala.swing.event.ButtonClicked | |
import com.mongodb.DBCollection | |
import com.mongodb.BasicDBObject | |
import scala.swing.BoxPanel | |
import scala.swing.Orientation | |
import scala.swing.Table | |
import javax.swing.table.DefaultTableModel | |
object AwesomeApp extends SimpleSwingApplication { | |
def createTxt = new TextField { | |
columns = 5 | |
} | |
def createTxArea = new TextArea { columns = 10; rows = 10; } | |
val eventTxt = createTxt | |
val descriptionTxt = createTxArea | |
val confirmButton = new Button { text = "Remember" } | |
val viewButton = new Button { text = "View" } | |
val viewTbl = new Table(Array(), Array("")){} | |
def top = new MainFrame { | |
title = "Hellow World" | |
var s = new BoxPanel(Orientation.Vertical){ | |
contents +=new FlowPanel(new Label("Event"), eventTxt, new Label("Description"), descriptionTxt, confirmButton, viewButton) | |
contents +=new FlowPanel(new Label("Past Events"), viewTbl) | |
} | |
contents = s | |
} | |
listenTo(confirmButton, viewButton) | |
reactions += { | |
case e: ButtonClicked => { | |
var collection = MongoUtil.getDB().getCollection("inspriations") | |
if (e.source.equals(viewButton)) { | |
var cursor = collection.find() | |
try { | |
while(cursor.hasNext()){ | |
var obj = cursor.next() | |
println(obj) | |
var model =viewTbl.model.asInstanceOf[DefaultTableModel] | |
model.addRow(Array[Object](obj.toString())) | |
} | |
} finally { | |
cursor.close() | |
} | |
} else if (e.source.equals(confirmButton)) { | |
var bsonobj = new BasicDBObject("eventText", eventTxt.text) | |
bsonobj.append("description", descriptionTxt.text) | |
collection.insert(bsonobj) | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.dchan.awesomebank | |
import com.mongodb.MongoClient | |
import com.mongodb.DB | |
object MongoUtil { | |
val client: MongoClient = new MongoClient() | |
val db = client.getDB("awesomeApp") | |
def getDB(): DB = { | |
return db | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment