ラベル SwingBuilder の投稿を表示しています。 すべての投稿を表示
ラベル SwingBuilder の投稿を表示しています。 すべての投稿を表示

2014年10月9日木曜日

SwingBuilderでラベル内のアイコンに対するテキスト位置を設定する

SwingBuilderでラベル内のアイコンに対するテキスト位置を設定するには、以下のコードのようにhorizontalTextPosition, verticalTextPositionを使用します。

サンプルコード
import java.awt.*
import javax.swing.*
import groovy.swing.*

sb = new SwingBuilder()
sb.edt {
  frame(
    title: "example : text position",
    show: true,
    resizable: true,
    pack: true,
    defaultCloseOperation: WindowConstants.EXIT_ON_CLOSE
  ){
    label(text:"left", icon:imageIcon(file:"e:/icons/home.png"),
      horizontalTextPosition: JLabel.LEFT, constraints: BorderLayout.WEST
    )
    label(text:"top", icon:imageIcon(file:"e:/icons/plane.png"),
      horizontalTextPosition: JLabel.CENTER,
      verticalTextPosition: JLabel.TOP, constraints: BorderLayout.NORTH
    )
    label(text:"right", icon:imageIcon(file:"e:/icons/mobile2.png"),
      horizontalTextPosition: JLabel.RIGHT, constraints: BorderLayout.EAST
    )
    label(text:"bottom", icon:imageIcon(file:"e:/icons/computer.png"),
      horizontalTextPosition: JLabel.CENTER,
      verticalTextPosition: JLabel.BOTTOM, constraints: BorderLayout.SOUTH
    )
  }
}
実行時画面

動作環境
groovy 2.2.2, JDK 1.7 update55

2014年9月29日月曜日

SwingBuilderでラベルにツールチップを設定する

SwingBuilderでラベルにツールチップを設定するには、以下のコードの様にtoolTipTextを使用します。
サンプルコード
import java.awt.*
import javax.swing.*
import groovy.swing.*

sb = new SwingBuilder()
sb.edt {
  frame(
    title: "example - label with tooltip",
    show: true,
    resizable: true,
    pack: true,
    defaultCloseOperation: WindowConstants.EXIT_ON_CLOSE
  ){
    label(text:"Sample label",
      // フォント
      font: new Font("Arial", Font.BOLD, 40),
      constraints: BorderLayout.CENTER,
      toolTipText: "サンプルのツールチップ"
    )
  }
}
実行時画面

動作環境
groovy 2.2.2, JDK 1.7 update55

2014年9月22日月曜日

SwingBuilderでディレクトリ選択ダイアログを使用する

SwingBuilderでディレクトリ選択ダイアログを使用するには、以下のコードの様にfileChooserを使用します。
サンプルコード
import javax.swing.*
import groovy.swing.*

sb = new SwingBuilder()
def dlg = sb.fileChooser(dialogTitle:"select a directory.",
  fileSelectionMode: JFileChooser.DIRECTORIES_ONLY)

if( dlg.showOpenDialog() == JFileChooser.APPROVE_OPTION ){
  println dlg.selectedFile.toString()
} else {
  println "cancelled."
}

動作環境
groovy 2.2.2, JDK 1.7 update55

2014年9月18日木曜日

Apache Commons CSVとSwingBuilderでCSVデータを読み込み、テーブルに表示する

Apache Commons CSVとSwingBuilderでCSVデータを読み込み、テーブルに表示するには、以下のコードを実行します。

import javax.swing.*
import groovy.swing.*
import org.apache.commons.csv.*

def fmt = CSVFormat.EXCEL.withHeader()

println "file:${args[0]}"
def parser = fmt.parse(new FileReader(args[0]))
def data = []
parser.iterator().each { data.add(it.toMap()) }

sb = new SwingBuilder()
sb.edt {
  frame(
    title: args[0],
    show: true,
    resizable: true,
    size: [300, 100],
    defaultCloseOperation: WindowConstants.EXIT_ON_CLOSE
  ){
    scrollPane(){
      table(){
        tableModel(list: data){
          parser.getHeaderMap().each { key, index ->
            propertyColumn( header:key, propertyName: key, editable: false )
          }
        }
      }
    }
  }
}

2014年8月31日日曜日

SwingBuilderでマウスホバー時にラベルの色を変更する

