Solved

jcombobox getselectedindex problem

Posted on 2004-04-13
15
998 Views
Last Modified: 2008-01-09
Hi!
I'm experiencing some problems with the getselectedindex method for jcombobox....for some reason it does not return the selectedindex...
it could return the previous or something else...whyy?? her is the code..

public void act2() {
    if ( (jComboBox3.getSelectedItem() != null)) {
      int ding;
      int axis;
      int ding3 = 0;

      axis = ( (int) jComboBox2.getSelectedIndex());
      boolean[] get = (boolean[]) Array.newInstance(boolean.class, 16);
      boolean[] get2 = (boolean[]) Array.newInstance(boolean.class, 16);
      ding = ( (int) jComboBox3.getSelectedIndex());       <<<<<<<<here is the problem  it works fine...most fo the time???

      get = (boolean[]) vecTest.elementAt(ding);
      get2 = (boolean[]) vecTest2.elementAt(ding);

      if (axis == 0) {
        jCheckBox5.setSelected(get[0]);
        jCheckBox10.setSelected(get[1]);
        jCheckBox6.setSelected(get[2]);
        jCheckBox19.setSelected(get[3]);
        jCheckBox3.setSelected(get[4]);
        jCheckBox1.setSelected(get[5]);
        jCheckBox12.setSelected(get[6]);
        jCheckBox14.setSelected(get[7]);
        jCheckBox7.setSelected(get[8]);
        jCheckBox15.setSelected(get[9]);
        jCheckBox17.setSelected(get[10]);
        jCheckBox8.setSelected(get[11]);
        jCheckBox13.setSelected(get[12]);
        jCheckBox4.setSelected(get[13]);
        jCheckBox2.setSelected(get[14]);
        jCheckBox18.setSelected(get[15]);

      }
      else {
        jCheckBox5.setSelected(get2[0]);
        jCheckBox10.setSelected(get2[1]);
        jCheckBox6.setSelected(get2[2]);
        jCheckBox19.setSelected(get2[3]);
        jCheckBox3.setSelected(get2[4]);
        jCheckBox1.setSelected(get2[5]);
        jCheckBox12.setSelected(get2[6]);
        jCheckBox14.setSelected(get2[7]);
        jCheckBox7.setSelected(get2[8]);
        jCheckBox15.setSelected(get2[9]);
        jCheckBox17.setSelected(get2[10]);
        jCheckBox8.setSelected(get2[11]);
        jCheckBox13.setSelected(get2[12]);
        jCheckBox4.setSelected(get2[13]);
        jCheckBox2.setSelected(get2[14]);
        jCheckBox18.setSelected(get2[15]);

      }
    }
  }


...in the combobox I have various strings for which when selected some options appear......

Thanks
0
Comment
Question by:Thunder_scream
  • 8
  • 5
15 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 10811274
>> I'm experiencing some problems with the getselectedindex method for jcombobox
>> ....for some reason it does not return the selectedindex...

What does it return than? Give an example please.

e.g. You select the 2nd item in the combo box and the returned int is ...?


Remark (that - I know - has nothing to do with your problem):

That big if-else statement can be replaced by

        jCheckBox5.setSelected( axis == 0) ? get[0] : get2[0] );
        ...
        jCheckBox18.setSelected( axis==0 ? get[15] : get2[15] );

which is better code viewed from the point of maintainability.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10811281
Did you already try to use getSelectedItem() ?
It should return the selected String.
0
 
LVL 2

Author Comment

by:Thunder_scream
ID: 10811379
there seem to be some problem with the update...hope this wont appear 8 times now..

yes the selecteditem does work...fine..

first an example for previous question..
 suppose the jcombo contains..

/////
345v
576p
398s
////

selecting the first item return index 0 , the second 1 ..etc..
but randomly if a select e.g index 2 I get 1 as return...which causes ...big problems..

for other parts of my code I've tried to use selectItem.. but suppose I wanted to do this here..
e.g we select item 0...i.e we use getSelectItem and we get 345v as return value..
the user makes her selection and it will be stored in a vector containg array of booleans...

suppose now ..we go on with other selection...and come back to 345v then the previous options selected should appear..
i.e. using the getselectedItem I must search the vector for that name....and return that value..

there is two problems with that ...one 345v can appear several times.(the items are generic)..in the vector...and there could be up to 4000 of selections..in the array..it will be costly to do this... that's why I've choosen the selectIndex..
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10811445
>>yes the selecteditem does work...fine..
Very strange. I mean, that getSelectedIndex() doesn't work.

>> one 345v can appear several times
I see.
Using getSelectedItem() was of course just to check.
If you have a problem you have to detect why and solve it.

