Jim Turner > Tk-JBrowseEntry-4.8 > Tk::JBrowseEntry



Annotate this POD


Open  0
View/Report Bugs
Module Version: 4.8   Source  


Tk::JBrowseEntry - Full-featured "Combo-box" (Text-entry combined with drop-down listbox) derived from Tk::BrowseEntry with many additional features and options.


        use Tk;
        use Tk::JBrowseEntry;

        my $mw = MainWindow->new;
        my $var;

        my $widget = $mw->JBrowseEntry(
                -label => 'Normal:',
                -variable => \$var,
                -state => 'normal',
                -choices => [qw(pigs cows foxes goats)],
                -width  => 12
                -side   => 'top',
                -pady => '10',
                -anchor => 'w');



Tk::JBrowseEntry is a derived widget from Tk::BrowseEntry, but adds numerous features and options. Among them are hash lists (one set of values is displayed for the user, but another is used as data), ability to disable either the text entry widget or the listbox, ability to allow user to delete items from the list, additional keyboard bindings, and much more.

JBrowseEntry widgets allow one to specify a full combo-box, a "readonly" box (text field allows user to type the 1st letter of an item to search for, but user may only ultimately select one of the items in the list), or a "textonly" version (drop-down list disabled), or a completely disabled widget.

This widget is similar to other combo-boxes, ie. JComboBox, but has good keyboard bindings and allows for quick lookup/search within the listbox. pressing <RETURN> in entry field displays the dropdown box with the first entry most closly matching whatever's in the entry field highlighted. Pressing <RETURN> or <SPACE> in the listbox selects the highlighted entry and copies it to the text field and removes the listbox. <ESC> removes the listbox from view. <UP> and <DOWN> arrows work the listbox as well as pressing a key, which will move the highlight to the next item starting with that letter/number, etc. <UP> and <DOWN> arrows pressed within the entry field circle through the various list options as well (unless "-state" is set to 'textonly'). Set "-state" to "text" to disable the dropdown list, but allow <UP> and <DOWN> to cycle among the choices. Setting "-state" to 'textonly' completely hides the choices list from the user - he must type in his choice just like a normal entry widget.

One may also specify whether or not the button which activates the dropdown list via the mouse can take focus or not (-btntakesfocus) or whether the widget itself can take focus or is skipped in the focusing order. The developer can also specify alternate bitmap images for the button (-arrowimage and -farrowimage). The developer can also specify the maximum length of the dropdown list such that if more than that number of items is added, a vertical scrollbar is automatically added (-height). A fixed width in characters (-width) can be specified, or the widget can be allowed to resize itself to the width of the longest string in the list. The listbox and text entry field are automatically kept to the same width.

One can optionally specify a label (-label), similar to the "LabEntry" widget. By default, the label appears packed to the left of the widget. The positioning can be specified via the "-labelPack" option. For example, to position the label above the widget, use "-labelPack => [-side => 'top']".


 It is easiest to illustrate this widget's capabilities via examples:
 use Tk;
 use Tk::JBrowseEntry;
 $MainWin = MainWindow->new;
 $dbname1 = 'cows';
 $dbname2 = 'foxes';
 $dbname3 = 'goats';
 $dbname5 = 'default';
 $jb1 = $MainWin->JBrowseEntry(
        -label => 'Normal:',
        -variable => \$dbname1,
        -state => 'normal',
        -choices => [qw(pigs cows foxes goats)],
        -width  => 12);
        -side   => 'top', -pady => '10', -anchor => 'w');
 $jb2 = $MainWin->JBrowseEntry(
        -label => 'TextOnly:',
        -variable => \$dbname2,
        -state => 'text',
        -choices => [qw(pigs cows foxes goats)],
        -width  => 12);
        -side   => 'top', -pady => '10', -anchor => 'w');
 $jb3 = $MainWin->JBrowseEntry(
        -label => 'ReadOnly:',
        -variable => \$dbname3,
        -choices => [qw(pigs cows foxes goats)],
        -state => 'readonly',
        -width  => 12);
        -side   => 'top', -pady => '10', -anchor => 'w');
 $jb4 = $MainWin->JBrowseEntry(
        -label => 'Disabled:',
        -variable => \$dbname3,
        -state => 'disabled',
        -choices => [qw(pigs cows foxes goats)],
        -width  => 12);
        -side   => 'top', -pady => '10', -anchor => 'w');
 $jb5 = $MainWin->JBrowseEntry(
        -label => 'Scrolled List:',
        -width => 12,
        -default => $dbname5,
        -height => 4,
        -variable => \$dbname5,
        -browsecmd => sub {print "-browsecmd!\n";},
        -listcmd => sub {print "-listcmd!\n";},
        -state => 'normal',
        -choices => [qw(pigs cows foxes goats horses sheep dogs cats ardvarks default)]);
        -side   => 'top', -pady => '10', -anchor => 'w');
 $jb6 = $MainWin->JBrowseEntry(
        -label => 'Button Focus:',
        -btntakesfocus => 1,
        -arrowimage => $MainWin->Getimage('balArrow'),   #SPECIFY A DIFFERENT BUTTON IMAGE.
        -farrowimage => $MainWin->Getimage('cbxarrow'),  #OPTIONAL 2ND IMAGE FOR BUTTON WHEN FOCUSED. 
        -width => 12,
        -height => 4,
        -variable => \$dbname6,
        -browsecmd => sub {print "-browsecmd!\n";},
        -listcmd => sub {print "-listcmd!\n";},
        -state => 'normal',
        -choices => [qw(pigs cows foxes goats horses sheep dogs cats ardvarks default)]);
        -side   => 'top', -pady => '10', -anchor => 'w');
 $jb7 = $MainWin->JBrowseEntry(
        -label => 'Skip Focus:',
        -takefocus => 0,
        -width => 12,
        -height => 4,
        -variable => \$dbname7,
        -browsecmd => sub {print "-browsecmd!\n";},
        -listcmd => sub {print "-listcmd!\n";},
        -state => 'normal',
        -choices => [qw(pigs cows foxes goats horses sheep dogs cats ardvarks default)]);
        -side   => 'top', -pady => '10', -anchor => 'w');
 $jb7->choices([qw(First Second Fifth Sixth)]);   #REPLACE LIST CHOICES!
 $jb7->insert(2, 'Third', 'Fourth');              #ADD MORE AFTER 1ST 2.
 $jb7->insert('end', [qw(Seventh Oops Eighth)]);  #ADD STILL MORE AT END.
 $jb7->delete(7);                                 #REMOVE ONE.
 $b = $MainWin->Button(-text => 'Quit', -command => sub {exit(); });
 $b->pack(-side => 'top');


