GrandFather has asked for the wisdom of the Perl Monks concerning the following question:
As often happens when preparing a SoPW this question was answered while stripping the code down to the "bare essentials". However the issue was subtle enough that I decided to post it here anyway so others might benefit.
The sample code below creates a dialog box containing a JComboBox widget. The intent is to use it to select a person from a list who's details are to be edited in the dialog. I wanted to set the initialy selected entry to either a value passed into the edit code, or to the first entry. The code as given fails to do that, although it looks like it ought to.
Note that $id is set to the desired value when it is declared and that the contents of $id are used to set the selected item using $IDText->setSelected ($id);.
use strict; use warnings; use Tk; use Tk::DialogBox; use Tk::JComboBox; my $main = MainWindow->new (-title => "Test JComboBox"); EditPlayers ($main); sub EditPlayers { my ($main, $sel) = @_; my @ids = qw(Fred Joe Bob); my $dlg = $main->DialogBox (-title => "Member Information", -butto +ns => ["OK", "Cancel"]); my $id = $sel || (@ids && $ids[0]) || ''; my $IDText = $dlg->add ( 'JComboBox', -mode => 'readonly', -selectcommand => [\&changeEditPlayer, \$id], -validate => 'match', -textvariable => \$id, ); $IDText->form (-left => '%0', -right => '%100', -top => '%0'); $IDText->configure (-options => \@ids); $IDText->setSelected ($id); return if $dlg->Show (-global) ne 'OK'; } sub changeEditPlayer { my ($id, $widget, $index, $value, $name) = @_; return 1 if ! defined $name; print "$name\n"; }
However when the dialog opens no value is selected and the diagnostic print in the selectcommand handler doesn't fire.
It turns out that with $id bound to the widget (-textvariable => \$id,), when the list items are set ($IDText->configure (-options => \@ids);) $id is set to undef. The fix is to use the code that was used to initialise $id in place of $id where the selection is set: $IDText->setSelected ($sel || (@ids && $ids[0]) || '');.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How do I set the default item in a Tk::JComboBox
by zentara (Cardinal) on Jun 17, 2006 at 11:05 UTC | |
by GrandFather (Saint) on Jun 17, 2006 at 15:43 UTC |