Java JTabbedPane 立即切换选项卡

发布于 2024-11-01 17:40:30 字数 536 浏览 6 评论 0原文

我有一个带有两个选项卡的简单 JTabbedPane 组件。当在其中一个选项卡上触发按钮事件时,我希望立即将当前视图设置为另一个选项卡。我可以在相关按钮的 actionPerformed 方法中使用 setSelectedIndex(int) 成功地切换当前选项卡,但按钮事件的其余功能有时可能需要一段时间才能完成。

看来 setSelectedIndex(int) 更改不会立即发生,并且仅在由按钮单击/按下触发的 actionPerformed 方法的主体完成时才会发生。

我想在单击/按下按钮时切换选项卡的原因是,用户可以在另一个选项卡上的文本区域中看到一条消息,告诉他们“正在处理,请等待”等,而按钮的其余部分单击功能被执行。该功能的结果到达后将取代“...请稍候”消息。

现在发生的情况是,我单击按钮,它会冻结在该选项卡上,直到按钮功能完全完成,然后进行切换。因此,“...请稍候”消息实际上从未见过。

我不知道该怎么办。也许为“功能”创建一个新线程,并让按钮单击事件启动该线程,以便按钮事件可以在等待结果时完成并切换选项卡。 只是大声思考。任何帮助表示赞赏。

-抢

I have a simple JTabbedPane component with two tabs. When a button event is fired on one of the tabs, I want the current view to be set to the other tab immediately. I can successfully get the current tab to switch using setSelectedIndex(int) in the actionPerformed method of the button in question, but the remaining functionality of the button event can sometimes take a while to complete.

It would seem that the setSelectedIndex(int) change doesn't occur immediately, and only happens when the body of the actionPerformed method that was triggered by the button click/press has completed.

The reason that I want to switch tabs when the button is clicked/pressed, is so that the user can see a message in a text area on the other tab telling them "processing, please wait" etc. while the remainder of the button click functionality is carried out. The results of that functionality, when they arrive, will replace the "...please wait" message.

So what's happening now is that I click the button, it freezes on that tab until the button functionality has completed entirely, and then it switches. So the "...please wait" message is actually never even seen.

I'm not sure what to do. Create a new thread for the "functionality", maybe, and let the button click event kick off the thread so that the button event can finish and switch tabs while waiting for results.
Just thinking out loud. Any help is appreciated.

-Rob

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

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

发布评论

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

评论(1

别念他 2024-11-08 17:40:30

在我看来,好像你遇到的线程问题比什么都重要,这会冻结你的 GUI,使其在完成之前完全没有响应,从而延迟你的选项卡式窗格交换。如果您在操作执行方法中运行任何长时间运行的进程,请在后台线程(例如 SwingWorker 提供的)中执行它们,然后您的 GUI 将变得更加响应。

有关详细信息,请查看本教程:Swing 中的并发

如果尽管有此建议,您仍然遇到问题,那么您可能需要发布一些有问题的代码。最好的情况是您可以创建一个可重现问题的小型可编译和可运行应用程序,即 SSCCE

Que tengas suerte!

It sounds to me as if you have a threading issue more than anything, that this is freezing your GUI making it completely unresponsive until it completes, and thus delaying your tabbed pane swap. If you are running any long-running processes in your action performed method, do them instead in a background thread such as supplied by a SwingWorker, and then your GUI will become more responsive.

For details on this, please check out this tutorial: Concurrency in Swing

If despite this advice, you're still having problems, then you're likely going to have to post some of your offending code. Best would be if you could create a small compilable and runnable app that reproduces the problem, an SSCCE.

¡Que tengas suerte!

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