import org.apache.commons.csv.* def fmt = CSVFormat.EXCEL .withHeader() .withDelimiter((char)'\t') for(record in fmt.parse(new FileReader("tab-sep-data.csv"))){ for(value in record){ println value } println "----" }
2014年9月4日木曜日
Apache Commons CSVとgroovyでタブ区切りのCSVデータを読み込む
Apache Commons CSVとgroovyでタブ区切りのCSVデータを読み込むには、以下のコードのようにwithDelimiter()を使用します。
2014年8月31日日曜日
SwingBuilderでマウスホバー時にラベルの色を変更する
SwingBuilderでマウスホバー時にラベルの色を変更するには、以下のコードの様にmouseEntered/mouseExitedを使用します。
サンプルコード
動作環境
groovy 2.2.2, JDK 1.7 update55
サンプルコード
import java.awt.* import javax.swing.* import groovy.swing.* sb = new SwingBuilder() sb.edt { frame( title: "example - label color when mouse over", show: true, resizable: true, pack: true, defaultCloseOperation: WindowConstants.EXIT_ON_CLOSE ){ label(id:"lbl1", text:"example label2", // 文字列の色 foreground: new Color(0xcc, 0xdd, 0xff), // 背景色(背景色を設定する場合はopaque=trueにする) background: new Color(0x00, 0x00, 0x00), opaque:true, // マウスホバー時 mouseEntered: { lbl1.setBackground(Color.WHITE) }, // マウスがラベル上から出た場合 mouseExited: { lbl1.setBackground(new Color(0x80, 0x80, 0x80)) }, constraints: BorderLayout.CENTER, font: new Font("Serif", Font.BOLD, 32) ) } }実行時画面
動作環境
groovy 2.2.2, JDK 1.7 update55
2014年8月29日金曜日
Raspberry pi上のgroovyでCPU温度・電圧を取得する
Raspberry pi上のgroovyでCPU温度・電圧を取得するには、以下のコードのようにSystemInfoのgetCpuTemperature()とgetCpuVoltage()を使用します。
@Grab(group='com.pi4j', module='pi4j-core', version='0.0.5') import com.pi4j.system.* println "cpu architecture:${SystemInfo.getCpuArchitecture()}" println "cpu temperature:${SystemInfo.getCpuTemperature()}" println "cpu voltage:${SystemInfo.getCpuVoltage()}"実行結果
cpu architecture:7 cpu temperature:56.2 cpu voltage:1.2
2014年8月28日木曜日
Apache Commons CSVとgroovyでNULL文字を含むCSVデータを読み込む
Apache Commons CSVとgroovyでNULL文字を含むCSVデータを読み込む場合、以下のコードのようにwithNullString()を使用します。
import org.apache.commons.csv.* def fmt = CSVFormat.EXCEL .withHeader() .withNullString("") // 空文字をnullとして取り扱う for(record in fmt.parse(new FileReader("data-with-null.csv"))){ for(value in record){ println value } println "----" }
2014年8月22日金曜日
Raspberry pi上のgroovyでモデル名称・シリアル番号を取得する
Raspberry pi上のgroovyでモデル名称・シリアル番号を取得するには、以下のコードのようにSystemInfoのgetBoardType()とgetSerial()を使用します。
@Grab(group='com.pi4j', module='pi4j-core', version='0.0.5') import com.pi4j.system.* println "board type:${SystemInfo.getBoardType().name()}" println "serial:${SystemInfo.getSerial()}"実行結果
board type:ModelB_Rev2 serial:00000000XXXXXXXX
2014年8月21日木曜日
Apache Commons CSVとgroovyでヘッダー行を読み飛ばしてCSVデータを読み込む
Apache Commons CSVとgroovyでヘッダー行を読み飛ばしてCSVデータを読み込むには、以下のコードを実行します。
import org.apache.commons.csv.* def fmt = CSVFormat.EXCEL .withHeader() for(record in fmt.parse(new FileReader("data-with-header.csv"))){ for(value in record){ println value } println "----" }
2014年8月17日日曜日
SwingBuilderでテキストペインでHTMLを表示する
SwingBuilderでテキストペインでHTMLを表示するには、以下のコードの様にtextPaneを使用します。
サンプルコード
動作環境
groovy 2.2.2, JDK 1.7 update55
サンプルコード
import java.awt.* import javax.swing.* import groovy.swing.* sb = new SwingBuilder() sb.edt { frame( title: "example - text pane", show: true, resizable: true, size: [300, 200], defaultCloseOperation: WindowConstants.EXIT_ON_CLOSE ){ scrollPane(){ textPane(id:"tp1", contentType:"text/html", editable:false, text:"""example<br><div bgcolor="#7799dd"><span color="white">line1</span></div><div bgcolor="#ccddff">line2</div>""", constraints: BorderLayout.CENTER ) } button(text:"OK", constraints: BorderLayout.SOUTH, actionPerformed: { sb.optionPane( message:"textarea=${sb.tp1.text}", messageType:JOptionPane.INFORMATION_MESSAGE) .createDialog("textarea").visible = true } ) } }実行時画面
動作環境
groovy 2.2.2, JDK 1.7 update55
登録:
投稿 (Atom)