Jump to: navigation, search

Difference between revisions of "JRuby/SWTSnippets/Snippet6.rb"

(New page: <pre> ################################################################################ # Copyright (c) 2009 IBM Corporation and others. # All rights reserved. This program and the accompan...)
 
(No difference)

Latest revision as of 07:21, 28 July 2009

################################################################################
# Copyright (c) 2009 IBM Corporation and others.
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0
# which accompanies this distribution, and is available at
# http://www.eclipse.org/legal/epl-v10.html
#
# Contributors:
#     IBM Corporation - initial API and implementation
################################################################################

require 'java'

# 
# GridLayout example snippet: insert widgets into a grid layout
#
# For a list of SWT example snippets ported to JRuby, see
# http://wiki.eclipse.org/JRuby/SWTSnippets
#
# @since 3.1
#
class Snippet6
	include_package 'org.eclipse.swt'
	include_package 'org.eclipse.swt.layout'
	include_package 'org.eclipse.swt.widgets'

	def self.main
		display = Display.new
		shell = Shell.new(display)
		shell.setLayout(GridLayout.new)
		c = Composite.new(shell, SWT::NONE)
		layout = GridLayout.new
		layout.numColumns = 3
		c.setLayout(layout)
		10.times do |i|
			b = Button.new(c, SWT::PUSH)
			b.setText("Button #{i}")
		end
		
		b = Button.new(shell, SWT::PUSH)
		b.setText("add a new button at row 2 column 1")
		index = 0
		b.addListener(SWT::Selection) do
			s = Button.new(c, SWT::PUSH)
			s.setText("Special #{index}")
			index += 1
			children = c.getChildren
			s.moveAbove(children[3])
			shell.layout([s].to_java Control)
		end
		
		shell.open
		display.sleep unless display.readAndDispatch until shell.disposed
		display.dispose
	end
end

Snippet6.main

Compare with the Java version.