>> selecting the first item return index 0 , the second 1 ..etc..
>> but randomly if a select e.g index 2 I get 1 as return
So, what you are saying is, that it works OK if you select them in order? (also reverse?)
But not when you select randomly?

Where is your function act2() triggered/called?
Can you post some more of your code?
0
 
LVL 2

Author Comment

by:Thunder_scream
ID: 10811615
>So, what you are saying is, that it works OK if you select them in order? (also reverse?)
>But not when you select randomly?
not exactly, I meant randomly I get the wrong index back

>Where is your function act2() triggered/called?
>Can you post some more of your code?
I try to dive the code in two section...this the where we set the values...(thanks for ealier tip...it sure makes the code..shorter..)

so when the app starts...the user decides to picks some items( actully nodes of a mesh) which will appear in the combobox..then we do this..
================================================
 public void initAct(boolean first) {
    vecTest = new Vector();
    vecTest2 = new Vector();

    boolean[] set = (boolean[]) Array.newInstance(boolean.class, 16);
    boolean[] set2 = (boolean[]) Array.newInstance(boolean.class, 16);
    for (int i = 0; i < (jList2.getModel().getSize()); i++) { //for number of nodes in combobox do
      for (int j = 0; j < (15); j++) {
        set[j] = false;
        set2[j] = false;
      }
      vecTest2.insertElementAt(set2, i);
      vecTest.insertElementAt(set, i);
    }
    //vectors
=============================================================


//then she starts to set the settings for each item in the jcombobox3
=============================================================
this button is pressed when selection is made..
//accept changes button
  void jButton25_actionPerformed(ActionEvent e) {
    if (jComboBox3.getSelectedItem() != null) {      
      act(false);
    }
  }
=============================================================

//here I try to store the option chosen...there is always two for each item in the jcombobox3...one for x and one for y..(sorry this kind of code gets easily to long.)
=============================================================
  public Vector act(boolean flag4) {
    Vector vecTe = new Vector();
    if (jComboBox3.getSelectedItem() != null) {
      int nodeSel;
      int axis;

      boolean[] set = (boolean[]) Array.newInstance(boolean.class, 16);
      boolean[] set2 = (boolean[]) Array.newInstance(boolean.class, 16);
      nodeSel = ( (int) jComboBox3.getSelectedIndex());//here we have the mesh nodes.
      axis = ( (int) jComboBox2.getSelectedIndex());//this is the x and y axis
      if (axis == 0) {
        set[0] = jCheckBox5.isSelected() && jCheckBox5.isEnabled();
        set[1] = jCheckBox10.isSelected() && jCheckBox10.isEnabled();
        set[2] = jCheckBox6.isSelected() && jCheckBox6.isEnabled();
        set[3] = jCheckBox19.isSelected() && jCheckBox19.isEnabled();
        set[4] = jCheckBox3.isSelected() && jCheckBox3.isEnabled();
        set[5] = jCheckBox1.isSelected() && jCheckBox1.isEnabled();
        set[6] = jCheckBox12.isSelected() && jCheckBox12.isEnabled();
        set[7] = jCheckBox14.isSelected() && jCheckBox14.isEnabled();
        set[8] = jCheckBox7.isSelected() && jCheckBox7.isEnabled();
        set[9] = jCheckBox15.isSelected() && jCheckBox15.isEnabled();
        set[10] = jCheckBox17.isSelected() && jCheckBox17.isEnabled();
        set[11] = jCheckBox8.isSelected() && jCheckBox8.isEnabled();
        set[12] = jCheckBox13.isSelected() && jCheckBox13.isEnabled();
        set[13] = jCheckBox4.isSelected() && jCheckBox4.isEnabled();
        set[14] = jCheckBox2.isSelected() && jCheckBox2.isEnabled();
        set[15] = jCheckBox18.isSelected() && jCheckBox18.isEnabled();
        if (flag4 == false) {
          vecTest.insertElementAt(set, nodeSel);
        }
        else {
          vecTe.addElement(set);
        }
      }
      else {
        set2[0] = jCheckBox5.isSelected() && jCheckBox5.isEnabled();
        set2[1] = jCheckBox10.isSelected() && jCheckBox10.isEnabled();
        set2[2] = jCheckBox6.isSelected() && jCheckBox6.isEnabled();
        set2[3] = jCheckBox19.isSelected() && jCheckBox19.isEnabled();
        set2[4] = jCheckBox3.isSelected() && jCheckBox3.isEnabled();
        set2[5] = jCheckBox1.isSelected() && jCheckBox1.isEnabled();
        set2[6] = jCheckBox12.isSelected() && jCheckBox12.isEnabled();
        set2[7] = jCheckBox14.isSelected() && jCheckBox14.isEnabled();
        set2[8] = jCheckBox7.isSelected() && jCheckBox7.isEnabled();
        set2[9] = jCheckBox15.isSelected() && jCheckBox15.isEnabled();
        set2[10] = jCheckBox17.isSelected() && jCheckBox17.isEnabled();
        set2[11] = jCheckBox8.isSelected() && jCheckBox8.isEnabled();
        set2[12] = jCheckBox13.isSelected() && jCheckBox13.isEnabled();
        set2[13] = jCheckBox4.isSelected() && jCheckBox4.isEnabled();
        set2[14] = jCheckBox2.isSelected() && jCheckBox2.isEnabled();
        set2[15] = jCheckBox18.isSelected() && jCheckBox18.isEnabled();
        if (flag4 == false) {
          vecTest2.insertElementAt(set2, nodeSel);
        }
        else {
          vecTe.addElement(set2);
        }
      }
    }
    return vecTe;
  }
===================================================
ok so all above was how I store the options... the retrival is done like this..

===================================================
 * In this combobox2 we show the user the axis avalible for charting purposes.
   *
   * @return ignored.
   */
  void jComboBox2_actionPerformed(ActionEvent e) {
    if (jComboBox3.getSelectedItem() != null) {
      act2();
    }
  }

===================================================
   * In this combobox3 we show the user the nodes of his choice.
   *
   * @return ignored.
   */
  void jComboBox3_actionPerformed(ActionEvent e) {
    if ( (jComboBox3.getSelectedItem() != null) &&
        (jComboBox3.getItemCount() > 1)) {
      availOps( (String) jComboBox3.getSelectedItem());  this just sets the related checkbox to this item
      act2();
    }
  }
===================================================

and as you suggest...the act2
===================================================
 public void act2() {
    if ( (jComboBox3.getSelectedItem() != null)) {
      int ding;
      int axis;
      int ding3 = 0;
      axis = ( (int) jComboBox2.getSelectedIndex());
      boolean[] get = (boolean[]) Array.newInstance(boolean.class, 16);
      boolean[] get2 = (boolean[]) Array.newInstance(boolean.class, 16);
      ding = ( (int) jComboBox3.getSelectedIndex());
      get = (boolean[]) vecTest.elementAt(ding);
      get2 = (boolean[]) vecTest2.elementAt(ding);
      jCheckBox5.setSelected( (axis == 0) ? get[0] : get2[0]);
      jCheckBox10.setSelected( (axis == 0) ? get[1] : get2[1]);
      jCheckBox6.setSelected( (axis == 0) ? get[2] : get2[2]);
      jCheckBox19.setSelected( (axis == 0) ? get[3] : get2[3]);
      jCheckBox3.setSelected( (axis == 0) ? get[4] : get2[4]);
      jCheckBox1.setSelected( (axis == 0) ? get[5] : get2[5]);
      jCheckBox12.setSelected( (axis == 0) ? get[6] : get2[6]);
      jCheckBox14.setSelected( (axis == 0) ? get[7] : get2[7]);
      jCheckBox7.setSelected( (axis == 0) ? get[8] : get2[8]);
      jCheckBox15.setSelected( (axis == 0) ? get[9] : get2[9]);
      jCheckBox17.setSelected( (axis == 0) ? get[10] : get2[10]);
      jCheckBox8.setSelected( (axis == 0) ? get[11] : get2[11]);
      jCheckBox13.setSelected( (axis == 0) ? get[12] : get2[12]);
      jCheckBox4.setSelected( (axis == 0) ? get[13] : get2[13]);
      jCheckBox2.setSelected( (axis == 0) ? get[14] : get2[14]);
      jCheckBox18.setSelected( (axis == 0) ? get[15] : get2[15]);
    }
  }


hope this is enough :)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10811684
Mmmm. I think see.

