Omnimaga: The Coders Of Tomorrow
Welcome, Guest. Please login or register.
 
Omnimaga: The Coders Of Tomorrow
20 May, 2013, 05:36:46 *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   home   news downloads projects tutorials misc forums rules new posts irc about Login Register  
+-OmnomIRC

You must Register, be logged in and have at least 40 posts to use this shout-box! If it still doesn't show up afterward, it might be that OmnomIRC is disabled for your group or under maintenance.

Note: You can also use an IRC client like mIRC, X-Chat or Mibbit to connect to an EFnet server and #omnimaga.

Pages: [1]   Go Down
  Print  
Author Topic: JList Index -  (Read 289 times) Bookmark and Share
0 Members and 1 Guest are viewing this topic.
ElementCoder
LV6 Super Member (Next: 500)
******
Offline Offline

Gender: Male
Last Login: Yesterday at 20:13:24
Date Registered: 22 May, 2011, 17:28:58
Location: Netherlands, Drachten
Posts: 400


Topic starter
Total Post Ratings: +22

View Profile
« on: 08 June, 2012, 15:49:34 »
0

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:

1
DefaultListModel kubesN = new DefaultListModel();

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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:

1
2
3
4
5
6
7
8
9
10
private static class ListListener implements ListSelectionListener{
public void valueChanged(ListSelectionEvent e){
if (!e.getValueIsAdjusting()){
int index = kube.getSelectedIndex();
System.out.println(index);
}
}
}
}
Logged

Calculators owned: TI-Nspire ClickPad (no batteries though Tongue); TI-Nspire CX
Known languages: TI BASIC (Nspire); Java; Lua; C++
Spoiler for Random stuff:

NerdTests.com says I'm a Nerd God.  Click here to take the Nerd Test, get nerdy images and jokes, and write on the nerd forum!


Omni search is down, use http://megapowers.net/v/search.htm instead.
jacobly
LV4 Regular (Next: 200)
****
Online Online

Last Login: Today at 05:21:49
Date Registered: 09 October, 2011, 01:53:09
Posts: 199

Total Post Ratings: +149

View Profile
« Reply #1 on: 08 June, 2012, 19:28:04 »
0

How does this kube

1
JList kube = new JList(kubes);
become this one?

1
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.
Logged
ElementCoder
LV6 Super Member (Next: 500)
******
Offline Offline

Gender: Male
Last Login: Yesterday at 20:13:24
Date Registered: 22 May, 2011, 17:28:58
Location: Netherlands, Drachten
Posts: 400


Topic starter
Total Post Ratings: +22

View Profile
« Reply #2 on: 09 June, 2012, 11:50:07 »
0

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: 09 June, 2012, 11:51:00 by ElementCoder » Logged

Calculators owned: TI-Nspire ClickPad (no batteries though Tongue); TI-Nspire CX
Known languages: TI BASIC (Nspire); Java; Lua; C++
Spoiler for Random stuff:

NerdTests.com says I'm a Nerd God.  Click here to take the Nerd Test, get nerdy images and jokes, and write on the nerd forum!


Omni search is down, use http://megapowers.net/v/search.htm instead.
jacobly
LV4 Regular (Next: 200)
****
Online Online

Last Login: Today at 05:21:49
Date Registered: 09 October, 2011, 01:53:09
Posts: 199

Total Post Ratings: +149

View Profile
« Reply #3 on: 09 June, 2012, 22:39:03 »
0

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();
         }
      });
   }
}

Logged
ElementCoder
LV6 Super Member (Next: 500)
******
Offline Offline

Gender: Male
Last Login: Yesterday at 20:13:24
Date Registered: 22 May, 2011, 17:28:58
Location: Netherlands, Drachten
Posts: 400


Topic starter
Total Post Ratings: +22

View Profile
« Reply #4 on: 11 June, 2012, 10:56:05 »
0

Thanks, that example helped me. It now works fine.
Logged

Calculators owned: TI-Nspire ClickPad (no batteries though Tongue); TI-Nspire CX
Known languages: TI BASIC (Nspire); Java; Lua; C++
Spoiler for Random stuff:

NerdTests.com says I'm a Nerd God.  Click here to take the Nerd Test, get nerdy images and jokes, and write on the nerd forum!


Omni search is down, use http://megapowers.net/v/search.htm instead.
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by EzPortal
Powered by MySQL Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Powered by PHP
Page created in 0.228 seconds with 31 queries.
Skin by DJ Omnimaga edited from SMF default theme with the help of tr1p1ea.
All programs, games and songs avaliable on this website are property of their respective owners.
Best viewed in Opera, Firefox, Chrome and Safari with a resolution of 1024x768 or above.