0

I am trying to make a unit converter in LWUIT, and I am working with two comboboxes.

The application basically converts the unit selected in one combobox to the unit selected in other combobox.

http://www.unit-conversion.info/length.html#data

It works like this online converter.

I have made it successfully, but I am facing a problem regarding comboboxes.

When I select an item on a combobox and then select an item on another combobox, the selection of previous combobox is lost.

Due to that I have to select the combobox again and again multiple times even when I want my one unit to be fixed.

I want my application to work like this online unit converter where the selection once made is fixed till another selection is made.

So is there any method which retains your selection till you haven't manually changed it?

I can't upload any image due to my low rating and providing code would be of no use as it's too lengthy and it works fine for the most part.

Please I need help, any info would be really helpful.

            Form b = new Form();
            String content = {"contents here for combobox 1"};
            String content2 = {"contents here for combobox 2"};
            TextArea value1 = new TextArea();
            value1.setConstraint(TextArea.DECIMAL)
        TextArea value2 = new TextArea();
            value2.setEditable(false);
            final ComboBox V2 = new ComboBox(content2);
            V2.setListCellRenderer(new checkBoxRenderer1());

            final ComboBox V1 = new ComboBox(content);
            V1.setListCellRenderer(new checkBoxRenderer());


            V1.addActionListener(new ActionListener()
             {
              public void actionPerformed(ActionEvent evt)
               {
             int a = V1.getSelectedIndex();
                 switch(a){
                  case 0:
                         int v2sel = V2.getSelectedIndex();
                         switch(v2sel){
                                       case 0 :
                                   double d1 ;                    
                                   value2.setText(value1.getText());
                                   break;
                                       case 1 :
                                       d1 = Double.parseDouble(value1.getText());
                                       d1 = d1 * 0.1;
                                   value2.setText(Double.toString(d1));
                                   break;
                                       case 2 :
                                   d1 = Double.parseDouble(value1.getText());
                                       d1 = d1 * 1.0e-5;
                                   value2.setText(Double.toString(d1));
                                   break;
                                       // multiple case statements
                                  }
                             //multiple case statements 
                                }
                            });


             b.addComponent(V1);

                 b.addComponent(value1);


             b.addComponent(V2);
             b.addComponent(value2);

             b.addComponent(Bexit);
                     b.show();

                     // End
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user2497398
  • 61
  • 1
  • 1
  • 4
  • 1
    `So is there any method which retains your selection till you haven't manually changed it?` - this is the default behaviour. Each comboBox works independly of the other. The problem is with your code. Post your [SSCCE](http://sscce.org/) that demonstrates the problem. `providing code would be of no use as it's too lengthy` - code should always be provided (in the form of a SSCCE). In your case a frame with two combo boxes. I whould gues is would be less than 20-30 lines of code to demonstrate the described problem. Usually when you create a SSCCE you find your real problem. – camickr Jun 22 '13 at 14:39
  • sorry for not providing the code, i was being lazy. Now i have edited my post. – user2497398 Jun 22 '13 at 18:28
  • `could you please guide me towards making saperate listmodels so that the checkbox states are preserved` - this is the default behaviour, unless you specifically add the same model to both combo boxes. `any sort of info would be of great help` - still waiting for your SSCCE that demonstrates the problem. – camickr Jun 23 '13 at 17:34

1 Answers1

1

Did you set the same model instance to both combo boxes?

Model controls selection as well so this obviously wouldn't work.

Shai Almog
  • 51,749
  • 5
  • 35
  • 65
  • i have passed two saperate strings in both comboboxes..... i am using the default list model for both boxes...not working posting the code now – user2497398 Jun 22 '13 at 18:03
  • Why are you using a checkbox renderer? The combo box only has one selected item anyway... Are you referring to the checkbox state not being preserved? It wouldn't be because it must be stored in the model and you are using strings. – Shai Almog Jun 23 '13 at 05:46
  • yes exactly....the checkbox state is not being preserved as soon as i click on a textfield or other combobox ... checkbox renderer is to use checkboxes in the combobox.... could you please guide me towards making saperate listmodels so that the checkbox states are preserved..... please sir , any sort of info would be of great help – user2497398 Jun 23 '13 at 14:41
  • There is no reason to add checkboxes to a combo box only to a list. In a list you should read about model and renderer and actually understand the logic: http://stackoverflow.com/questions/2810630/list-with-checkbox-using-lwuit a renderer has no state so it will obviously not keep the selected state – Shai Almog Jun 23 '13 at 19:19
  • thanks Shai ... http://stackoverflow.com/questions/17305584/textfield-not-updating-dynamically please help me with this TextField issue – user2497398 Jun 25 '13 at 19:30