Tk::JComboBox Tk::BrowseEntry Tk::Listbox Tk::Entry


-state => normal | readonly | text | textonly | disabled

Default: normal

JBrowseEntry supports 5 different states:

normal: Default operation -- Both text entry field and dropdown list button function normally.

readonly: Dropdown list functions normally. When text entry field has focus, user may type in a letter, and the dropdown list immediately drops down and the first/ next matching item becomes highlighted. The user must ultimately select from the list of valid entries and may not enter anything else.

text: Text entry functions normally, but dropdown list button is disabled. User must type in an entry or use the up and down arrows to choose from among the list items.

textonly: Similar to "text": Text entry functions normally, but dropdown list button is disabled. User must type in an entry. The list choices are completely hidden from the user.

disabled: Widget is completely disabled and greyed out. It will not activate or take focus.


Allows one to specify alternate binding schema for certain keys. Currently valid values are "Return=Next" (which causes pressing the [Return] key to advance the focus to the next widget in the main window); and "Down=Popup", which causes the [Down-arrow] key to pop up the selection listbox.


The dropdown list button is normally activated with the mouse and is skipped in the focusing circuit. If this option is set, then the button will take keyboard focus. Pressing <Return>, <Spacebar>, or <Downarrow> will cause the list to be dropped down, repeating causes the list to be removed again. Normally, the text entry widget receives the keyboard focus. This option can be used in combination with "-takefocus" so that either the text entry widget, the button, or both or neither receive keyboard focus. If both options are set, the entry field first receives focus, then pressing <Tab> causes the button to be focused.


If set, allows user to delete individual items in the drop-down list by pressing the <Delete> key to delete the current (active) item.


Allows one to specify a second alternate bitmap for the image on the button which activates the dropdown list when the button has the keyboard focus. The default is to use the "-arrowimage" image. This option should only be specified if the "-arrowimage" option is also specified. See the "-arrowimage" option under Standard BrowseEntry options for more details.


Specify the maximum number of items to be displayed in the listbox before a vertical scrollbar is automatically added. Default is infinity (listbox will not be given a scrollbar regardless of the number of items added).


Specify alternate packing options for the label. The default is: "[-side => 'left', -anchor => 'e']". The argument is an arrayref. Note: if no label is specified, none is packed or displayed.


Default "flat"

Allow relief of the label portion of the widget to be specified.


Specify an alternate font for the text in the listbox. Use "-font" to change the text of the text entry field. For best results, "-font" and "-listfont" should specify fonts of similar size.


Normally, when the widget has the focus, the current value is "selected" (highlighted and in the cut-buffer). Some consider this unattractive in appearance, particularly with the "readonly" state, which appears as a raised button in Unix, similar to an "Optionmenu". Setting this option will cause the text to not be selected.


