0

When inputting a new Student object inside my JTables I would like to hold all the localities/cities inside my JComboBox. Each city is stored inside a HashMap which holds the name as key(as there are no duplicates) and it's postcode.

Example: hashMap.put("City", "AAA")

Now my issue would be to represent the HashMap OR List<String> inside the JComboBox itself. The cheap and easy alternative was to simply re-write the String[] to hold all the town names and switch-case the selected value but there are few issues with that:

  1. It's too long, adding new localities may be a pain or waste of time
  2. Alot of unnecessary code
  3. Looks horrible if being reviewed by someone
  4. Potentially slower than my proposed method
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Juxhin
  • 5,068
  • 8
  • 29
  • 55
  • 2
    The key is not the JComboBox but rather its model. You could create your own model class by extending [AbstractListModel](http://docs.oracle.com/javase/8/docs/api/javax/swing/AbstractListModel.html) and implement the right interfaces, and then have it accept your choice of collections. Note though that order is important, and so a HashMap would likely not be acceptable. An ordered map would, I believe. – Hovercraft Full Of Eels Aug 23 '14 at 20:28
  • I don't see a question. Or any code. What have you tried so far? Please read [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask). – DavidPostill Aug 23 '14 at 20:28
  • @HovercraftFullOfEels - I understand, it's the same concept I had to undergo with my JTables. Since it would originally accept `Object[][]` I had to create my own model to accept List to retrieve the required fields. The only part I didn't quite get is the order and ordered map. 1) Why would it be an issue. 2) Why would an ordered map be required instead of let's say `List`? – Juxhin Aug 23 '14 at 20:32
  • 2
    I didn't say that. A `List` would work just fine and easily too. I'm just saying that if you absolutely had to use a Map as your model, it would have to be ordered. The model is *indexed* which forces order on it. – Hovercraft Full Of Eels Aug 23 '14 at 20:32
  • 3
    As HovercraftFullOfEels has already stated, of the default models don't suit your needs, you are free To create your own... – MadProgrammer Aug 23 '14 at 20:48

1 Answers1

1

Here you go :

 String [] string = {"city","town","country","province"};
 java.util.List<String> list = new ArrayList<String>(Arrays.asList(string));


 Object[] arrayObject= list.toArray();
 String [] data = Arrays.copyOf(arrayObject, arrayObject.length,String[].class); // java 1.6+
 JComboBox<String> combo = new JComboBox<>( data);

Actually you can do :

 String [] string = {"city","town","country","province"};
 java.util.List<String> list = new ArrayList<String>(Arrays.asList(string));


 JComboBox< java.util.List<String>> combo = new JComboBox<>( );
 combo.addItem(list);

but , for every single elemen of JComboxBox will hold all elements of list in a row.

*To prevent ambiguity between java.util.List & java.awt.List , we should declare them clearly.

Here the complete demo :

import java.awt.*;
import javax.swing.*;
import java.util.*;

public class ComboBoxDemo extends JFrame {
public ComboBoxDemo() {

  super("JComboBox Demo");

    String [] string = {"city","town","country","province"};
  java.util.List<String> list = new ArrayList<String>(Arrays.asList(string));


 Object[] arrayObject= list.toArray();
 String [] data = Arrays.copyOf(arrayObject, arrayObject.length,String[].class); // java 1.6+
 JComboBox<String> combo = new JComboBox<>( data);

    setLayout(new FlowLayout(FlowLayout.CENTER));
    add(combo, BorderLayout.CENTER);      
}

 public static void main(String[] args) {
      ComboBoxDemo g = new ComboBoxDemo();
    g.setVisible(true);
    g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    g.setBounds(100, 100, 300, 300);
 }
 }

enter image description here

And the result of

JComboBox< java.util.List<String>> combo = new JComboBox<>( ); combo.addItem(list);

declaration :

enter image description here

Fevly Pallar
  • 3,059
  • 2
  • 15
  • 19