在jpanels的arraylist中获取某个容器的索引号 [英] Get index number of a certain container in arraylist of jpanels

查看:133
本文介绍了在jpanels的arraylist中获取某个容器的索引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一个类似于indexOf()的ArrayLists方法可以告诉我特定容器的元素编号是多少.

说我有一个保存字符串的ArrayList:

ArrayList<String> stringHolder = new ArrayList<>();

如果我要查找包含"example"的字符串的元素编号,则只需调用:

stringHolder.indexOf("example");

找到我的答案.

您如何通过JPanels执行此操作?特别是如果所有容器实际上都相同.我需要能够通过在特定容器中按按钮来从其父面板中删除该容器.动作侦听器将从面板中删除容器.但是,我需要知道该特定容器的元素编号. TIA.

解决方案

要查找容器中保存的组件的索引",您需要通过调用getComponents()从容器中获取组件数组,然后找到您的容器所需的组件,您需要遍历数组,进行测试以查看是否已获得所需的内容.

请注意,您可以使用静态Arrays.binarySearch(Object[] a, Object key)方法来实现此目的,但也请注意,为了使其正常工作,您的JPanel应该重写equals和hashCode,以便此方法可以工作,以便键与JPanel匹配感兴趣,而不必掌握感兴趣的部分.

请注意,以这种方式获取对组件的引用是相当笨拙"的,这意味着存在导致易碎程序的风险,这些程序比您想要的更容易形成错误.最好有其他更简洁的方式来获取参考,例如将关键组件放置在ArrayList或Map中.


对于您的程序,您已经具有对单击的JPanel的引用.由于JCheckBox由该JPanel持有,因此可以通过其侦听器的getSource()方法获取对JCheckBox的引用,然后通过在JCheckBox上调用getParent()来获取对其父"容器JPanel的引用. /p>

I was wondering if there was a method for ArrayLists, similar to indexOf(), that could tell me what the element number of a particular container was.

Say I had an ArrayList that held Strings:

ArrayList<String> stringHolder = new ArrayList<>();

If I were looking for the element number of the String that contained "example", I would just call:

stringHolder.indexOf("example");

to find my answer.

How can you do this wth JPanels? Especially if all of the containers are virtually the same. I need to be able to remove a specific container from its parent panel by pressing a button within that container. The action listener would remove the container from the panel. But, I need to know the element number of that particular container. TIA.

解决方案

To find the "index" of a component held in a container, you need to get the component array from container by calling getComponents(), then to find your desired component, you need to iterate through the array, testing to see if you've gotten what you want.

Note that you could use the static Arrays.binarySearch(Object[] a, Object key) method to achieve this, but also note that for this to work well, your JPanel should override equals and hashCode so that this method will work, so that the key will match the JPanel of interest, without having to already have the component of interest in hand.

Note that obtaining a reference to a component this way is fairly "kludgy" meaning it risks leading to brittle programs that can form bugs more easily than you want. Better to have other cleaner ways of getting your reference, such as placing your key components in an ArrayList or Map.


Edit: for your program, you already have a reference to the clicked JPanel. Since the JCheckBox is held by that JPanel, you can get a reference to the JCheckBox via its listener's getSource() method, and then get a reference to its "parent" container, the JPanel, by calling getParent() on the JCheckBox.

这篇关于在jpanels的arraylist中获取某个容器的索引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