JListbox - justify text within a Listbox


  use JListbox;
  $dw = $parent->JListbox(-justify=>'center', -popupmenu=>1);


JListbox is identical to a Listbox, but has two addtional options: -justify and -popupmenu.


Possible values for '-justify' are 'left', 'center' and 'right'. The default is 'left'. All text within the Listbox will be justified according to the option you set.

The widget automatically checks for variable or fixed width fonts and adjusts accordingly.

You cannot justify individual entries separately (as of version .02).

Your text will remain justified appropriately, even if you set the '-expand' and '-fill' options.

The justification ability is provided via plain old pixel and character counting (depending on whether you are using a variable or fixed width font). There have been no underlying changes in the C code to the Tcl Listbox.


If the -popupmenu option is used, a ``Cut, Copy, Paste'' menu will appear when the user right-clicks anywhere on the JListbox.

The ``Cut'' option will remove the item from the JListbox, copy it to the clipboard and the remaining items will shift up automatically.

The ``Copy'' option simply copies the selected value to the clipboard.

The ``Paste'' option, if selected, will bring up a Dialog window that gives the user the option to paste (insert) above or below the selected item, as well as on the same line, either to the left or right of the selected item.

One whitespace character automatically separates the pasted value from the original value if the 'left' or 'right' option is chosen.

If you wish to modify the popup menu itself, you can retrieve it using the Subwidget method with 'popupmenu' as the widget name.

$menu = $dw->Subwidget('popupmenu');


If using a variable width font, you may encounter a problem with the last character disappearing off the right side of the listbox when you use right justify. I think I fixed this, so let me know if you have any problems.

If the text you insert into the listbox includes characters that have special meaning in regular expressions (e.g. '*', '?'), you will need to escape them using the '\' character or your app may crash.

e.g. $dw->insert('end', ``What did you say\?'');


Fix the regular expression issue mentioned above.

Allow individual entries to be justified.

Add the 'addMenuItem' and 'deleteMenuItem' methods to allow greater configurability of the right-click menu.


Daniel J. Berger djberg96@hotmail.com

Thanks goes to Damion K. Wilson for his help with creating widgets.