You store the made selections (via checkboxes) in a Vector. (in fact one per axis)
And when the user makes a combo box selection you show the accordingly made (or if not yet made all are false) selections.
So the user can
1) make a selection (e.g. 2nd item in the combo box)
2) check some check boxes to make some choices
3) press a key to indicate his selection is done
4) the selection is stored
5) he makes a new selection in the combo box
6) Now he returns back to the 2nd combo box item
7) He must see the previously made choices. Right?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10811689
But of course if getSelectedIndex() returns the wrong index you have problems to display the previously made choices.

Thinking about what could be the reason of the bad index returned...
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 2

Author Comment

by:Thunder_scream
ID: 10811783
yes...that's correct....:)
getting wrong index = problem

let me give you some more examples when this problem occur...
1: we start fresh everything is false
2: I take the first item in the jcombobox3
3: set some settings for x-axies(jcombobox2) click accept,
4: change to y-axis set some settings for y..
5: repeat above step 2-4
6 : but suddenly when selecting a new item (jcombobox3) where all values should be false...I get some checkboxes to be ticked
7: this indicates that wrong index have been return and I have values from some other..item..
8: step 6-7 most of the time do not happen..

and the entire ...option settings are all in a joptionspane(might be helpful to know).
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10811831
>> option settings are all in a joptionspane
In a JOptionPane ?
I only use this one for its static functions showXXXX()...

