Skip to content

Commit

Permalink
combobox added. also added another example with a combobox.
Browse files Browse the repository at this point in the history
  • Loading branch information
bakkdoor committed Nov 4, 2008
1 parent 395d506 commit c2d1de1
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 0 deletions.
53 changes: 53 additions & 0 deletions examples/combobox_example.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
module ComboboxExample
require "rubygems"
require "rswing"

include RSwing::Components

GridBagLayout = java.awt.GridBagLayout

frame = Frame.new "ComboBox example",
:size => [400,200],
:location => :center do |frame|

frame.default_close_operation = :exit_on_close

Panel.new :name => :panel,
:belongs_to => frame,
:layout_manager => GridBagLayout.new do |panel|

combobox_items = ["Value 1", "Value 2", "Value 3"]
ComboBox.new combobox_items,
:belongs_to => panel,
:name => :combobox

Button.new "OK",
:belongs_to => panel,
:name => :ok_button

Button.new "Cancel",
:belongs_to => panel,
:name => :cancel_button

end

frame.visible = true

end

# event handlers
panel = frame[:panel]

panel[:ok_button].on_click do
selected_item = panel[:combobox].selected_item
Dialog.show "Value selected: #{selected_item}",
:dialog_type => :info,
:title => "You have selected an item.",
:belongs_to => frame
end

panel[:cancel_button].on_click do
frame.dispose
end

end
1 change: 1 addition & 0 deletions lib/rswing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class Module
require "window"

require "button"
require "combo_box"
require "dialog"
require "frame"
require "listener"
Expand Down
67 changes: 67 additions & 0 deletions lib/rswing/components/combo_box.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
###############################################################################
# This file is part of RSwing. #
# RSwing - Swing wrapper for JRuby #
# (C) 2008 Christopher Bertels (bakkdoor@flasht.de) #
# #
# RSwing is free software: you can redistribute it and/or modify #
# it under the terms of the GNU Lesser General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# RSwing is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU Lesser General Public License for more details. #
# #
# You should have received a copy of the GNU Lesser General Public License #
# along with RSwing. If not, see <http://www.gnu.org/licenses/>. #
###############################################################################

module RSwing
module Components
JComboBox = javax.swing.JComboBox

class ComboBox < JComboBox
include Component

# Valid items are either an array or a ComboBoxModel object.
# e.g.: <tt>["First item", "Second item"]</tt> (default: nil)
#
# Valid options are:
# 1. <tt>:belongs_to => container</tt> (default: nil)
# 2. <tt>:name => :text_field</tt> (default: nil)
def initialize(items = nil, options = {})
if(items.kind_of? javax.swing.ComboBoxModel)
super(items)
elsif(items.kind_of? Array)
super(items.to_java(:Object))
else
super()
end

unless options.empty?
Container.add_if_requested(self, options)
end
end

# Returns an item at a given index.
# Equivalent to ComboBox#item_at
def [](item_index)
self.item_at(item_index)
end

# Returns the items of this ComboBox as an Array.
def items
@items = []

self.item_count.times do |i|
@items << self[i]
end

@items
end

end

end
end
2 changes: 2 additions & 0 deletions lib/rswing/components/text_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class TextField < JTextField
# 4. <tt>:font => nil</tt> (default nil)
# 5. <tt>:visible => false</tt> (default: true)
# 6. <tt>:editable => false</tt> (default: true)
# 7. <tt>:belongs_to => container</tt> (default: nil)
# 8. <tt>:name => :text_field</tt> (default: nil)
def initialize(options = {})
if(options.empty?)
super()
Expand Down

0 comments on commit c2d1de1

Please sign in to comment.