File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Skipping an element when reading out from an array. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Skipping an element when reading out from an array." Watch "Skipping an element when reading out from an array." New topic

Skipping an element when reading out from an array.

wayne morton

Joined: May 17, 2012
Posts: 28
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.
Greg Brannon

Joined: Oct 24, 2010
Posts: 563
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.

Always learning Java, currently using Eclipse on Fedora.
Linux user#: 501795
wayne morton

Joined: May 17, 2012
Posts: 28
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.

Winston Gutkowski

Joined: Mar 17, 2011
Posts: 8942

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


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.



Bats fly at night, 'cause they aren't we. And if we tried, we'd hit a tree -- Ogden Nash (or should've been).
Articles by Winston can be found here
I agree. Here's the link:
subject: Skipping an element when reading out from an array.
It's not a secret anymore!