Author Topic: JList Index  (Read 1948 times)

0 Members and 1 Guest are viewing this topic.

Offline ElementCoder

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 611
  • Rating: +42/-2
    • View Profile
JList Index
« on: June 08, 2012, 09:49:34 am »
I'm having some trouble with getting which index of the list is selected. The list (JList) is in a scrollpane, I added a ListSelectionListener and in the listener I gave it the task to return the selected index. But it always returns -1. The code is below. What am I doing wrong?

List:
Code: [Select]
DefaultListModel kubesN = new DefaultListModel();
Code: [Select]
final String kubes[] = { "Bedrock", "Water", "Dirt", "Grassy Dirt",
"Rock", "Wood", "Green Leaves", "Old Citronox", "Short Grass",
"Tall Grass", "Wood Rose", "Mushlite", "Stellar Sand",
"Creeping Vines", "Hanging Grass", "Cactus", "Citronox",
"ESCorp Box", "Cracked Rock", "HardRoq", "Pyrite",
"Cactus Root", "Cactus Flower", "Blue Mineral", "Blue Crystal",
"Copper", "Hangar Ceiling Lighting", "Copper Arrow",
"Bound Copper", "Copper Wire Mesh", "Hangar Lighting",
"Purple Leaves", "Pink Leaves", "Moon Earth",
"Grassy Moon Earth", "Moon Rock", "Moon Dust", "Lumen Rock",
"Dead Lumen Rock", "Lumen Rock Column", "Moon Water",
"Moon Rock Column", "Cracked Moon Rock", "Purple Stain",
"Pink Stain" };
java.util.Arrays.sort(kubes);
JList kube = new JList(kubes);
kube.setModel(kubesN);
ListListener listlistener = new ListListener();
kube.addListSelectionListener(listlistener);
for (int i = 0; i < kubes.length; i++)
kubesN.addElement(kubes[i]);
kube.setVisibleRowCount(5);
kube.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane kubeView = new JScrollPane(kube);

ListSelectionListener:
Code: [Select]
private static class ListListener implements ListSelectionListener{
public void valueChanged(ListSelectionEvent e){
if (!e.getValueIsAdjusting()){
int index = kube.getSelectedIndex();
System.out.println(index);
}
}
}
}

Some people need a high five in the face... with a chair.
~EC

Offline jacobly

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 205
  • Rating: +161/-1
    • View Profile
Re: JList Index
« Reply #1 on: June 08, 2012, 01:28:04 pm »
How does this kube
Code: [Select]
JList kube = new JList(kubes);become this one?
Code: [Select]
kube.getSelectedIndex();Unless you have this.kube = kube; or something, they probably don't reference the same object.

On a side node, creating a ListModel is completely unnecessary when you use that JList constructor.

Offline ElementCoder

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 611
  • Rating: +42/-2
    • View Profile
Re: JList Index
« Reply #2 on: June 09, 2012, 05:50:07 am »
I see. Could you help me? I just can't get it to work. When using 'this', it gives an error about 'this' being impossible to use in a static context and when I remove the static, it gives an error that no enclosing type is available. I don't see how to fix this. Is there something I'm missing?
« Last Edit: June 09, 2012, 05:51:00 am by ElementCoder »

Some people need a high five in the face... with a chair.
~EC

Offline jacobly

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 205
  • Rating: +161/-1
    • View Profile
Re: JList Index
« Reply #3 on: June 09, 2012, 04:39:03 pm »
I can't help with your code without seeing more of it, but here is a complete example.

Spoiler For example:
import java.awt.BorderLayout;
import java.util.Arrays;

import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class Test extends JFrame implements ListSelectionListener {
   private static final String[] KUBE_NAMES = {
      "Bedrock", "Water", "Dirt", "Grassy Dirt",
      "Rock", "Wood", "Green Leaves", "Old Citronox", "Short Grass",
      "Tall Grass", "Wood Rose", "Mushlite", "Stellar Sand",
      "Creeping Vines", "Hanging Grass", "Cactus", "Citronox",
      "ESCorp Box", "Cracked Rock", "HardRoq", "Pyrite",
      "Cactus Root", "Cactus Flower", "Blue Mineral", "Blue Crystal",
      "Copper", "Hangar Ceiling Lighting", "Copper Arrow",
      "Bound Copper", "Copper Wire Mesh", "Hangar Lighting",
      "Purple Leaves", "Pink Leaves", "Moon Earth",
      "Grassy Moon Earth", "Moon Rock", "Moon Dust", "Lumen Rock",
      "Dead Lumen Rock", "Lumen Rock Column", "Moon Water",
      "Moon Rock Column", "Cracked Moon Rock", "Purple Stain",
      "Pink Stain"
   };
   static {
      Arrays.sort(KUBE_NAMES);
   }

   private JList kube;
   public Test() {
      super("Test");
      setDefaultCloseOperation(EXIT_ON_CLOSE);

      kube = new JList(KUBE_NAMES);
      kube.setVisibleRowCount(5);
      kube.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
      kube.addListSelectionListener(this);

      add(new JScrollPane(kube), BorderLayout.CENTER);

      pack();
      setVisible(true);
   }

   @Override
   public void valueChanged(ListSelectionEvent e) {
      if (!e.getValueIsAdjusting())
         System.out.println(kube.getSelectedIndex());
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         @Override
         public void run() {
            new Test();
         }
      });
   }
}


Offline ElementCoder

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 611
  • Rating: +42/-2
    • View Profile
Re: JList Index
« Reply #4 on: June 11, 2012, 04:56:05 am »
Thanks, that example helped me. It now works fine.

Some people need a high five in the face... with a chair.
~EC