SwingBuilderでマウスホバー時にラベルの色を変更するには、以下のコードの様にmouseEntered/mouseExitedを使用します。
サンプルコード
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月17日日曜日

SwingBuilderでテキストペインでHTMLを表示する

SwingBuilderでテキストペインでHTMLを表示するには、以下のコードの様にtextPaneを使用します。
サンプルコード
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

2014年7月13日日曜日

SwingBuilderでテーブルにポップアップメニューを表示する

SwingBuilderでテーブルにポップアップメニューを表示するには、以下のコードのようにpopupMenuでポップアップメニューを作成しcomponentPopupMenuで設定します。

サンプルコード
import java.awt.*
import javax.swing.*
import groovy.swing.*

data = [
  [category:'City', city:'Tokyo'],
  [category:'City', city:'Osaka'],
  [category:'City', city:'Sapporo']
]

sb = new SwingBuilder()
def popupMenu = sb.popupMenu() {
  menuItem() {
    action( name:'Information',
      closure:{
        if( sb.table.getSelectedRowCount() != 0 ){
          sb.optionPane(
            message:(sb.table.getSelectedRows() as ArrayList<Integer>)
              .collect {sb.table.model.getValueAt(it,1)}.join(","),
            messageType:JOptionPane.INFORMATION_MESSAGE)
            .createDialog("table").visible = true
        } else {
          sb.optionPane(
            message:"no row selected",
            messageType:JOptionPane.INFORMATION_MESSAGE)
            .createDialog("table").visible = true
        }
      }
    )
  }
}

sb.edt {
  frame(
    title: "example - table with popup menu",
    show: true,
    resizable: true,
    size: [300, 100],
    defaultCloseOperation: WindowConstants.EXIT_ON_CLOSE
  ){
    scrollPane(){
      table(id:"table", componentPopupMenu:popupMenu){
        tableModel(list: data){
          propertyColumn( header:"category", propertyName: "category", editable: false )
          propertyColumn( header:"city", propertyName: "city", editable: false )
        }
      }
    }
  }
}
実行時画面

動作環境
groovy 2.2.2, JDK 1.7 update55

2014年7月11日金曜日

SwingBuilderでラベルのアイコンとテキスト間の幅を設定する

SwingBuilderでラベルのアイコンとテキスト間の幅を設定するには、以下のコードのようにiconTextGapに幅を指定します。

サンプルコード
import java.awt.*
import javax.swing.*
import groovy.swing.*

sb = new SwingBuilder()
sb.edt {
  frame(
    title: "example : gap between label and icon",
    show: true,
    resizable: true,
    pack: true,
    defaultCloseOperation: WindowConstants.EXIT_ON_CLOSE
  ){
    label(text:"example label1", icon:imageIcon(file:"e:/icons/home.png"),
      iconTextGap:10, constraints: BorderLayout.NORTH
    )
    label(text:"example label2", icon:imageIcon(file:"e:/icons/plane.png"),
      iconTextGap:20, constraints: BorderLayout.CENTER
    )
    label(text:"example label3", icon:imageIcon(file:"e:/icons/mobile2.png"),
      iconTextGap:30, constraints: BorderLayout.SOUTH
    )
  }
}
実行時画面

動作環境
groovy 2.2.2, JDK 1.7 update55

2014年7月9日水曜日

SwingBuilderでテーブルを単一行選択モードに設定する

SwingBuilderでテーブルを単一行選択モードに設定するには、以下のコードのようにselectionModeにListSelectionModel.SINGLE_SELECTIONを指定します。

サンプルコード
import java.awt.*
import javax.swing.*
import groovy.swing.*

data = [
  [category:'Apache', product:'HTTPD'],
  [category:'Apache', product:'Tomcat']
]

sb = new SwingBuilder()
sb.edt {
  frame(
    title: "example - single row selection",
    show: true,
    resizable: true,
    size: [300, 100],
    defaultCloseOperation: WindowConstants.EXIT_ON_CLOSE
  ){
    scrollPane(){
      table(selectionMode:ListSelectionModel.SINGLE_SELECTION){
        tableModel(list: data){
          propertyColumn( header:"category", propertyName: "category", editable: false )
          propertyColumn( header:"product", propertyName: "product", editable: false )
        }
      }
    }
  }
}
実行時画面

動作環境
groovy 2.2.2, JDK 1.7 update55

2014年7月7日月曜日

SwingBuilderでテーブルのデフォルトの選択色を設定する

