简体   繁体   English

如何将JProgressBar与其他组件相关联?

[英]How can I relate a JProgressBar with other component?

I need to show the progress bar when i click in a button and the progress is related with the information that shows a jtextpane. 当我单击按钮时,我需要显示进度栏,并且进度与显示jtextpane的信息有关。

this is the method of the progress bar 这是进度条的方法

private JProgressBar getMethodsjProgressBar() {
    if (methodsjProgressBar == null) {
        methodsjProgressBar = new JProgressBar();
        methodsjProgressBar.setVisible(false);
        methodsjProgressBar.setStringPainted(true);
        methodsjProgressBar.setBounds(new Rectangle(20, 244, 103, 22));
        methodsjProgressBar.setString("Cargando...");
    }
    return methodsjProgressBar;
}

and this one is the button 这是一个按钮

private JButton getLoadMethodsButton() {
    if (loadMethodsButton == null) {
        loadMethodsButton = new JButton();
        loadMethodsButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                FileChooserDirectory.getInstance().getAddress();
                @SuppressWarnings("unused")
                LoadClass loadClass = new LoadClass();
                methodsjProgressBar.setVisible(true);
            }
        });
        loadMethodsButton.setText("Buscar clase");
        loadMethodsButton.setBounds(237, 255, 126, 26);
    }
    return loadMethodsButton;
}

i need to know what i have to do to change the jprogressbar value when i'm loading the class 我在加载类时需要知道更改jprogressbar值的方法

Suggestions: 意见建议:

  • Do any file input/output in a background thread so as not to tie up the Swing event thread. 在后台线程中执行任何文件输入/输出,以免占用Swing事件线程。
  • A SwingWorker would work nicely for your background thread. SwingWorker可以很好地为您的后台线程工作。
  • One way to monitor the reading in of information is to use a ProgressMonitorInputStream which will display a dialog if the reading in through the Stream takes any appreciable time. 监视信息读入的一种方法是使用ProgressMonitorInputStream ,如果通过Stream的读入花费了可观的时间,它将显示一个对话框。
  • Otherwise if you want to use a JProgressBar, then inside the SwingWorker, set the progress property via setProgress(...) to a value between 0 and 100, add a PropertyChangeListener to your SwingWorker and listen for changes to the progress property, and then use this data to update your JProgressBar. 否则,如果要使用JProgressBar,则在SwingWorker内部,通过setProgress(...)将progress属性设置为0到100之间的值,向您的SwingWorker添加PropertyChangeListener并侦听对progress属性的更改,然后使用此数据更新您的JProgressBar。
  • A side recommendation: Avoid using null layouts and setBounds(...) since this will result in very rigid GUI's that are difficult to update or maintain. 附带建议:避免使用null布局和setBounds(...)因为这将导致非常僵化的GUI,难以更新或维护。

If you need more detailed help, consider creating and posting a minimal example program 如果您需要更多详细的帮助,请考虑创建并发布一个最小的示例程序

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

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