0

I have a csv file that is being loaded in my programme. It contains citys and areas and some other stuff (not important here). Once the csv is selected I load the data into several comboboxes.

1 Thing is not working, I have a combobox containing all the citys and now I need to list all the areas for that country based on selection from the combobox.

Here is the event:

 private void cboProvinciesItemStateChanged(java.awt.event.ItemEvent evt) {                                               
   //System.out.println(Arrays.asList(gemeentesPerProvincie(gemeentes)));
    invullenListProvincie(gemeentes);
}      

Here is the method:

      private void invullenListProvincie(ArrayList<Gemeentes> gemeentes) {

    Gemeentes gf = (Gemeentes) cboProvincies.getSelectedItem();
    DefaultListModel model = new DefaultListModel();
    JList list = new JList(model);

    for (Gemeentes gemeente : gemeentesPerProvincie(gemeentes)) {
        model.addElement(gemeente);
    }

    lstGemeentes.setModel(model);

}

and this is the method to filter all the areas that equal the selection from the combobox:

    private ArrayList<Gemeentes> gemeentesPerProvincie(ArrayList<Gemeentes> gemeentes) {

    String GemPerProv = (String) cboProvincies.getSelectedItem();
    ArrayList<Gemeentes> selectie = new ArrayList<Gemeentes>();
    for (Gemeentes gemeente : gemeentes) {
        if (gemeente.getsProvincie().equals(GemPerProv)) {
            selectie.add(gemeente);
        }
    }

    return selectie;
}

I am convinced the error is the way I am trying to add items to the jList gemeentesPerProvincie(), I have tried so many things already. I really hope someone can see what i am clearly missing...

mKorbel
  • 109,525
  • 20
  • 134
  • 319
logistef
  • 746
  • 1
  • 7
  • 20
  • 1
    It's hard to say what's wrong based on bits and pieces of code. If you don't get a strong answer soon, consider creating and posting an [sscce](http://sscce.org). – Hovercraft Full Of Eels Jun 20 '12 at 20:50
  • Are you registering the `cboProvinciesItemStateChanged` eventhandler with the combobox's state change event? Also, `gf` and `list` are unused and unneeded – Attila Jun 20 '12 at 20:57
  • I understand, but I tought maybe someone might see what I am doing wrong adding the items to the jList. Java is kinda annoying to ask questions about I think. Also the documentation for the jList is always about adding a simple string which is not the case here. – logistef Jun 20 '12 at 21:04
  • Yes Attila I register it with the combobox state change event. Would there be a possibility to upload my project somewhere so that people could have a look at it? Is this allowed @ stackoverflow? – logistef Jun 20 '12 at 21:06
  • A JList model can and should accept any type of object. The keys are that 1) all objects should be the same type (enforced with generics) and 2) making sure that you display your objects properly. The latter is where a ListCellRenderer comes in handy. – Hovercraft Full Of Eels Jun 20 '12 at 21:07
  • I have uploaded my project if anyone wants to give it a go, be my hero... http://dgoods.eu/gemeentes.zip – logistef Jun 20 '12 at 21:18
  • the correct url... http://dgoods.eu/Gemeentes.zip – logistef Jun 20 '12 at 21:25
  • Again, consider simplifying your problem greatly and then posting the compilable and runnable code with a small data file here. It will greatly increase your chances of getting useful help. 1+ to @mKorbel for his useful suggestions. – Hovercraft Full Of Eels Jun 20 '12 at 21:42

1 Answers1

2
  • use AutoComplete JComboBox / JTextField instead of plain JCombobox

  • use JTable with one Column (maybe without JTableHeader)

  • create TableModel with two Columns, cities and areas, then both JComboBox and JTable have got the same data, from JTable to remove Column cities (JTable.removeColumn() is only about JTables view, data are still presents in the TableModel)

  • add RowFilter to JTable to the Column contains cities, output to the JTables view will be only areas for cities,

  • value for RowFilter will be became from JComboBox.getSelectedItem

  • data for JCombobox are from TableModel

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Thanks for your answer, the only problem is that I have to do it in a jList. I have a test in a couple of days so the code itself will be checked. – logistef Jun 20 '12 at 21:09
  • change JList to the JTable isn't something, somehow very complicated matter – mKorbel Jun 20 '12 at 21:10
  • I have uploaded my project if anyone wants to give it a go, be my hero... http://dgoods.eu/Gemeentes.zip – logistef Jun 20 '12 at 21:18