2014年4月30日水曜日

SwingBuilderでツールバーを使用する

SwingBuilderでツールバーを使用するには、以下のコードの様にtoolBarを使用します。
import java.awt.*
import javax.swing.*
import groovy.swing.*

sb = new SwingBuilder()
sb.edt {
  frame(
    title: "Toolbar example",
    show: true,
    resizable: true,
    size: [300, 200],
    defaultCloseOperation: WindowConstants.EXIT_ON_CLOSE
  ){
    toolBar(constraints: BorderLayout.NORTH){
      button(text:"hello", actionPerformed:{
        sb.optionPane(message:"hi!",
          messageType:JOptionPane.INFORMATION_MESSAGE)
          .createDialog("test").visible = true
      })
    }
  }
}
実行時画面

動作環境
groovy 2.2.2, JDK 1.7 update55

2014年4月29日火曜日

SwingBuilderでラジオボタンを使用する

SwingBuilderでラジオボタンを使用するには、以下のコードの様にbuttonGroupを作成してradioButtonを作成します。
import java.awt.*
import javax.swing.*
import groovy.swing.*

sb = new SwingBuilder()
sb.edt {
  frame(
    title: "radio button example",
    show: true,
    resizable: true,
    pack: true,
    defaultCloseOperation: WindowConstants.EXIT_ON_CLOSE,
    layout:new FlowLayout()
  ){
    def bg = buttonGroup(id:"bg")
    radioButton(id:"rb1", text:"option1", selected:true, buttonGroup:bg)
    radioButton(id:"rb2", text:"option2", buttonGroup:bg)
    radioButton(id:"rb3", text:"option3", buttonGroup:bg)
    button(text:"OK", constraints: BorderLayout.SOUTH,
      actionPerformed: {
        sb.optionPane(
          message:"rb1=${sb.rb1.selected}, rb2=${sb.rb2.selected}, rb3=${sb.rb3.selected}",
          messageType:JOptionPane.INFORMATION_MESSAGE)
          .createDialog("radiobutton").visible = true
      }
    )
  }
}
実行時画面

動作環境
groovy 2.2.2, JDK 1.7 update55

2014年4月28日月曜日

SwingBuilderでコンボボックスを使用する

SwingBuilderでコンボボックスを使用するには、以下のコードを実行します。
import java.awt.*
import javax.swing.*
import groovy.swing.*

sb = new SwingBuilder()
sb.edt {
  frame(
    title: "Combobox example",
    show: true,
    resizable: true,
    pack: true,
    defaultCloseOperation: WindowConstants.EXIT_ON_CLOSE
  ){
    def options = ["example option1", "example option2", "example option3"]
    comboBox(id:"cb1", items: options,
      constraints: BorderLayout.NORTH
    )
    button(text:"OK", constraints: BorderLayout.SOUTH,
      actionPerformed: {
        sb.optionPane(
          message:"combobox1=${sb.cb1.selectedItem}, index=${sb.cb1.selectedIndex}",
          messageType:JOptionPane.INFORMATION_MESSAGE)
          .createDialog("combobox").visible = true
      }
    )
  }

}
実行時画面

動作環境
groovy 2.1.7, JDK 1.7

2014年4月27日日曜日

SwingBuilderでチェックボックスを使用する

SwingBuilderでチェックボックスを使用するには、以下のコードを実行します。
import java.awt.*
import javax.swing.*
import groovy.swing.*

sb = new SwingBuilder()
sb.edt {
  frame(
    title: "Checkbox example",
    show: true,
    resizable: true,
    pack: true,
    defaultCloseOperation: WindowConstants.EXIT_ON_CLOSE
  ){
    // label -> 表示文字列
    checkBox(id:"chk1", label:"example checkbox1",
      constraints: BorderLayout.NORTH
    )
    // selected -> 選択状態
    checkBox(id:"chk2", label:"example checkbox2",
      selected:true, constraints: BorderLayout.CENTER
    )
    button(text:"OK", constraints: BorderLayout.SOUTH,
      actionPerformed: {
        sb.optionPane(
          message:"checkbox1=${sb.chk1.selected}, checkbox2=${sb.chk2.selected}",
          messageType:JOptionPane.INFORMATION_MESSAGE)
          .createDialog("checkbox").visible = true
      }
    )
  }

}
実行時画面

動作環境
groovy 2.1.7, JDK 1.7