Jump to content

Confused, need java help

Go to solution Solved by dannytech357,

Just found a workaround, removed the button and instead saved it on selection change using:

((JList)e.getSource()).getSelectedIndex()

which for some reason works. Thanks for the help and if there are other solutions or you know why it didn't work, add a post.

I'm trying to write some code that takes the selected index of a JList and sets another variable to it, but getSelectedIndex() always returns 0.

private static final long serialVersionUID = 1L;	JList<Object> resourcePacks = new JList<>(Resources.getResourcePacks().toArray());	JList<Object> languages = new JList<>(Resources.getLanguages().toArray());	JButton saveSettings = new JButton(Resources.getLocalizedString("vanilla.lang.saveandclose"));	public ResourcesPage() {				this.setLayout(new BorderLayout());		JPanel resourcesPanel = new JPanel(new BorderLayout());		resourcesPanel.setBorder(BorderFactory.createTitledBorder(Resources.getLocalizedString("vanilla.lang.resourcepacks")));		resourcePacks.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);		resourcePacks.setSelectedIndex(Resources.resourcePackIndex);		resourcesPanel.add(resourcePacks, BorderLayout.CENTER);		JPanel languagePanel = new JPanel(new BorderLayout());		languagePanel.setBorder(BorderFactory.createTitledBorder(Resources.getLocalizedString("vanilla.lang.languages")));		languages.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);		languages.setSelectedIndex(Resources.languageIndex);		languagePanel.add(languages, BorderLayout.CENTER);		this.add(resourcesPanel, BorderLayout.NORTH);		this.add(languagePanel, BorderLayout.CENTER);		this.add(saveSettings, BorderLayout.SOUTH);		saveSettings.addActionListener(new ActionListener() {			@[member=OverRide]			public void actionPerformed(ActionEvent e) {				Resources.resourcePackIndex = resourcePacks.getSelectedIndex(); //ALWAYS RETURNS 0				Console.addMessage(String.valueOf(Resources.resourcePackIndex)+":"+String.valueOf(resourcePacks.getSelectedIndex())+"-"+String.valueOf(Resources.languageIndex)+":"+String.valueOf(languages.getSelectedIndex())); //TEST, ALWAYS RETURNS 0:0-0:0, EVEN WHEN 2ND ITEM OF EITHER LIST IS SELECTED				Settings.setSetting("vanilla.setting.resourcepackindex", String.valueOf(resourcePacks.getSelectedIndex()));				Resources.languageIndex = languages.getSelectedIndex();				Settings.setSetting("vanilla.setting.languageindex", String.valueOf(languages.getSelectedIndex()));				Settings.saveSettings();				//ResourcesPage.setVisible(false);			}		});		resourcePacks.addMouseListener(new MouseListener() {						@[member=OverRide]			public void mouseReleased(MouseEvent e) {				// TODO Auto-generated method stub							}						@[member=OverRide]			public void mousePressed(MouseEvent e) {				// TODO Auto-generated method stub							}						@[member=OverRide]			public void mouseExited(MouseEvent e) {				// TODO Auto-generated method stub							}						@[member=OverRide]			public void mouseEntered(MouseEvent e) {				// TODO Auto-generated method stub							}						@[member=OverRide]			public void mouseClicked(MouseEvent e) {				Resources.setResourcePack(resourcePacks.getSelectedIndex());				Console.addMessage("Switched resource pack to id: "+resourcePacks.getSelectedIndex()+", name: "+Resources.getResourcePacks().get(resourcePacks.getSelectedIndex()));				reloadIndices();			}		});	}

(Sorry for the messy code, in a hurry)

˙ǝɯᴉʇ ɹnoʎ ƃuᴉʇsɐʍ ǝɹɐ noʎ 'sᴉɥʇ pɐǝɹ oʇ ƃuᴉʎɹʇ ǝɹɐ noʎ ɟI

Link to comment
https://linustechtips.com/topic/473509-confused-need-java-help/
Share on other sites

Link to post
Share on other sites

I don't see any problem with your code upfront. I haven't used JList, only JTable, which consists of a similar concept. When I ran into problems with JTable, I first tried to isolate the problem by extracting the problematic code and putting it into a test class. Then I would remove parts of the code until it looked similar to example code - in your case, example code would be found here: http://docs.oracle.com/javase/tutorial/uiswing/components/list.html

 

I checked to see if getSelectedIndex worked with the example code, and if it worked with stripped down test code. If both work, then I gradually introduced original code back into the test code, checking to see if getSelectedIndex still worked. This way, I could isolate where exactly the issues were occurring. After you isolate the location of the problem you can explore modifying things related to that location; maybe the ListSelectionModel, the ListSelectionModel's ListSelectionMode, checking the number of ListSelectionListeners, etc. If I isolated it to a single method or class, then I looked at that method's source code in the JDK. 

 

That's the best I can do. Oh, and by the way, is there any reason you're using MouseListener instead of ListSelectionListener? Seems like ListSelectionListener was what you were looking for. 

Link to comment
https://linustechtips.com/topic/473509-confused-need-java-help/#findComment-6346234
Share on other sites

Link to post
Share on other sites

Just found a workaround, removed the button and instead saved it on selection change using:

((JList)e.getSource()).getSelectedIndex()

which for some reason works. Thanks for the help and if there are other solutions or you know why it didn't work, add a post.

˙ǝɯᴉʇ ɹnoʎ ƃuᴉʇsɐʍ ǝɹɐ noʎ 'sᴉɥʇ pɐǝɹ oʇ ƃuᴉʎɹʇ ǝɹɐ noʎ ɟI

Link to comment
https://linustechtips.com/topic/473509-confused-need-java-help/#findComment-6347062
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×