简体   繁体   English

多个JList选定索引彼此中断

[英]Multiple JList selected indexes breaking each other

I have a simple interface that is designed to select one or more seats in a theater and reserve them for yourself. 我有一个简单的界面,旨在选择剧院中的一个或多个座位,并为自己保留座位。

I have three columns: zone / row / seat. 我有三列:区域/行/座位。 You select the first to populate the second, and the second to populate the third. 您选择第一个填充第二个,然后选择第二个填充第三个。

When I select them the first time no problem. 当我第一次选择它们时,没问题。

首次在所有三列中选择一个项目

If I change the second row, the third row re-populates (in this case all rows have the same number of seats) it does not break! 如果更改第二行,则第三行会重新填充(在这种情况下,所有行都具有相同的座位数),它不会中断!

在第二栏中选择了一个新项目

However if I change the first row everything breaks! 但是,如果我更改第一行,一切都会中断!

在第一栏中选择了一个新项目

Now the reason for this is kinda clear, but I don't understand exactly why this is. 现在,这样做的原因已经很清楚了,但我不知道为什么会这样。

This is the first list event trigger: 这是第一个列表事件触发器:

List_Zona.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent arg0) {
                if (! arg0.getValueIsAdjusting()){

                    RowListModel.clear(); // second list
                    SeatListModel.clear(); // third list
                    List_Rand.clearSelection(); // second list
                    List_Scaun.clearSelection(); // third list

                    int[] rows = self.repository.getRowsForZone(
                              List_Zona.getSelectedValue().toString()
                    );
                    int index = 0;
                    while (rows[index]!=0) {
                          RowListModel.addElement(String.valueOf(rows[index]));
                          index++;
                    }
                }
            }
        });

It should clear the other column selections so it should not interfere with this next trigger (second column), but apparently it does, or something happens which I don't get: 它应该清除其他列的选择,以免干扰下一个触发器(第二列),但显然可以,或者发生某些我没有得到的事情:

List_Rand.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent arg0) {
                if (! arg0.getValueIsAdjusting()){

                    SeatListModel.clear();

                    int[] seats = self.repository.getSeatByZoneAndRow(
                          List_Zona.getSelectedValue().toString(),
                          Integer.parseInt(List_Rand.getSelectedValue().toString())//HERE
                    );
                    int index = 0;
                    while (seats[index]!=0) {
                        SeatListModel.addElement(String.valueOf(seats[index]));
                        index++;
                    }
                }
            }
        });

It can't parse the integer because the second column should be cleared, but it's not ? 它不能解析整数,因为应该清除第二列,但是不是吗? But even though it's not ... it is ? 但是,即使不是……是吗?

What I'm trying to say: The second column should be clear (it's not) but if it's not then the error should not occur, but it does. 我想说的是:第二列应该是清楚的(不是),但是如果不是,则不应发生错误,但确实可以。

I hope this makes some sense! 我希望这有道理!

Can anyone spot the problem ? 谁能发现问题? Should I provide more code ? 我应该提供更多代码吗?

The error is a NullPointerException at the second column, because something fishy is happening there (again: at the integer parsing). 错误是第二列的NullPointerException,因为那里发生了一些混乱(再次:在整数分析中)。

By my mind the second column's valueChanged should not trigger at all when I click an item in the first column. 我认为 ,当我单击第一列中的项目时,第二列的valueChanged根本不会触发。 It should just clear the other two and that's that. 它应该只清除其他两个,就是这样。 Why am I wrong ? 我为什么错了?

PS First Code snippet is responsible for the second one breaking the program. PS First Code片段负责破坏程序的第二个。

Maybe I should also rephrase the question How can I safely clear everything when I re-select a new "Zone" (Zona) - Column one ? 也许我也应该改写这个问题,当我重新选择一个新的“区域”(Zona)-第一列时,如何安全地清除所有内容?

The first listener clears the selection of List_Rand . 第一个侦听器清除List_Rand的选择。 That makes the selection change: it goes from "the index i is selected" to "no index is selected", so the second listener is invoked. 这使选择发生变化:它从“选择索引i”到“未选择索引”,因此调用了第二个侦听器。 And in the second listener, you're trying to call a method on the selected value of List_Rand . 在第二个侦听器中,您尝试对List_Rand的选定值调用一个方法。 Since you just cleared the selection, there's no selected value anymore, hence the NullPointerException. 由于您只是清除选择,因此不再有选择的值,因此NullPointerException。

Side note: your code is very hard to read, because it doesn't respect the Java naming conventions. 旁注:您的代码很难阅读,因为它不遵守Java命名约定。 Variables start with a lowercase letter, and are camelCased (no underscore in their name). 变量以小写字母开头,并且被驼峰化(名称中没有下划线)。

Other side note : what's the point in calling parseInt(selectedValue.toString()) ? 其他说明:调用parseInt(selectedValue.toString())什么意义? If the list contains Integer instances, the cast the value to Integer directly. 如果列表包含Integer实例,则将值直接转换为Integer。 If it contains String, then why not store Integers instead, since this is what you want the list to contain? 如果它包含String,那么为什么不存储Integers,因为这是您希望列表包含的内容?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM