1

I was having trouble trying to copy and paste the code here it said something about having unformatted?? i dont know its my first time but here is the troublesome code i'll link a pastebin so you can see too

whole code: http://pastebin.com/p6RQiSLz

the problematic code:

    //City (JList with txtfield)

    pane.add(lblCity);
    lblCity.setForeground(Color.BLACK);
    lblCity.setBounds(5,260,80,25);

    pane.add(list);
    list.setSelectedIndex(0);
    list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    list.setBackground(Color.BLACK);
    list.setForeground(Color.WHITE);
    list.setBounds(90,260,150,100);
    list.setEnabled(false);
    pane.add(txtCity);
    txtCity.setBounds(90,380,150,25);

//================================TROUBLED BLOCK OF CODE============================================

    list.addListSelectionListener(new ListSelectionListener(){
        public void valueChanged(ListSelectionEvent er){
            txtCity.setText(listCity[listCity.getSelectedIndex]);
        }
    });
Channa Jayamuni
  • 1,876
  • 1
  • 18
  • 29
Oscar Cubz
  • 33
  • 4

1 Answers1

1

listCity is an array and arrays don't have getSelectedIndex properties, there isn't even the concept of a "selected element" for arrays. You probably want the item that's selected on the JList made from listCity, so instead of listCity.getSelectedIndex, try list.getSelectedIndex(). That should at least compile.

blm
  • 2,379
  • 2
  • 20
  • 24
  • yeah it worked it seems I had 2 problems first was I was only using getSelectedIndex and I was referring to the wrong variable... – Oscar Cubz Sep 29 '15 at 17:43
  • @OscarCubz Cool, glad it worked. Not to shill for reputation or anything :-), but if an answer's useful and answers the question, it's polite to upvote it and mark it as answering the question. – blm Sep 29 '15 at 17:59
  • how exactly do I do that lol I just started here like not 30mins ago XD – Oscar Cubz Sep 29 '15 at 18:13
  • @OscarCubz To upvote, click the up arrow above the 0 to the left of the answer. – blm Sep 29 '15 at 19:48