SwingBuilderでテーブルのデフォルトの選択色を設定するには、以下のUIManagerのキーを設定します。
  • Table.selectionBackground : テーブル選択時の背景色
  • Table.selectionForeground : テーブル選択時の文字色

サンプルコード
import java.awt.*
import javax.swing.*
import groovy.swing.*

// テーブルのデフォルトの選択色を設定
UIManager.put("Table.selectionBackground", new Color(0xff, 0xff, 0xdd))
UIManager.put("Table.selectionForeground", new Color(0x77, 0x99, 0xdd))

data = [
  [category:'Browser', product:'Firefox'],
  [category:'Browser', product:'IE']
]

sb = new SwingBuilder()
sb.edt {
  frame(
    title: "example - default colors for selected rows",
    show: true,
    resizable: true,
    size: [300, 100],
    defaultCloseOperation: WindowConstants.EXIT_ON_CLOSE
  ){
    scrollPane(){
      table(){
        tableModel(list: data){
          propertyColumn( header:"category", propertyName: "category", editable: false )
          propertyColumn( header:"product", propertyName: "product", editable: false )
        }
      }
    }
  }
}
実行時画面

動作環境
groovy 2.2.2, JDK 1.7 update55

○テーブルに関するその他のエントリ

2014年7月5日土曜日

SwingBuilderでデスクトップペインの背景色を設定する

SwingBuilderでデスクトップペインの背景色を設定するには、以下のコードのようにdesktopPaneのbackgroundに背景色を設定します。
サンプルコード
import java.awt.*
import javax.swing.*
import groovy.swing.*

sb = new SwingBuilder()
sb.edt {
  frame(
    title: "example - background color for desktop pane",
    show: true,
    resizable: true,
    size: [300, 300],
    defaultCloseOperation: WindowConstants.EXIT_ON_CLOSE
  ){
    desktopPane(id:"dp", background: new Color(0xff, 0xff, 0xcc) ){
      internalFrame(id: "if1", closable:true, iconifiable:true, maximizable:true, resizable:true,
        title: "internal frame1", visible:true)
      {
        label(text: "internal frame1")
      }.pack()
      internalFrame(closable:true, iconifiable:true, maximizable:true, resizable:true,
        title: "internal frame2", visible:true, size:[200, 100])
      {
        label(text: "internal frame2")
      }
    }
  }
}
実行時画面

動作環境
groovy 2.2.2, JDK 1.7 update55

2014年7月3日木曜日

SwingBuilderでテキストフィールドのデフォルトの選択色を設定する

SwingBuilderでテキストフィールドのデフォルトの選択色を設定するには、以下のUIManagerのキーを設定します。
  • TextField.selectionBackground : テキストフィールド選択時の背景色
  • TextField.selectionForeground : テキストフィールド選択時の文字色

サンプルコード
import java.awt.*
import javax.swing.*
import groovy.swing.*

// テキストフィールドのデフォルトの選択色を設定する
UIManager.put("TextField.selectionBackground", new Color(0xcc, 0xdd, 0xff))
UIManager.put("TextField.selectionForeground", new Color(0xff, 0xff, 0x88))

sb = new SwingBuilder()
sb.edt {
  frame(
    title: "example - selection colors for Textfield",
    show: true,
    resizable: true,
    pack: true,
    defaultCloseOperation: WindowConstants.EXIT_ON_CLOSE
  ){
    textField(id:"tf1", text:"example textfield1",
      constraints: BorderLayout.NORTH
    )
    textField(id:"tf2", text:"example textfield2",
      constraints: BorderLayout.CENTER
    )
    tf1.selectAll()
  }

}
実行時画面

動作環境
groovy 2.2.2, JDK 1.7 update55

2014年7月1日火曜日

SwingBuilderでテーブルヘッダーにラベルと画像を表示する

SwingBuilderでテーブルヘッダーにラベルと画像を表示するには、以下のコードのようにTableCellRendererインターフェイスを実装したクラスを、ヘッダーのsetDefaultRendererメソッドを使用して設定します。
サンプルコード
import java.awt.*
import javax.swing.*
import javax.swing.border.*
import javax.swing.table.*
import groovy.swing.*

class MyHeader
{
  def label
  def icon
  public MyHeader(label, icon)
  {
    this.label = label
    this.icon = icon
  }
  @Override
  public String toString()
  {
    return label
  }
}

