Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Help with JComboBox

by JamesNC (Chaplain)
on Nov 17, 2004 at 04:12 UTC ( [id://408312]=note: print w/replies, xml ) Need Help??


in reply to Help with JComboBox

Ok, here is the solution. First, you are using the same textvariable reference and that is why the box pops up, use a second one and that issue goes away. The other problems are caused by the fact that you keep creating instances of dialog boxes and JCombo's. Create a single instance, and then use the Show() method in your CallDialog sub. Here is your version hacked up to work:
use strict; use warnings; use Tk; use Tk::JComboBox; use Tk::Dialog; my $form; my %choices = ( 'a'=>1, 'b'=>2, 'c'=>3 ); my $selected_1 = "a"; my $selected = "a"; my $dialog; my $testbox1; my $testbox2; CreateMainForm(); MainLoop(); sub CreateMainForm { $form = MainWindow->new(); $form->title("Test for JComboBox Problem"); $testbox1 = $form->JComboBox( -textvariable=>\$selected_1, -validate=>'match', -mode=>'editable', -relief=>'sunken', -highlightthickness=>0, ); for my $x(keys %choices) { $testbox1->addItem("$x"); } $testbox1->pack(-side=>'left'); my $button = $form->Button( -text=>"Dialog", -command=>sub{CallDialog($testbox1)} )->pack(-side=>'right'); $dialog = $form->Dialog ( -title=>"Part2 of test", -buttons=>["Submit","Cancel"],-default_button=>"Submit" ); $testbox2 = $dialog->JComboBox(-textvariable=>\$selected, -validate=>'match',-mode=>'editable', -relief=>'sunken',-highlightthickness=>0 ); for my $x(keys %choices) { $testbox2->addItem("$x"); } $testbox2->pack(-side=>'left'); } sub CallDialog { $testbox1->configure(-state=>'disabled'); my $retval = $dialog->Show( -popover=>$form, -overanchor=>'c', -popanchor=>'c'); if($retval eq "Cancel") { $testbox1->configure(-state=>'normal'); } elsif($retval eq "Submit") { $testbox1->configure(-state=>'normal'); $testbox1->setSelected($selected ); #(optional?) } }


Cheers,
JamesNC

Replies are listed 'Best First'.
Re^2: Help with JComboBox
by ChrisR (Hermit) on Nov 17, 2004 at 14:25 UTC
    Thanks. Your example does work, however, I need the combobox in the dialog to show the correct value. With this modification, I still have the same problem.
    sub CallDialog { $testbox1->configure(-state=>'disabled'); ############################### # Added this line so the displayed value is correct $selected = $selected_1; ############################### my $retval = $dialog->Show( -popover=>$form, -overanchor=>'c', -po +panchor=>'c'); if($retval eq "Cancel") { $testbox1->configure(-state=>'normal'); } elsif($retval eq "Submit") { $testbox1->configure(-state=>'normal'); $testbox1->setSelected($selected ); #(optional?) } }
    I also tried to set the variable in the callback for the button on the main window and got the same problem.
    my $button = $form->Button( -text=>"Dialog", -command=>sub{$selected= +$selected_1;CallDialog($testbox1);} )->pack(-side=>'right');
    I have also tried passing the variable $selected to the sub CallDialog in the callback. Same problem.

    What does work is to use the setSelected method as you mentioned below at the beginning of the sub CallDialog along with a seperate variable. This way the problem does not show up and the correct value is displayed to the user in the dialog. Also, I found that there is no need to disable the first combobox since it doesn't really disable the background activities of the combobox.
    use strict; use warnings; use Tk; use Tk::JComboBox; use Tk::Dialog; my $form; my %choices = ( 'a'=>1, 'b'=>2, 'c'=>3 ); my $selected_1 = "a"; my $selected = "a"; my $dialog; my $testbox1; my $testbox2; CreateMainForm(); MainLoop(); sub CreateMainForm { $form = MainWindow->new(); $form->title("Test for JComboBox Problem"); $testbox1 = $form->JComboBox( -textvariable=>\$selected_1,-valida +te=>'match', -mode=>'editable',-relief=>'sunken', -highlightthicknes +s=>0 ); for my $x(keys %choices) { $testbox1->addItem("$x"); } $testbox1->pack(-side=>'left'); my $button = $form->Button( -text=>"Dialog", -command=>sub{CallDi +alog();} )->pack(-side=>'right'); $dialog = $form->Dialog ( -title=>"Part2 of test", -buttons=>["Su +bmit","Cancel"],-default_button=>"Submit" ); $testbox2 = $dialog->JComboBox(-textvariable=>\$selected, -validat +e=>'match',-mode=>'editable', -relief=>'sunken',-highlightthickness=> +0); for my $x(keys %choices) { $testbox2->addItem("$x"); } $testbox2->pack(-side=>'left'); } sub CallDialog { $testbox2->setSelected($selected_1); my $retval = $dialog->Show( -popover=>$form, -overanchor=>'c', -po +panchor=>'c'); if($retval eq "Cancel") { $testbox1->configure(-state=>'normal'); } elsif($retval eq "Submit") { $testbox1->setSelected($selected ); #(optional?) } }
    Thanks again for your help. ++Too bad I can vote only once per node.
      I think you did it again... do this to set the dialog windows combo box to the main window combo box, should probably rename these to avoid the Who's on first, what's on second...
      ############################### # Added this line so the displayed value is correct #$selected = $selected_1; #<-- not this $testbox2->setSelected( $selected1 ); #<-- but this ###############################
        Hello, I am using the JCombobox module. All of the time, when I select an option from the combobox, I receive the message: 'Use of uninitialized value in pattern match (m//) at ../JComboBox.pm line 567' I have tried thousands of things, but nothing seem to work. Is there anyone who has an idea? Thanks in advance, Ton

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://408312]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-23 16:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found