Home > Archive > PerlTk > November 2006 > JComboBox Beginner's question
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
JComboBox Beginner's question
|
|
|
| I really have looked at the Perldoc and done searches on google and
this group.
I have this really BASIC question about using JComboBox.
<code>
use Tk;
use Tk::JComboBox;
my $mw = MainWindow->new;
my $jcb = $mw->JComboBox(
-choices => [qw/Black Blue Green Purple Red Yellow/],
-textvariable => \$variable,
)->pack;
$sel = $jcb ->getSelectedIndex($variable);
MainLoop;
</code>
How do I get the choice so that I can use it later say in a simple
report to the shell
such as:
print "$sel\n";
Sorry this is such a basic question. All the fine docs on JComboBox
seem to be about how to set it up. Things that let me get a value from
Listbox do not work for me with JComboBox.
| |
| Rob Seegel 2006-10-05, 9:58 pm |
| Did you look at the brief tutorial that's comes with JComboBox? It's
also available in the following locations:
http://www.perltk.org/index.php?opt...id=55&Itemid=40
or:
http://search.cpan.org/~rcseege/Tk-...ox/tutorial.pod
Here's an example that shows some of the variety of ways in which you
can get information from a JComboBox:
use Tk;
use Tk::JComboBox;
my $mw = MainWindow->new;
my $variable;
my $done;
my $jcb = $mw->JComboBox(
-choices => [qw/Black Blue Green Purple Red Yellow/],
-textvariable => \$variable,
-selectcommand => \&SelectCommand
)->pack;
my $button = $mw->Button(
-text => "Submit",
-command => \&Submit
)->pack;
$mw->waitVariable(\$done);
print "change choices\n";
$button->configure(-bg => 'blue', -fg => 'white');
$mw->update;
$jcb->configure(-choices => [
{ -name => 'Black', -value => '#000000' },
{ -name => 'Blue', -value => '#0000ff' },
{ -name => 'Green', -value => '#008000' },
{ -name => 'Purple', -value => '#8000ff' },
{ -name => 'Red', -value => '#ff0000' },
{ -name => 'Yellow', -value => '#ffff00' }
]);
$mw->waitVariable(\$done);
print "Bye!\n";
sub Submit {
print "\nSubmit Called\n";
my $index = $jcb->getSelectedIndex();
print "Index: $index \n";
print "value: " . $jcb->getSelectedValue() . "\n";
print "name: " . $jcb->getItemNameAt($index) . "\n";
print "textvariable: $variable \n";
$done = "done";
}
sub SelectCommand {
my ($jcb, $selIndex, $selValue, $selName) = @_;
print "\nSelectCommand called\n";
print "Index: $selIndex\n";
print "Value: $selValue\n";
print "Name: $selName\n";
}
bdz wrote:
> I really have looked at the Perldoc and done searches on google and
> this group.
>
> I have this really BASIC question about using JComboBox.
>
> <code>
>
> use Tk;
> use Tk::JComboBox;
>
> my $mw = MainWindow->new;
> my $jcb = $mw->JComboBox(
> -choices => [qw/Black Blue Green Purple Red Yellow/],
> -textvariable => \$variable,
> )->pack;
> $sel = $jcb ->getSelectedIndex($variable);
> MainLoop;
>
> </code>
>
> How do I get the choice so that I can use it later say in a simple
> report to the shell
> such as:
> print "$sel\n";
>
> Sorry this is such a basic question. All the fine docs on JComboBox
> seem to be about how to set it up. Things that let me get a value from
> Listbox do not work for me with JComboBox.
>
| |
|
| Thank you very much, Rob, for the example and the links. My brain
really needs examples especially when learning new ways of doing
things.
In the JComboBox documentation you state that one needs to be careful
about binding other things to JComboBox.
Does this mean it cannot be set up to bind a <Return> key press once
the item is selected?
I have an application using listboxes which allows you to select the
item once you scroll to it by using a <Return> key press and then the
focus is passed to the next listbox automatically. This is for
selecting a series of attributes for a record where many of the
attributes are in a series of predicatable sets. However I liked the
idea of being able to have an editable additional attribute with
JComboBox.
| |
| Rob Seegel 2006-10-06, 6:59 pm |
| bdz wrote:
> Thank you very much, Rob, for the example and the links. My brain
> really needs examples especially when learning new ways of doing
> things.
You're welcome. Normally, I would gently, but firmly direct you to the
documentation, since the Perldoc for JComboBox covers this specific
point fairly carefully, *and* even features an example. However, in this
case, I will cut you some slack because the docs feature some incorrect
information on this point, so I feel a bit guilty ;-) It also brought a
minor bug to my attention related to focus highlight.
Instead of long-winding explanations, I'll give you two examples of how
I'd recommend you do it if you want to create a "key" binding, one for
each mode. In both cases the approach is identical.
Then I'll proceed to fix the docs... I would recommend you reread the
docs for the binding section, though, and replace "Box" references with
"Entry" -- that's the only real mistake. Everything else should still apply.
Example 1: readonly mode
use Tk;
use Tk::JComboBox;
my $mw = MainWindow->new;
my $done;
my $jcb = $mw->JComboBox(
-choices => [qw/Black Blue Green Purple Red Yellow/],
)->pack;
$jcb->Subwidget('Entry')->bind('<Return>', [\&Submit, $jcb]);
$jcb->focus;
$mw->waitVariable(\$done);
print "Bye\n";
sub Submit {
my ($subwidget, $jcb) = @_;
print "\nSubmit Called\n";
my $index = $jcb->getSelectedIndex();
print "Index: $index \n";
print "value: " . $jcb->getSelectedValue() . "\n";
print "name: " . $jcb->getItemNameAt($index) . "\n";
$done = "done";
}
Example 2: editable mode: Note that -1 is returned for the index if the
entry contains a value that is not in the List.
use Tk;
use Tk::JComboBox;
my $mw = MainWindow->new;
my $done;
my $jcb = $mw->JComboBox(
-mode => 'editable',
-choices => [
{ -name => 'Black', -value => '#000000' },
{ -name => 'Blue', -value => '#0000ff' },
{ -name => 'Green', -value => '#008000' },
{ -name => 'Purple', -value => '#8000ff' },
{ -name => 'Red', -value => '#ff0000' },
{ -name => 'Yellow', -value => '#ffff00' }]
)->pack;
$jcb->Subwidget('Entry')->bind('<Return>', [\&Submit, $jcb]);
$jcb->focus;
$mw->waitVariable(\$done);
print "Bye\n";
sub Submit {
my ($subwidget, $jcb) = @_;
print "\nSubmit Called\n";
my $index = $jcb->getSelectedIndex();
print "Index: $index \n";
print "value: " . $jcb->getSelectedValue() . "\n";
print "name: " . $jcb->getItemNameAt($index) . "\n";
$done = "done";
}
Hope this helps,
Rob
| |
| Rob Seegel 2006-10-12, 7:03 pm |
| bdz wrote:
> Thank you very much, Rob, for the example and the links. My brain
> really needs examples especially when learning new ways of doing
> things.
Thanks for the feedback. I'll put out another release tonight or
tomorrow with the new examples, plus a slightly revised section on event
handling in the pod, and other minor corrections.
I've also revised the code to stem the 8.4 memory leak with Listbox.
Rob
| |
| Slaven Rezic 2006-11-16, 6:59 pm |
| Rob Seegel <RobSeegel@comcast.net> writes:
> bdz wrote:
>
> Thanks for the feedback. I'll put out another release tonight or
> tomorrow with the new examples, plus a slightly revised section on event
> handling in the pod, and other minor corrections.
>
> I've also revised the code to stem the 8.4 memory leak with Listbox.
I think I found a fix for this memory leak --- please check out the
patch in http://rt.cpan.org/Ticket/Display.html?id=12466
Regards,
Slaven
--
Slaven Rezic - slaven <at> rezic <dot> de
BBBike - route planner for cyclists in Berlin
WWW version: http://www.bbbike.de
Perl/Tk version for Unix and Windows: http://bbbike.sourceforge.net
|
|
|
|
|