I wrote a simple application and I want show delay of it with JProgressBar Plese help me ;
I want show JProgressBar with Joptionpane , with a cancel button and it should be modal
this is my source code :
class CustomFrame extends JFrame {
private JProgressBar progressBar;
public CustomFrame() {
long start = System.currentTimeMillis();
myMethod();
this.getContentPane().setLayout(null);
this.setSize(200, 200);
//JOptionPane. ?????
this.setTitle("JFrame");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
long end = System.currentTimeMillis();
System.out.print("\nTime: " + (end - start));
}
public void myMethod(){
try {
java.io.File file = new java.io.File("i://m.txt");
BufferedReader input =
new BufferedReader(new FileReader(file));
String line;
while ((line = input.readLine()) != null) {
if (line.indexOf("CREATE KGCGI=") != -1 ){
System.out.println(line);
}
}
input.close();
}
catch(Exception e){
e.printStackTrace();
}
}
Thanks …
There are a couple things that you will need to do to get this to work:
file.length())to determine how to scope your progress bar (myProgressBar.setMaximum(length))myProgressBar.setValue(myProgressBar.getValue()+lineLength)).A couple points by way of critique:
init()method.JFrameas superclass.JOptionPaneis a class that will pop up a very basic modal dialog with some text, maybe an icon or input field. It isnt a panel that is embedded in a dialog.JDialog, which can also be made modal.JDialogwill allow you to add buttons as you please, where as a standaloneJOptionPanewill require you to use Yes/No, or Yes/No/Cancel or OK/Cancel etc.JOptionPane, and only show a cancel button, you can instantiate aJOptionPane(as opposed to using the utilityshow*methods), with the progressbar as themessage, and theJOptionPane.CANCEL_OPTIONas theoptionTypeparam. You will still need to put this into aJDialogto make it visible. See this tutorial for more details: