如果在 DefaultListModel 中选择之前单击按钮,可以防止崩溃吗?

发布于 2024-12-28 13:56:38 字数 292 浏览 4 评论 0原文

我使用 DefaultListModel 在列表中选择不同的名称。要选择名称,用户单击列表中的名称之一,使其突出显示,然后按按钮确认所选名称。我的问题是,如果用户在列表中选择名称之前单击按钮,则会崩溃!

到目前为止,我已经使用了这段代码,但还不足以防止崩溃

if(event.getSource() == buttonSelectCustomer && model1.getSize() > 0)

如果用户在没有任何选择的情况下单击按钮,是否有办法防止崩溃?谢谢!

I'm using a DefaultListModel to select different names in a list. To select a name the user click on one of the name in the list so it get highlited and then confirm the selected name by pressing a button. My problem is if the user click on the button before a name is selected in the list, then it crash!

So far I have used this code, but it's not enough to prevent an crash

if(event.getSource() == buttonSelectCustomer && model1.getSize() > 0)

Is there a way to prevent a crash if user click the button without any selection? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

郁金香雨 2025-01-04 13:56:38

将此添加到您的条件(list.getSelectedIndex() != -1)

Add this to you condition (list.getSelectedIndex() != -1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文