Module: SwingParadise

Included in:
BaseModule, QuitButtonExample, TextAreaExample
Defined in:
lib/swing_paradise/misc/misc.rb,
lib/swing_paradise/awt/color.rb,
lib/swing_paradise/version/version.rb,
lib/swing_paradise/prototype/prototype.rb,
lib/swing_paradise/toplevel_methods/misc.rb,
lib/swing_paradise/base_module/base_module.rb,
lib/swing_paradise/examples/015_menu_example.rb,
lib/swing_paradise/examples/020_table_example.rb,
lib/swing_paradise/examples/017_buttons_example.rb,
lib/swing_paradise/widget_collection/text_viewer.rb,
lib/swing_paradise/examples/002_text_area_example.rb,
lib/swing_paradise/examples/003_combo_box_example.rb,
lib/swing_paradise/examples/021_jsplitpane_example.rb,
lib/swing_paradise/examples/001_quit_button_example.rb,
lib/swing_paradise/examples/019_jeditorpane_example.rb,
lib/swing_paradise/examples/016_file_chooser_example.rb,
lib/swing_paradise/examples/018_colour_chooser_example.rb,
lib/swing_paradise/examples/012_quit_on_escape_key_being_pressed.rb

Overview

#

012_quit_on_escape_key_being_pressed.rb

#

Defined Under Namespace

Modules: AWT, BaseModule Classes: ButtonsExample, ColourChooserExample, ComboBoxExample, JSplitPaneExample, JeditorpaneExample, Prototype, QuitButtonExample, QuitOnEscapeKeyBeingPressed, TableExample, TestButton, TextAreaExample, TextViewer

Constant Summary collapse

VERSION =
#

VERSION

#
'0.1.48'
LAST_UDPATE =
#

LAST_UPDATE

#
'21.04.2024'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.button(i = '') ⇒ Object

#

SwingParadise.button

#


120
121
122
123
124
# File 'lib/swing_paradise/toplevel_methods/misc.rb', line 120

def self.button(i = '')
  require 'swing_paradise/java_classes/jbutton/jbutton.rb'
  _ = javax.swing.JButton.new(i)
  return _
end

.create_checkbox(i = '') ⇒ Object

#

SwingParadise.create_checkbox

#


958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
# File 'lib/swing_paradise/base_module/base_module.rb', line 958

def self.create_checkbox(
    i = ''
  )
  if i.is_a? Symbol
    case i
    # ======================================================================= #
    # === :is_checked
    # ======================================================================= #
    when :is_checked
      _ = JCheckBox.new
      _.setSelected(true)
    end
  else
    _ = JCheckBox.new(i)
  end
  return _
end

.create_grid(argument1 = 3, argument2 = 3) ⇒ Object

#

SwingParadise.create_grid (grid tag)

You are encouraged to use something like ‘4x4’ as input argument to this method, e. g. to create a grid with 4 cells and 4 rows, thus 16 elements in total.

#


983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
# File 'lib/swing_paradise/base_module/base_module.rb', line 983

def self.create_grid(
    argument1 = 3,
    argument2 = 3
  )
  if argument1.is_a?(String) and argument1.include?('x') # Support e. g. "2x2" as input.
    splitted = argument1.split('x')
    argument1 = splitted[0].to_i
    argument2 = splitted[1].to_i
  end
  if argument1.is_a?(String) and argument1.empty?
    argument1 = argument2.to_i
  end
  grid_layout = GridLayout.new(argument1, argument2)
  panel = ::SwingParadise.create_panel(grid_layout)
  return panel
end

.create_image(i) ⇒ Object

#

SwingParadise.create_image

We can not use ImageIcon directly because this can then not be .add()-ed to a box, so we need to use a label, and then use .setIcon() on it instead.

#


70
71
72
73
74
75
# File 'lib/swing_paradise/toplevel_methods/misc.rb', line 70

def self.create_image(i)
  label = JLabel.new
  image_icon = ImageIcon.new(i)
  label.setIcon(image_icon)
  return label
end

.create_text_viewObject

#

SwingParadise.create_text_view

#


52
53
54
# File 'lib/swing_paradise/toplevel_methods/misc.rb', line 52

def self.create_text_view
  return JTextArea.new
end

.entry(i = '', optional_n_entries = nil) ⇒ Object

#

SwingParadise.entry

Java Swing has two basic text components:

(1) JTextField, and
(2) JTextArea
#


191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/swing_paradise/toplevel_methods/misc.rb', line 191

def self.entry(
    i                  = '',
    optional_n_entries = nil
  )
  # require 'swing_paradise/java_classes/jtextarea/jtextarea.rb'
  require 'swing_paradise/java_classes/jtextfield/jtextfield.rb'
  if optional_n_entries
    _ = javax.swing.JTextField.new(i, optional_n_entries) # JTextArea.new(i)
  else
    _ = javax.swing.JTextField.new(i) # JTextArea.new(i)
  end
  return _
