JLabel array problem

I know there's an easy solution to this, one that will make me kick myself for not figuring it out beforehand, but here we go: I have a combo box and 6 labels in an array. What I want to happen is have it where whatever choice the user makes from the combo box is put after the word "Selected:", and all the other choices put in order afterwords skipping the spot where the selected item is.

For instance: if I were to select 3 the print out would be:

Selected: 3

1

2

4

5

6

If 6 was selected it would be:

Selected: 6

1

2

3

4

5

...etc...

I know all I need to use is some sort of simple 'if else' statement to get this to work properly, but I just can't get it! How embarrassing...

Here's the code (sorry if the indents are too big):

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

publicclass Test

{

publicstaticvoid main(String[] args)

{

//Initiate everything

JFrame frame =new JFrame();

frame.getContentPane().setLayout(new GridLayout(7, 1));

String[] values ={"One","Two","Three","Four","Five","Six"};

final JComboBox cb =new JComboBox(values);

final JLabel[] labels =new JLabel[6];

//Add components

frame.getContentPane().add(cb);

for (int i = 0; i < labels.length; i++)

{

labels[i] =new JLabel((String)cb.getItemAt(i));

frame.getContentPane().add(labels[i]);

}

labels[0].setText("Selected: " + labels[0].getText());

//Add listener

cb.addItemListener

(

new ItemListener()

{

publicvoid itemStateChanged(ItemEvent e)

{

int index = cb.getSelectedIndex();

for (int i = 1; i < labels.length; i++)

{

/*if (i == index)

labels[0].setText("Selected: " + (String)cb.getItemAt(index));

else if (i > index)

labels[i].setText((String)cb.getItemAt(i));*/

if (i < index)

labels[i].setText((String)cb.getItemAt(i));

elseif (i == index)

labels[0].setText("Selected: " + (String)cb.getItemAt(index));

else

labels[i].setText((String)cb.getItemAt(i - 1));

}

}

}

);

//Finalize and display window

frame.pack();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setLocationRelativeTo(null);

frame.setVisible(true);

}

}

[4156 byte] By [Jason102a] at [2007-11-26 14:37:30]
# 1

This code works. Be careful not to confuse your indices and your data structures. It also makes things a lot simpler to reference the values array rather than try to get the values from the combo box...

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class Test

{

public static void main(String[] args)

{

//Initiate everything

JFrame frame = new JFrame();

frame.getContentPane().setLayout(new GridLayout(7, 1));

final String[] values = {"One", "Two", "Three", "Four", "Five", "Six"};

final JComboBox cb = new JComboBox(values);

final JLabel[] labels = new JLabel[6];

//Add components

frame.getContentPane().add(cb);

for (int i = 0; i < labels.length; i++)

{

labels[i] = new JLabel((String)cb.getItemAt(i));

frame.getContentPane().add(labels[i]);

}

labels[0].setText("Selected: " + labels[0].getText());

//Add listener

cb.addItemListener

(

new ItemListener()

{

public void itemStateChanged(ItemEvent e)

{

int index = cb.getSelectedIndex();

for (int i = 0; i < labels.length; i++)

{

if (i < index)

labels[i+1].setText(values[i]);

else if (i == index)

labels[0].setText("Selected: " + values[i]);

else

labels[i].setText(values[i]);

}

}

}

);

//Finalize and display window

frame.pack();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setLocationRelativeTo(null);

frame.setVisible(true);

}

}

therevolutiona at 2007-7-8 8:18:30 > top of Java-index,Desktop,Core GUI APIs...
# 2
Yep, so it was an easy solution. Thanks therevolution. That's also a good point when you use the values[] array instead of referencing the combo box for less confusion. Thanks again.
Jason102a at 2007-7-8 8:18:30 > top of Java-index,Desktop,Core GUI APIs...
# 3

> That's also a good point when you use the values[] array instead of

> referencing the combo box for less confusion.

I don't agree. It may be a few less character to type but it not a good idea.

The array has nothing to do with the combo box once the combo box has been create. The array is used to create a DefaultComboBoxModel which uses a Vector to store the items. Each item from the array is then copied to the Vector. So if you change the contents of the array the contents of the combo box will not change.

You should always reference the model once it has been created.

camickra at 2007-7-8 8:18:30 > top of Java-index,Desktop,Core GUI APIs...
# 4

camickr:

I guess you're right. When I made that switch, I had it in my head that the combo box values were changing, when in fact they don't. Certainly I didn't do it to save keystrokes - Java's a terrible language to choose if that's your goal. ;) Though the sore loser in me wants to say it's pretty trivial in this case, I agree that it would have been a good idea to stick to the combo box's model.

therevolutiona at 2007-7-8 8:18:30 > top of Java-index,Desktop,Core GUI APIs...