>> Thinking about what could be the reason of the bad index returned...
Just a thought:
- Sure you only fill up your combo box once? And not over and over again?

How do you fill up your combo box? What combobox model are you using?
0
 
LVL 2

Author Comment

by:Thunder_scream
ID: 10811964
>How do you fill up your combo box? What combobox model are you using?
do I need a model for that...oops..it seems to work :) (ok if the jcombobox is generic ..we need a model..for it?)
this is how I fill the combobox..(how is a typical model setup for jcombo?)  but I'm not sure this will solve the problem
because I have this problem with other static comboboxes, these aswell do not have models..

the user selects some items ...these are placed in a list"jlist2" once they hit the button ok..I do something like below...

int jList2Size = jList2.getModel().getSize();
 if (jList2Size > 0) {
            jComboBox3.removeAllItems();
            jComboBox3.addItem("Select All");//  >>giving the option of selecting all nodes first item of combo
            for (int i = 0; i < jList2Size; i++) {
              jComboBox3.addItem(jList2.getModel().getElementAt(i));
            }
          }



>In a JOptionPane ?
>I only use this one for its static functions showXXXX()...

I do something like this.
Object[] panel = {
        optionsPane};//>>checkboxes goes in here.
    JOptionPane pane = new JOptionPane(panel, JOptionPane.PLAIN_MESSAGE,
                                       JOptionPane.OK_CANCEL_OPTION);
    JDialog jdogtest = pane.createDialog(this, "Options");
    jdogtest.show();

I'd try to check up how to set the model for the combo
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10812371
>> do I need a model for that...oops..it seems to work
That's OK. If you don't specify a combobox model automatically the DefaultComboBoxModel is used.
(http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/DefaultComboBoxModel.html)

>> the user selects some items ...these are placed in a list"jlist2" once they hit the button ok..I do something like below...
>>
>> int jList2Size = jList2.getModel().getSize();
>> if (jList2Size > 0) {
>>            jComboBox3.removeAllItems();
>>            jComboBox3.addItem("Select All");//  >>giving the option of selecting all nodes first item of combo
>>            for (int i = 0; i < jList2Size; i++) {
>>              jComboBox3.addItem(jList2.getModel().getElementAt(i));
>>            }
>>          }

Mmm. So each time the user hits the button, you refill your combo box.
How can you be sure that (between two presses) the same combo box item (eg. rv457) will correspond with the same index? e.g. rv457 had index = 3, will it have index 3 again when the user presses OK and the combo box is refilled?
Could be a problem...


0
 
LVL 2

Author Comment

by:Thunder_scream
ID: 10812459
you are right but...every time new set of items are choosen I do initAct(boolean first).
.I just missed that out in above code..or see below...which is complete

//calling the node setting joptions pane
  void jButton21_actionPerformed(ActionEvent e) {
    Object[] panel = {
        optionsPane2};
    JOptionPane pane2 = new JOptionPane(panel, JOptionPane.PLAIN_MESSAGE,
                                        JOptionPane.OK_CANCEL_OPTION);
    JDialog jdogtest2 = pane2.createDialog(this, "Options");
    jdogtest2.show();
    try {
      int value = ( (Integer) pane2.getValue()).intValue();
      if (value == JOptionPane.OK_OPTION) {

        // Get the index of all the selected items in jlist2
        int jList2Size = jList2.getModel().getSize();
        if (jList2Size > 0) {
          jComboBox3.removeAllItems();
          jComboBox3.addItem("Select All");
          for (int i = 0; i < jList2Size; i++) {
            jComboBox3.addItem(jList2.getModel().getElementAt(i));
          }
          initAct(true);<<<<the piece of code related to the boolean true is not pasted (would be to long) and is not related to this problem.
        }
      }
    }
    catch (Exception ex) {
      //error either getting user answer or something else! do nothing (i.e. keep orginal settings)
    }
  }

I have this problem in the first attempt...already ...i.e first time...a set is choosen..
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10812801
I'm out of ideas. Sorry.
0
 

Accepted Solution

by:
PashaMod earned 0 total points
ID: 10995882
Closed, 125 points refunded.
modname
Community Support Moderator
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now