在 Swing 组件和工作线程之间传递数据 [英] Passing data between swing components and worker Threads

查看:33
本文介绍了在 Swing 组件和工作线程之间传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试确定在 Swing 组件和线程之间传递数据和更新的最佳方式.下面是我面临的一个例子

I am trying to determine the best way to pass data and updates between swing components and threads. Below is an example of what I am faced with

这是用户与之交互的主窗口,它包含一个 JTabbedPanel 和一个 JPanel 作为选项卡面板的选项卡.它还包含一个 JTextPane 用于向用户显示一些信息.

This is the main windows that the user interacts with, it holds a JTabbedPanel with a single JPanel as a tab of the tabbed panel. It also holds a JTextPane that is used to show the user some information.

public class MainView extends JFrame
{
    JTextPane m_jtOutput;
    JTabbedPane m_jtpTabs;

    JSortPanel m_jpSortTab;

    public LinkedList<DataPoint> data;
    DataPoint curPoint;

    public MainView()
    {
        m_jtpTabs = new JTabbedPane();
        m_jpSortTab = new JSortPanel(this);
        m_jtpTabs.addTab("Sort", m_jpSortTab);
        this.add(m_jtpTabs);
        this.add(m_jtOutPut);
    }

    public void newStatus(String inStat)
    {
        m_jtOutput.setText(inStat);
    }

    public DataPoint getCurPoint()
    {
        return curPoint;
    }
}

这是在主视图中的 JTabbedPanel 中显示的 JPanel.它有一个工作线程对数据进行一些排序.它还向主视图输出文本窗格写入一些更新.

This is the JPanel that is shown in the JTabbedPanel within the main view. It has a worker thread that does some sorting on the data. It also writes some updates to the main views output text pane.

 public class JSortPanel extends JPanel
 {
     JButton doSort; 
     MainView mainVw;
     SortThread sortThread;

     public JSortPanel(MainView inView)
     {
         mainVw = inView;
         this.add(doSort); // also get the listener hooked up
         sortThread = new SortThread(mainView);
     }

     public void doSortClicked(EventArgs e)
     {
         sortThread.start();
         mainView.newStatus("Started sorting");
     }
 }

这是排序线程.它最初从主视图读取数据,然后对其进行排序,定期将一些有关排序状态的信息写入主视图文本窗格.

This is the sorting thread. It initially reads the data from the main view and then sorts it, periodically writing some information to the main views text pane about the state of the sorting.

public class SortThread extends Thread
{
    MainView mainVw;
    LinkedList<DataPoint> data;

    public SortThread(MainView inView)
    {
        mainVw = inView;
        System.out.println("Cur data " + mainView.getCurData());
    }

    public void run()
    {
        data = mainVw.data;
        while(true)
        {
            doASortingStep(data);
            mainVw.newStatus("Did a sorting step");
        }
    }
}

我的问题是.

  1. JSortPanel 可以安全地调用 mainView.newStatus() 吗?它是否只能在事件处理程序的范围内调用它,例如当用户单击 doSort 按钮时?

  1. Can JSortPanel call mainView.newStatus() safely? Can it only call it in the scope of an event handler, such as when a user clicks the doSort button?

SortThread 可以安全地调用 mainView.newStatus() 吗?这个电话会在 Swing EDT 上进行吗?

Can SortThread call mainView.newStatus() safely? Would that call be on the Swing EDT?

可以 SortThread 调用 mainView.getCurData() 并安全地获取返回值,我假设它也要么在 EDT 上,要么不在基于答案到上一个问题?

Can SortThread call mainView.getCurData() and get the return value safely, I assume it is also either on the EDT or not based on the answer to the previous question?

如果所有电话都在 EDT 上,您能举一个不在 EDT 上的电话的例子吗?

If all calls are on the EDT, can you give an example of a call that would not be on the EDT?

如果JSortPanelSortThread 中的一个或两个都不能安全地调用mainView.newStatus(..),那么他们怎么能呢?

If either or both JSortPanel and SortThread cannot call mainView.newStatus(..) safely, then how can they?

推荐答案

在一个组件在屏幕/其他设备上可视化之前,多线程通常是安全的.显示组件后,所有更改都应通过 SwingWorker 或 SwingUtilities.invokeLater 在 UIThread 上完成

Before a component is visualized on the screen / other device, it is generally safe for multi-threading. After a component is shown, all changes should be done on the UIThread via a SwingWorker or SwingUtilities.invokeLater

这篇关于在 Swing 组件和工作线程之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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