end

.frame(frame_title = '') ⇒ Object

#

SwingParadise.frame

#


29
30
31
# File 'lib/swing_paradise/misc/misc.rb', line 29

def self.frame(frame_title = '')
  JFrame.new(frame_title)
end

.hbox(*optional_widgets) ⇒ Object

#

SwingParadise.hbox

A horizontal box.

#


219
220
221
222
223
224
225
226
227
228
# File 'lib/swing_paradise/toplevel_methods/misc.rb', line 219

def self.hbox(*optional_widgets)
  optional_widgets.flatten!
  _ = javax.swing.Box.createHorizontalBox
  if optional_widgets and !optional_widgets.empty?
    optional_widgets.each {|this_widget|
      _.add(this_widget)
    }
  end
  return _
end

.is_left_mouse_button?(event) ⇒ Boolean

#

SwingParadise.is_left_mouse_button?

#

Returns:

  • (Boolean)


59
60
61
# File 'lib/swing_paradise/toplevel_methods/misc.rb', line 59

def self.is_left_mouse_button?(event)
  javax.swing.SwingUtilities.isLeftMouseButton(event)
end

.jpanelObject

#

SwingParadise.jpanel

#


36
37
38
# File 'lib/swing_paradise/misc/misc.rb', line 36

def self.jpanel
  JPanel.new
end

.jruby_font(i = 'Sans serif 28', type_variant = Font::PLAIN, font_size = 20) ⇒ Object

#

SwingParadise.jruby_font

This method should be equivalent to:

Font.new('Sans serif', Font::PLAIN, 28)

It must also be able to deal with Symbols, such as :hack_25.

Usage example:

LARGE_FONT = SwingParadise.jruby_font('Sans serif 28')
#


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/swing_paradise/toplevel_methods/misc.rb', line 91

def self.jruby_font(
    i            = 'Sans serif 28',
    type_variant = Font::PLAIN, # This is the default font type to be used.
    font_size    = 20
  )
  name_of_the_font = 'Sans serif'
  if i.is_a? Symbol # Handle Symbols first.
    splitted = i.to_s.split('_') # :hack_20 would now be ['hack','20'].
    font_size = splitted.last.to_i # ['hack','20']
    name_of_the_font = splitted.first.capitalize
    i = nil # Short-circuit it here.
  end
  if i and i.respond_to?(:include?) and i.include?(' ')
    splitted = i.split(' ') # i = 'Monospace 25'; splitted = i.split(' ')
    font_size = splitted.last.to_i
    name_of_the_font = splitted[0 .. -2].join(' ')
  else
    name_of_the_font = i
  end
  if i.is_a? Java::JavaAwt::Font
    return i
  end
  return Font.new(name_of_the_font, type_variant, font_size)
end

.jscroll_pane(child_widget, vertical_scrollbar_policy = :vertical_always, horizontal_scrollbar_policy = :default) ⇒ Object

#

SwingParadise.jscroll_pane

A JScrollPane provides a scrollable view of another widget. The JScrollPane manages a viewport, optional vertical and horizontal scroll bars, and optional row and column heading viewports.

#


146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/swing_paradise/toplevel_methods/misc.rb', line 146

def self.jscroll_pane(
    child_widget,
    vertical_scrollbar_policy   = :vertical_always,
    horizontal_scrollbar_policy = :default
  )
  case vertical_scrollbar_policy
  # ======================================================================= #
  # === :vertical_always
  # ======================================================================= #
  when :vertical_always
    vertical_scrollbar_policy = Java::JavaxSwing::ScrollPaneConstants::VERTICAL_SCROLLBAR_ALWAYS
  end
  case horizontal_scrollbar_policy
  # ======================================================================= #
  # === :default
  # ======================================================================= #
  when :default
    horizontal_scrollbar_policy = Java::JavaxSwing::ScrollPaneConstants::HORIZONTAL_SCROLLBAR_ALWAYS
  # ======================================================================= #
  # === :horizontal_never
  # ======================================================================= #
  when :horizontal_never
    horizontal_scrollbar_policy = Java::JavaxSwing::ScrollPaneConstants::HORIZONTAL_SCROLLBAR_NEVER
  end
  JScrollPane.new(
    child_widget,
    vertical_scrollbar_policy,
    horizontal_scrollbar_policy
  )
end

.remove_html(i) ⇒ Object

#

SwingParadise.remove_html

Usage example:

SwingParadise.remove_html('<one>two</three>')
#


238
239
240
241
242
243
# File 'lib/swing_paradise/toplevel_methods/misc.rb', line 238

def self.remove_html(i)
  i.gsub(
    %r{<[^>]+>},
    ''
  )
end

.set_global_font(font1 = Font.new('Hack', Font::PLAIN, 50)) ⇒ Object

#

SwingParadise.set_global_font

#


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/swing_paradise/toplevel_methods/misc.rb', line 13

