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月6日日曜日

JGraphXで全ての図形・線をプログラムで選択する

JGraphXで全ての図形・線をプログラムで選択するには、以下のコードのようにmxGraphのselectAllメソッドを使用します。
サンプルコード
import java.awt.*
import javax.swing.*
import groovy.swing.*
import com.mxgraph.swing.*
import com.mxgraph.view.*
import com.mxgraph.util.mxConstants

def graph = new mxGraph()
def mxgc = new mxGraphComponent(graph)

sb = new SwingBuilder()
def frm = sb.frame(
  title: "JGraphX - selecting all vertices and edges",
  visible: true,
  size: [400, 150],
  resizable: true,
  contentPane: mxgc,
  defaultCloseOperation: WindowConstants.EXIT_ON_CLOSE
){

  parent = graph.getDefaultParent()
  graph.model.beginUpdate()
  try
  {

    def v1 = graph.insertVertex(parent, null, "処理1",
      20, 20, 50, 30, "style1")
    def v2 = graph.insertVertex(parent, null, "処理2",
      170, 20, 50, 30, "style2")
    def v3 = graph.insertVertex(parent, null, "処理3",
      320, 20, 50, 30, "style3")

    graph.insertEdge(parent, null, "正常終了", v1, v2)
    graph.insertEdge(parent, null, "正常終了", v2, v3)

    // 図形と線をすべて選択
    graph.selectAll()
  }
  finally
  {
    graph.model.endUpdate()
  }
}
実行結果

関連情報
JGraphxのダウンロードページ

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月4日金曜日

JGraphXで図形をプログラムで選択する

JGraphXで図形をプログラムで選択するには、以下のコードのようにGraphSelectionModelのaddCellを使用します。
サンプルコード
import javax.swing.*
import groovy.swing.*
import com.mxgraph.swing.*
import com.mxgraph.view.*
import com.mxgraph.util.mxConstants

def graph = new mxGraph()
def mxgc = new mxGraphComponent(graph)

sb = new SwingBuilder()
def frm = sb.frame(
  title: "JGraphX - selecting a vertex",
  visible: true,
  size: [400, 150],
  resizable: true,
  contentPane: mxgc,
  defaultCloseOperation: WindowConstants.EXIT_ON_CLOSE
){

  parent = graph.getDefaultParent()
  graph.model.beginUpdate()
  try
  {

    def v1 = graph.insertVertex(parent, null, "処理1",
      20, 20, 50, 30, "style1")
    def v2 = graph.insertVertex(parent, null, "処理2",
      170, 20, 50, 30, "style2")
    def v3 = graph.insertVertex(parent, null, "処理3",
      320, 20, 50, 30, "style3")

    graph.insertEdge(parent, null, "正常終了", v1, v2)
    graph.insertEdge(parent, null, "正常終了", v2, v3)

    def sm = graph.getSelectionModel()
    sm.clear()
    sm.addCell(v2)

  }
  finally
  {
    graph.model.endUpdate()
  }
}
実行結果

関連情報
JGraphxのダウンロードページ

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月2日水曜日

JGraphXでテキストを太字・イタリックで表示する

JGraphXでテキストを太字・イタリックで表示するにはmxConstantsの以下の定数を使用します。
  • STYLE_FONTSTYLE:テキストのスタイル
  • FONT_BOLD:太字
  • FONT_ITALIC:イタリック
サンプルコード
import javax.swing.*
import groovy.swing.*
import com.mxgraph.swing.*
import com.mxgraph.view.*
import com.mxgraph.util.mxConstants

def graph = new mxGraph()
def mxgc = new mxGraphComponent(graph)

sb = new SwingBuilder()
def frm = sb.frame(
  title: "JGraphX - bold and italic",
  visible: true,
  size: [400, 150],
  resizable: true,
  contentPane: mxgc,
  defaultCloseOperation: WindowConstants.EXIT_ON_CLOSE
){

  parent = graph.getDefaultParent()
  graph.model.beginUpdate()
  try
  {
    def stylesheet = graph.getStylesheet()

    def style1 = [
      (mxConstants.STYLE_FONTSTYLE): mxConstants.FONT_BOLD
    ]
    stylesheet.putCellStyle("style1", style1)
    def style2 = [
      (mxConstants.STYLE_FONTSTYLE): mxConstants.FONT_ITALIC
    ]
    stylesheet.putCellStyle("style2", style2)

    def v1 = graph.insertVertex(parent, null, "処理1",
      20, 20, 50, 30, "style1")
    def v2 = graph.insertVertex(parent, null, "Process2",
      170, 20, 50, 30, "style2")
    def v3 = graph.insertVertex(parent, null, "処理3",
      320, 20, 50, 30, "style3")

    graph.insertEdge(parent, null, "正常終了", v1, v2)
    graph.insertEdge(parent, null, "正常終了", v2, v3)
  }
  finally
  {
    graph.model.endUpdate()
  }
}
実行結果

関連情報
JGraphxのダウンロードページ

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