JRBuilder: Widgets Demo 1
Home «  Prev    Contents    Next  » Download
JRBuilder: Widgets Demo 1
Various widgets.
Commentary TBD            
require 'jrbuilder'

ctx = JRBuilder.new_swing_context

ctx.enter {
  attr_reader :my_frame
 
  bgcolor = color(240,238,220)
  # unfortunately, symbol/constant substitution can't be
  # used in all cases, so...
  Font = java.awt.Font
  Color = java.awt.Color
  JOptionPane = javax.swing.JOptionPane

  popup = nil # custom dialog (see end of file)
  bg1 = nil # radio button group
  bg2 = nil # radio button group
  text = nil # text area

  @my_frame = frame('Widgets Demo 1') { size 500, 500
    content_pane { background  bgcolor; layout :box, _parent, :Y_AXIS
      border :bevel, :RAISED }
    on_window_closing { my_frame.dispose }
    menu_bar { background bgcolor
      menu("File") {  mnemonic :VK_F; background :WHITE
        menu_item("Exit") { mnemonic :VK_X; background :WHITE
          on_click { my_frame.dispose }
        }
    } }
    panel { background bgcolor; layout :box, _parent, :Y_AXIS; align :LEFT
      border :compound, border(:titled, 'Widgets') {
        border :etched,:LOWERED }, border(:empty,0,4,4,4)
      x_box { align :LEFT
        button('Custom Dialog') { align :LEFT; on_click { _center(popup,my_frame).show }}
        x_strut 8
        button('Message Dialog') { align :LEFT
          on_click {JOptionPane.showMessageDialog(
            my_frame,'Thanks for visiting!','SwingBuilder Sez',JOptionPane::PLAIN_MESSAGE)}
        }
        x_strut 8
        button('Confirm Dialog') { align :LEFT
          on_click {JOptionPane.showConfirmDialog(my_frame, 'Are we having fun yet?')}
        }
        x_glue
      }
      y_strut 5
      x_box { align :LEFT
        y_box { align :LEFT,:TOP
          panel { border(:titled,'Text Style') { border :etched, :LOWERED }
            layout :box, _parent, :Y_AXIS ; background bgcolor; align :LEFT,:TOP        
            check_box('Bold') { background bgcolor; align :LEFT
              on_click { |event,box|
                if box.selected
                  text.font = font(text.font.name,text.font.style|Font::BOLD,text.font.size)
                else
                  text.font = font(text.font.name,text.font.style^Font::BOLD,text.font.size)
                end
              }
            }
            check_box('Italic') { background bgcolor; align :LEFT
              on_click { |event,box|
                if box.selected
                  text.font = font(text.font.name,text.font.style|Font::ITALIC,text.font.size)
                else
                  text.font = font(text.font.name,text.font.style^Font::ITALIC,text.font.size)
                end
              }
            }
            check_box('Mono-spaced') { background bgcolor; align :LEFT
              on_click { |event,box|
                if box.selected
                  text.font = font('Monospaced',text.font.style,text.font.size)            
                else
                  text.font = font('Dialog',text.font.style,text.font.size)            
                end
              }
            }
            x_spacer(120) { align :LEFT }
          }
          panel { background bgcolor; layout :box, _parent, :Y_AXIS; align :LEFT,:TOP
            border(:titled,'Color') { border :etched, :LOWERED }
            r1 = radio_button('Black',true) { background bgcolor; align :LEFT
              on_click { text.foreground = Color::BLACK }
            }
            r2 = radio_button('Blue') { background bgcolor; align :LEFT
              on_click { text.foreground = Color::BLUE }
            }
            r3 = radio_button('Red') { background bgcolor; align :LEFT
              on_click { text.foreground = Color::RED }
            }
            bg1 = button_group { add r1; add r2; add r3 }
            x_spacer(120) { align :LEFT }
          }
          panel { background bgcolor; layout :box, _parent, :Y_AXIS; align :LEFT,:TOP
            border(:titled,'Size') { border :etched, :LOWERED }
            s1 = radio_button('Large') { background bgcolor; align :LEFT
              on_click { text.font = font(text.font.name,text.font.style, 20) }
            }
            s2 = radio_button('Medium',true) { background bgcolor; align :LEFT
              on_click { text.font = font(text.font.name,text.font.style, 12) }
            }
            s3 = radio_button('Small') { background bgcolor; align :LEFT
              on_click { text.font = font(text.font.name,text.font.style, 8) }
            }
            bg2 = button_group { add s1; add s2; add s3 }
            x_spacer(120) { align :LEFT }
          }
        }
        x_glue
        panel { background bgcolor; layout :box, _parent, :Y_AXIS; align :LEFT,:TOP
          scroll_pane( 
            text = text_area(20,25){border :bevel, :LOWERED; text 'Welcome to SwingBuilder!'
              align :LEFT,:TOP
            }) {
            align :LEFT,:TOP
          }
          y_glue
        }
      }    
      y_glue
    }
  }
  popup = dialog(my_frame,"Hello",true) { size 240, 180
    content_pane { layout :box,_parent,:Y_AXIS; background  bgcolor; align :CENTER }
    y_glue
    label('Howdy Doody') { align :CENTER }
    y_spacer(15)
    button("Click to close") { on_click { popup.visible = false }; align :CENTER }
    y_glue
  }
}

ctx.my_frame.show
Home «  Prev    Contents    Next  » Download