The number of characters (average if proportional font used) wide to make the entry field. The dropdown list will be set the same width as the entry widget plus the width of the button. If not specified, the default is to calculate the width to the width of the longest item in the choices list and if items are later added or removed the width will be recalculated.


Default 0

Prevents dropdown list button from being displayed.



Specifies the image to be used in the arrow button beside the entry widget. The default is an downward arrow image in the file cbxarrow.xbm


Specifies a function to call when a selection is made in the popped up listbox. It is passed the widget and the text of the entry selected. This function is called after the entry variable has been assigned the value.


Specifies the list of choices to pop up. This is a reference to an array of strings specifying the choices.


Depreciated -- Appears to force the background of the entry widget on the Unix version to "grey95" if state is normal and a "-background" color is not specified.


Specifies the function to call when the button next to the entry is pressed to popup the choices in the listbox. This is called before popping up the listbox, so can be used to populate the entries in the listbox.


Specifies relief for the dropdown list (default is "sunken").


Specifies the width of the popup listbox.


Specifies the maximum width the entry and listbox widgets can expand to in characters. The default is zero, meaning expand to the width to accomodate the widest string in the list.


Specifies one of four states for the widget: "normal", "readonly", "textonly", or "disabled". If the widget is "disabled" then the value may not be changed and the arrow button won't activate. If the widget is "readonly", the entry may not be edited, but it may be changed by choosing a value from the popup listbox. "textonly" means the listbox will not activate. "normal" is the default.


If set to "1", pressing the "<Tab>" key will cause the string in the entry field to be "auto-completed" to the next matching item in the list. If there is no match, the typed text is not changed. If it already matches a list item, then the listbox is removed from view and keyboard focus transfers to the next widget. If set to "2" and there is no match in the list, then entry is set to the default value or empty string.


Specifies the variable in which the entered value is to be stored.



activate() invokes the activate() option on the listbox to make the item with the index specified by the first argument "active". Unless a second argument is passed containing a false value, the value of the "-textvariable" variable is also set to this now active value.


Sets the dropdown list listbox to the list of values referenced by listref, if specified. Returns the current list of choices in the listbox if no arguments provided.


Returns the currently-selected element in the listbox, if any, otherwise, undef.

$widget->delete(first [, last])

Deletes one or more elements of the listbox. First and last are indices specifying the first and last elements in the range to delete. If last isn't specified it defaults to first, i.e. a single element is deleted.


Deletes one or more elements of the listbox. "hashkey" specifies the element to be deleted by the value visible to the user.


Returns the actual option key value that corresponds to the choice value displayed in the listbox. (undef if there is none). (Opposite of dereference() and dereferenceOnly().


Returns the value (displayed in the listbox) that corresponds to the choice key specified by "hashkey". If the key is not one of the valid choices or the choices are a list instead of a hash, then "hashkey" is returned.


Returns 1 if the key specified by "hashkey" is one of the valid choices and the list of choices is a hash, otherwise undef is returned.


Returns a reference to the current hash of choices (keyed by the option visable to the user) if the choice list is a hash (reversed from the hash passed to choices()), otherwise, undef is returned.


Returns a reference to the current hash of choices (keyed by actual option value) if the choice list is a hash (same as the hash passed to choices()), otherwise, undef is returned.

$widget->get([first [, last])

get() with no arguments returns the current value of the "-textvariable" variable. If any arguments are passed, they are passed directly to the listbox->get() function, ie. "0", "end" to return all values of the listbox.


Returns the index number in the list (zero-based) that can be used by get() of the value specified by "hashkey".


Returns the value (displayed in the listbox) that corresponds to the choice key specified by "hashkey". If the key is not one of the valid choices or the choices are a list instead of a hash, then undef is returned.


Invokes and returns the result of the listbox->index() function.

$widget->insert(index, [item | list | listref | hashref])

Inserts one or more elements in the list just before the element given by index. If index is specified as "end" then the new elements are added to the end of the list. List can be a reference to a list (listref). If a hash reference is specified, then the values are displayed to the user in the dropdown list, but the values returned by the "-textvariable" variable or the get() function are the corresponding hash key(s).


Invokes and returns the result of the listbox size() function (the number of items in the list.

$widget->state([normal | readonly | text | textonly | disabled])

Get or set the state of the widget.


Jim Turner, <http://home.mesh.net/turnerjw/jim>


Copyright 2001-2011 (c) Jim Turner <http://home.mesh.net/turnerjw/jim>. All rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

This is a derived work from Tk::BrowseEntry. Tk::BrowseEntry is copyrighted by Rajappa Iyer

syntax highlighting: