多个JList选择的索引相互分离

时间:2022-10-05 11:27:41

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.

当我第一次选择它们没问题。

多个JList选择的索引相互分离

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!

如果我改变第二行,第三行重新填充(在这种情况下所有行具有相同数量的席位)它不会破坏!

多个JList选择的索引相互分离

However if I change the first row everything breaks!

但是,如果我改变第一行,一切都会破裂!

多个JList选择的索引相互分离

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. It should just clear the other two and that's that. Why am I wrong ?

据我所知,当我点击第一列中的某个项目时,第二列的valueChanged根本不应该触发。它应该清除其他两个就是那个。为什么我错了?

P.S. First Code snippet is responsible for the second one breaking the program.

附: 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) - 第一列时,我怎样才能安全地清除所有内容?

1 个解决方案

#1


2  

The first listener clears the selection of 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. And in the second listener, you're trying to call a method on the selected value of List_Rand. Since you just cleared the selection, there's no selected value anymore, hence the NullPointerException.

第一个侦听器清除List_Rand的选择。这使得选择改变:它从“选择索引i”到“没有选择索引”,因此调用第二个监听器。在第二个侦听器中,您尝试在List_Rand的选定值上调用方法。由于您刚刚清除了选择,因此没有选定的值,因此NullPointerException。

Side note: your code is very hard to read, because it doesn't respect the Java naming conventions. Variables start with a lowercase letter, and are camelCased (no underscore in their name).

旁注:您的代码很难阅读,因为它不遵守Java命名约定。变量以小写字母开头,并且是camelCased(名称中没有下划线)。

Other side note : what's the point in calling parseInt(selectedValue.toString())? If the list contains Integer instances, the cast the value to Integer directly. If it contains String, then why not store Integers instead, since this is what you want the list to contain?

另外注意:调用parseInt(selectedValue.toString())有什么意义?如果列表包含Integer实例,则将值直接转换为Integer。如果它包含String,那么为什么不存储Integers,因为这是你希望列表包含的内容?

#1


2  

The first listener clears the selection of 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. And in the second listener, you're trying to call a method on the selected value of List_Rand. Since you just cleared the selection, there's no selected value anymore, hence the NullPointerException.

第一个侦听器清除List_Rand的选择。这使得选择改变:它从“选择索引i”到“没有选择索引”,因此调用第二个监听器。在第二个侦听器中,您尝试在List_Rand的选定值上调用方法。由于您刚刚清除了选择,因此没有选定的值,因此NullPointerException。

Side note: your code is very hard to read, because it doesn't respect the Java naming conventions. Variables start with a lowercase letter, and are camelCased (no underscore in their name).

旁注:您的代码很难阅读,因为它不遵守Java命名约定。变量以小写字母开头,并且是camelCased(名称中没有下划线)。

Other side note : what's the point in calling parseInt(selectedValue.toString())? If the list contains Integer instances, the cast the value to Integer directly. If it contains String, then why not store Integers instead, since this is what you want the list to contain?

另外注意:调用parseInt(selectedValue.toString())有什么意义?如果列表包含Integer实例,则将值直接转换为Integer。如果它包含String,那么为什么不存储Integers,因为这是你希望列表包含的内容?