サンプルコード
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
0 件のコメント:
コメントを投稿