Omnimaga

General Discussion => Technology and Development => Computer Programming => Topic started by: ElementCoder on June 08, 2012, 09:49:34 am

Title: JList Index
Post by: ElementCoder 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);
}
}
}
}
Title: Re: JList Index
Post by: jacobly 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.
Title: Re: JList Index
Post by: ElementCoder 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?
Title: Re: JList Index
Post by: jacobly 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();
         }
      });
   }
}

Title: Re: JList Index
Post by: ElementCoder on June 11, 2012, 04:56:05 am
Thanks, that example helped me. It now works fine.