2014年6月30日月曜日

JGraphXでマウスオーバー時の色を設定する

JGraphXでマウスオーバー時の色を設定するには、以下のコードのようにmxCellTrackerクラスを使用します。

サンプルコード
import java.awt.*
import javax.swing.*
import groovy.swing.*
import com.mxgraph.swing.*
import com.mxgraph.swing.handler.*
import com.mxgraph.swing.util.mxSwingConstants
import com.mxgraph.view.*

def graph = new mxGraph()
def mxgc = new mxGraphComponent(graph)
def mxct = new mxCellTracker(mxgc, new Color(0x77, 0x99, 0xdd))

sb = new SwingBuilder()
def frm = sb.frame(
  title: "JGraphX - hover color for vertices and edges",
  visible: true,
  size: [300, 200],
  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, 80, 30)
    def v2 = graph.insertVertex(parent, null, "処理2",
      170, 20, 80, 30)

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

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

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月28日土曜日

JGraphXで線を選択した時の色を設定する

JGraphXで線を選択した時の色を設定するにはmxSwingConstantsの以下の定数を使用します。
  • EDGE_SELECTION_COLOR:線を選択した時の色
  • EDGE_SELECTION_STROKE:線を選択した時のStroke
  • HANDLE_BORDERCOLOR:ハンドルの線の色
  • CONNECT_HANDLE_FILLCOLOR:コネクタの塗りつぶし色
サンプルコード
import java.awt.*
import javax.swing.*
import groovy.swing.*
import com.mxgraph.swing.*
import com.mxgraph.swing.util.mxSwingConstants
import com.mxgraph.view.*

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

mxSwingConstants.EDGE_SELECTION_COLOR = new Color(0xff, 0xff, 0xff)
mxSwingConstants.EDGE_SELECTION_STROKE = new BasicStroke()
mxSwingConstants.HANDLE_BORDERCOLOR = new Color(0xff, 0x00, 0x00)
mxSwingConstants.CONNECT_HANDLE_FILLCOLOR = new Color(0x80, 0x80, 0x80)

sb = new SwingBuilder()
def frm = sb.frame(
  title: "JGraphX - selection color for edges",
  visible: true,
  size: [300, 200],
  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, 80, 30)
    def v2 = graph.insertVertex(parent, null, "処理2",
      170, 20, 80, 30)

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

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

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

2014年6月26日木曜日

JGraphXでリサイズ時の色を設定する

JGraphXでリサイズ時の色を設定するにはmxSwingConstantsの以下の定数を使用します。
  • PREVIEW_BORDER:リサイズ時の線の色
サンプルコード
import java.awt.*
import javax.swing.*
import javax.swing.border.*
import groovy.swing.*
import com.mxgraph.swing.*
import com.mxgraph.swing.util.mxSwingConstants
import com.mxgraph.view.*

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

mxSwingConstants.PREVIEW_BORDER = new LineBorder(new Color(0xff, 0x80, 0xff))

sb = new SwingBuilder()
def frm = sb.frame(
  title: "JGraphX - line color for resizing",
  visible: true,
  size: [300, 200],
  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, 80, 30)
    def v2 = graph.insertVertex(parent, null, "処理2",
      170, 20, 80, 30)

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

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

2014年6月25日水曜日

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

SwingBuilderでコンボボックスに画像を表示するには、以下のコードのようにimageIconの配列を使用します。
サンプルコード
import java.awt.*
import javax.swing.*
import groovy.swing.*