def self.set_global_font(
    font1 = Font.new('Hack', Font::PLAIN, 50)
  )
  UIManager.put('Button.font', font1)
  UIManager.put('ToggleButton.font', font1)
  UIManager.put('RadioButton.font', font1)
  UIManager.put('CheckBox.font', font1)
  UIManager.put('ColorChooser.font', font1)
  UIManager.put('ComboBox.font', font1)
  UIManager.put('Label.font', font1)
  UIManager.put('List.font', font1)
  UIManager.put('MenuBar.font', font1)
  UIManager.put('MenuItem.font', font1)
  UIManager.put('RadioButtonMenuItem.font', font1)
  UIManager.put('CheckBoxMenuItem.font', font1)
  UIManager.put('Menu.font', font1)
  UIManager.put('PopupMenu.font', font1)
  UIManager.put('OptionPane.font', font1)
  UIManager.put('Panel.font', font1)
  UIManager.put('ProgressBar.font', font1)
  UIManager.put('ScrollPane.font', font1)
  UIManager.put('Viewport.font', font1)
  UIManager.put('TabbedPane.font', font1)
  UIManager.put('Table.font', font1)
  UIManager.put('TableHeader.font', font1)
  UIManager.put('TextField.font', font1)
  UIManager.put('PasswordField.font', font1)
  UIManager.put('TextArea.font', font1)
  UIManager.put('TextPane.font', font1)
  UIManager.put('EditorPane.font', font1)
  UIManager.put('TitledBorder.font', font1)
  UIManager.put('ToolBar.font', font1)
  UIManager.put('ToolTip.font', font1)
  UIManager.put('Tree.font', font1)
end

.text(i = '') ⇒ Object

#

SwingParadise.text

This method will return a JLabel instance.

#


250
251
252
253
# File 'lib/swing_paradise/toplevel_methods/misc.rb', line 250

def self.text(i = '')
  _ = javax.swing.JLabel.new(i)
  return _
end

.vboxObject

#

SwingParadise.vbox

A vertical box.

#


210
211
212
# File 'lib/swing_paradise/toplevel_methods/misc.rb', line 210

def self.vbox
  javax.swing.Box.createVerticalBox
end

.window(width = 640, height = 480) ⇒ Object

#

SwingParadise.window

This is ultimately a wrapper over JFrame.

#


14
15
16
17
18
19
20
21
22
23
24
# File 'lib/swing_paradise/misc/misc.rb', line 14

def self.window(
    width  = 640,
    height = 480
  )
  require 'swing_paradise/java_classes/jframe/jframe.rb'
  widget = Java::JavaxSwing::JFrame.new
  if width and height
    widget.width_height(width, height)
  end
  return widget
end

Instance Method Details

#jcombobox(i = nil) ⇒ Object

#

jcombobox

#


267
268
269
270
271
272
273
274
275
# File 'lib/swing_paradise/toplevel_methods/misc.rb', line 267

def jcombobox(i = nil)
  combo_box = JComboBox.new
  if i and i.respond_to?(:each)
    i.each {|this_exam_topic|
      combo_box.addItem(this_exam_topic)
    }
  end
  return combo_box
end

#jframe(frame_title = '') ⇒ Object Also known as: frame

#

jframe

#


43
44
45
# File 'lib/swing_paradise/misc/misc.rb', line 43

def jframe(frame_title = '')
  ::SwingParadise.frame(frame_title)
end

#jtextview(i = '') ⇒ Object Also known as: text_view, textview

#

jtextview

JTextArea is the one for multi-line input.

#


52
53
54
# File 'lib/swing_paradise/misc/misc.rb', line 52

def jtextview(i = '')
  JTextArea.new(i)
end

#quit_buttonObject

#

quit_button

This method can be used to quickly create a quit-button.

#


131
132
133
134
135
136
137
# File 'lib/swing_paradise/toplevel_methods/misc.rb', line 131

def quit_button
  _ = SwingParadise.button('Quit')
  _.on_event {|event|
    System.exit(0)
  }
  return _
end

#show_message_dialog(text = '') ⇒ Object

#

show_message_dialog

#


287
288
289
290
291
# File 'lib/swing_paradise/toplevel_methods/misc.rb', line 287

def show_message_dialog(
    text = ''
  )
  javax.swing.JOptionPane.showMessageDialog(nil, text)
end

#swing_dimension(new_width, new_height) ⇒ Object

#

swing_dimension

This is a slight wrapper over java.awt.Dimension.

#


260
261
262
# File 'lib/swing_paradise/toplevel_methods/misc.rb', line 260

def swing_dimension(new_width, new_height)
  java.awt.Dimension.new(new_width, new_height)
end

#text(i = '') ⇒ Object Also known as: jtext

#

text

#


280
281
282
# File 'lib/swing_paradise/toplevel_methods/misc.rb', line 280

def text(i = '')
  return JLabel.new(i)
end