• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Skipping an element when reading out from an array.

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The general setup i have is a JComboBox with a list of options. When one is chosen four Jbuttons appear with labels depending on what was selected in the JComboBox.

When you click one of those buttons it reads out from an array (which contains the option in the JComboBox) into a set of JTextFields.

I am looking to have it read out and skip the option that was chosen in the JComboBox.e.g. if you choose "Three" in the JComboBox and the array contains "One" "Two" "Three" "Four" it will read out "One" into the first box, "Two" into the second box and "Four" into the third box.

I have made a counter that will skip the option in the JComboBox


Where list is my array, comboChoice is the option chosen from the JComboBox and gui.matches are the JTextFields it reads out to.

My problem is it reads "One" in the first box, "Two" in the second box, the third box is blank and reads "Four" in the fourth box.

I have tried making a parallel counter.e.g. something like

so that the counter for the array and the counter for the read out are seperate, and that the counter for the read out steps back (and therefore steps back a box) on coming to the option chosen in the JComboBox.
What seems to happen though is that you get a readout in all boxes but they all end up being the last element of the array.
Does anyone have any advice on how best to achieve what i am looking to do?

Thank You.
 
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So you'll have 4 JTextFields filled with 3 items. What's supposed to be in the 4th JTextField?

From what you've told us (my interpretation, anyway):

1. each of the 4 JTextFields can contain either "One", "Two", "Three", or "Four";
2. apparently none of the JTextFields will repeat what is shown in any of the other JTextFields
3. "One", "Two", "Three", and "Four" are displayed in order (with one item missing)
4. The item shown in the JComboBox is not shown (or skipped) in the JTextFields

Possible approach:

The index of the chosen item in JComboBox can be obtained using the getSelectedIndex() method.
Using a switch or if/else logic, that index could then be used to determine which item WILL NOT appear (will be skipped) in the JTextFields, remembering that index 0 will correspond to "One", index 1 to "Two", etc.
 
wayne morton
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply Greg.

I have sorted it now as of the code below, but to clarify a little, the forth JTextField is left blank and will be set invisible, or to be more precise all JTextFields will be set invisible to start with and only set visible when they have data entered into them as i will be using multiple arrays of varying lengths so not all JTextFields will be needed at all times.




I see someone edited my post so that the code was separated, how do i do that so i can make it clearer in future?

Never mind, found it

Thank You Again.







}
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

wayne morton wrote:I have sorted it now as of the code below...


Wayne,

Please UseCodeTags (←click) when you post code. I suspect somebody cleaned up your previous post, but I'll leave you to do it this time. Just read the page (thoroughly) and use the 'Edit' button to add them to your post. You'll be amazed how much better it looks.

Thanks.

Winston
 
reply
    Bookmark Topic Watch Topic
  • New Topic