简体   繁体   中英

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.

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

Suggestions:

  • Do any file input/output in a background thread so as not to tie up the Swing event thread.
  • A SwingWorker would work nicely for your background thread.
  • 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.
  • 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.
  • 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.

If you need more detailed help, consider creating and posting a minimal example program

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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