class MyHeaderRenderer extends JLabel implements TableCellRenderer
{
  public MyHeaderRenderer()
  {
    setBorder(BorderFactory.createRaisedBevelBorder())
  }

  @Override
  public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
    boolean hasFocus, int row, int column)
  {
    setText(value.toString())
    if( value instanceof MyHeader ){
      setIcon(value.icon)
    }
    return this
  }
}


data = [
  [category:'Database', product:'PostgreSQL'],
  [category:'Database', product:'MySQL']
]

sb = new SwingBuilder()
sb.edt {
  frame(
    title: "example - labels and images for table headers",
    show: true,
    resizable: true,
    size: [300, 100],
    defaultCloseOperation: WindowConstants.EXIT_ON_CLOSE
  ){
    scrollPane(){
      table(id:"table"){
        tableModel(list: data){
          propertyColumn( header: new MyHeader("category", imageIcon(file:"e:/icons/home.png")),
            propertyName: "category", editable: false )
          propertyColumn( header: new MyHeader("product", imageIcon(file:"e:/icons/plane.png")),
            propertyName: "product", editable: false )
        }
      }
      table.tableHeader.setDefaultRenderer(new MyHeaderRenderer())
    }
  }

}
実行時画面

動作環境
groovy 2.2.2, JDK 1.7 update55

2014年6月29日日曜日

SwingBuilderでテーブルヘッダーのデフォルトの色を設定する

SwingBuilderでテーブルヘッダーのデフォルトの色を設定するには、以下のUIManagerのキーを設定します。
  • TableHeader.background : テーブルヘッダーの背景色
  • TableHeader.foreground : テーブルヘッダーの文字色

サンプルコード
import java.awt.*
import javax.swing.*
import groovy.swing.*

// テーブルのヘッダーのデフォルト色を設定
UIManager.put("TableHeader.background", new Color(0xcc, 0xdd, 0xff))
UIManager.put("TableHeader.foreground", new Color(0x77, 0x99, 0xdd))

data = [
  [category:'Database', product:'PostgreSQL'],
  [category:'Database', product:'MySQL']
]

sb = new SwingBuilder()
sb.edt {
  frame(
    title: "example - default color for table headers",
    show: true,
    resizable: true,
    size: [300, 100],
    defaultCloseOperation: WindowConstants.EXIT_ON_CLOSE
  ){
    scrollPane(){
      table(){
        tableModel(list: data){
          propertyColumn( header:"category", propertyName: "category", editable: false )
          propertyColumn( header:"product", propertyName: "product", editable: false )
        }
      }
    }
  }
}
実行時画面

動作環境
groovy 2.2.2, JDK 1.7 update55

○テーブルに関するその他のエントリ

2014年6月27日金曜日

SwingBuilderでコンボボックスに画像とラベルを表示する

SwingBuilderでコンボボックスに画像とラベルを表示するには、以下のコードのようにDefaultListCellRendererのgetListCellRendererComponentメソッドをオーバーライドします。
サンプルコード
import java.awt.*
import javax.swing.*
import groovy.swing.*

class MyItem
{
  def label
  def icon
  public MyItem(label, icon)
  {
    this.label = label
    this.icon = icon
  }
  @Override
  public String toString()
  {
    return label
  }
}

sb = new SwingBuilder()
sb.edt {
  frame(
    title: "example - labels and images in Combobox",
    show: true,
    resizable: true,
    size: [200, 200],
    defaultCloseOperation: WindowConstants.EXIT_ON_CLOSE
  ){
    def options = [
      new MyItem("home", imageIcon(file:"e:/icons/home.png")),
      new MyItem("plane", imageIcon(file:"e:/icons/plane.png")),
      new MyItem("mobile", imageIcon(file:"e:/icons/mobile2.png"))
    ]
    comboBox(id:"cb1", items: options,
      constraints: BorderLayout.NORTH,
      renderer: new DefaultListCellRenderer(){
        @Override
        public Component getListCellRendererComponent(
          JList list, Object value, int index,
          boolean isSelected, boolean cellHasFocus)
        {
          super.getListCellRendererComponent(list, value, index,
            isSelected, cellHasFocus
          )
          if( value instanceof MyItem ){
            setText(value.label)
            setIcon(value.icon)
          }
          return this
        }
      }
    )
    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.2.2, JDK 1.7 update55