sb = new SwingBuilder()
sb.edt {
  frame(
    title: "example - images in Combobox",
    show: true,
    resizable: true,
    size: [200, 200],
    defaultCloseOperation: WindowConstants.EXIT_ON_CLOSE
  ){
    def options = [
      imageIcon(file:"e:/icons/home.png"),
      imageIcon(file:"e:/icons/plane.png"),
      imageIcon(file:"e:/icons/mobile2.png")
    ]
    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.2.2, JDK 1.7 update55

2014年6月24日火曜日

JGraphXで図形選択時の色を設定する

JGraphXで図形選択時の色を設定するにはmxSwingConstantsの以下の定数を使用します。
  • HANDLE_BORDERCOLOR:ハンドルの線の色
  • HANDLE_FILLCOLOR:ハンドルの塗りつぶし色
  • VERTEX_SELECTION_COLOR:選択した図形の色
  • VERTEX_SELECTION_STROKE:選択した図形のStroke
サンプルコード
import java.awt.*
import javax.swing.*
import groovy.swing.*
import com.mxgraph.swing.*
import com.mxgraph.swing.util.mxSwingConstants
import com.mxgraph.view.*

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

mxSwingConstants.HANDLE_BORDERCOLOR = new Color(0x77, 0x99, 0xdd)
mxSwingConstants.HANDLE_FILLCOLOR = new Color(0xcc, 0xdd, 0xff)
mxSwingConstants.VERTEX_SELECTION_COLOR = new Color(0xff, 0xff, 0xff)
mxSwingConstants.VERTEX_SELECTION_STROKE = new BasicStroke()

sb = new SwingBuilder()
def frm = sb.frame(
  title: "JGraphX - selection color for vertices",
  visible: true,
  size: [300, 200],
  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, 80, 30)
    def v2 = graph.insertVertex(parent, null, "処理2",
      170, 20, 80, 30)

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

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

2014年6月23日月曜日

SwingBuilderでコンボボックスのデフォルトの色を設定する

SwingBuilderでコンボボックスのデフォルトの色を設定するには、以下のUIManagerのキーを設定します。
  • ComboBox.background : コンボボックスの背景色
  • ComboBox.foreground : コンボボックスの文字色
  • ComboBox.selectionBackground : コンボボックスの選択した項目の背景色
  • ComboBox.selectionForeground : コンボボックスの選択した項目の文字色

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

// コンボボックスのデフォルトカラーを設定
UIManager.put("ComboBox.background", new Color(0xcc, 0xdd, 0xff))
UIManager.put("ComboBox.foreground", new Color(0x77, 0x99, 0xdd))
UIManager.put("ComboBox.selectionBackground", new Color(0xff, 0xff, 0xdd))
UIManager.put("ComboBox.selectionForeground", new Color(0x80, 0x80, 0x80))

sb = new SwingBuilder()
sb.edt {
  frame(
    title: "example - setting colors for Combobox",
    show: true,
    resizable: true,
    size: [200, 200],
    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.2.2, JDK 1.7 update55

2014年6月22日日曜日

JGraphXで図形を半透明で表示する

JGraphXで図形を半透明で表示するにはmxConstantsの以下の定数を使用します。
  • STYLE_STROKECOLOR:線の色
  • STYLE_FILLCOLOR:塗りつぶし色
  • STYLE_OPACITY:不透明度
  • STYLE_TEXT_OPACITY:テキストの不透明度
サンプルコード
import java.awt.*
import javax.swing.*
import groovy.swing.*
import com.mxgraph.swing.*
import com.mxgraph.view.*
import com.mxgraph.util.mxConstants
import com.mxgraph.layout.*

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

sb = new SwingBuilder()
def frm = sb.frame(
  title: "JGraphX - transparent vertices",
  visible: true,
  size: [300, 200],
  resizable: true,
  contentPane: mxgc,
  defaultCloseOperation: WindowConstants.EXIT_ON_CLOSE
){

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

    def style1 = [
      (mxConstants.STYLE_STROKECOLOR): "#ffffbb",
      (mxConstants.STYLE_FILLCOLOR): "#ffffbb",
      (mxConstants.STYLE_OPACITY): "50",
      (mxConstants.STYLE_TEXT_OPACITY): "50"
    ]
    stylesheet.putCellStyle("style1", style1)

    def v1 = graph.insertVertex(parent, null, "処理1",
      20, 20, 80, 30)
    def v2 = graph.insertVertex(parent, null, "処理2",
      70, 30, 80, 30, "style1")
  }
  finally
  {
    graph.model.endUpdate()
  }
}
実行結果

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

2014年6月21日土曜日

SwingBuilderでテーブルのセル以外の部分の背景色を設定する

SwingBuilderでテーブルのセル以外の部分の背景色を設定するには、以下のコードのようにscrollPaneのviewport.background/opaqueを使用します。

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

data = [
  [category:'Language', name:'Java'],
  [category:'Language', name:'Groovy']
]

sb = new SwingBuilder()
sb.edt {
  frame(
    title: "background color of viewport example",
    show: true,
    resizable: true,
    size: [300, 100],
    defaultCloseOperation: WindowConstants.EXIT_ON_CLOSE
  ){
    scrollPane(id:"sp"){
      table(id:"table"){
        tableModel(list: data){
          propertyColumn( header:"category", propertyName: "category", editable: false )
          propertyColumn( header:"name", propertyName: "name", editable: false )
        }
      }
    }
    sp.viewport.opaque = true
    sp.viewport.background = new Color(0xff, 0xff, 0xbb)
  }
}
実行時画面

動作環境
groovy 2.2.2, JDK 1.7 update55

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

2014年6月20日金曜日

JGraphXでズームイン・ズームアウトする

JGraphXでズームイン・ズームアウトするには、以下のコードのようにmxGraphComponentのzoomIn, zoomOut, zoomメソッドを使用します。

サンプルコード
import javax.swing.*
import groovy.swing.*
import com.mxgraph.swing.*
import com.mxgraph.view.*
import com.mxgraph.util.mxConstants
import com.mxgraph.layout.*

def graph = new mxGraph()
def mxgc = new mxGraphComponent(graph)
// ズームイン・ズームアウト設定
//mxgc.zoomOut() // ズームアウト
//mxgc.zoomIn()  // ズームイン
mxgc.zoom(1.5)  // 任意の倍率でズーム

sb = new SwingBuilder()
def frm = sb.frame(
  title: "JGraphX - zoom",
  visible: true,
  size: [300, 200],
  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, 80, 30)
    def v2 = graph.insertVertex(parent, null, "処理2",
      170, 20, 80, 30)

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

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

2014年6月19日木曜日

SwingBuilderでテーブルのヘッダー並び替えをできなくする

SwingBuilderでテーブルのヘッダー並び替えをできなくするには、以下のコードの様にtableHeaderのreorderingAllowedをfalseに設定します。

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

data = [
  [category:'Protocol', name:'http'],
  [category:'Protocol', name:'ftp']
]

sb = new SwingBuilder()
sb.edt {
  frame(
    title: "exaple - disable reordering of columns",
    show: true,
    resizable: true,
    size: [300, 100],
    defaultCloseOperation: WindowConstants.EXIT_ON_CLOSE
  ){
    scrollPane(){
      table(id:"table"){
        tableModel(list: data){
          propertyColumn( header:"category", propertyName: "category", editable: false )
          propertyColumn( header:"name", propertyName: "name", editable: false )
        }
      }
    }
    table.tableHeader.reorderingAllowed = false
  }
}
実行時画面

動作環境
groovy 2.2.2, JDK 1.7 update55

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