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.JFrame
as superclass. JOptionPane
is 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. JDialog
will allow you to add buttons as you please, where as a standalone JOptionPane
will require you to use Yes/No, or Yes/No/Cancel or OK/Cancel etc.JOptionPane
, and only show a cancel button, you can instantiate a JOptionPane
(as opposed to using the utility show*
methods), with the progressbar as the message
, and the JOptionPane.CANCEL_OPTION
as the optionType
param. You will still need to put this into a JDialog
to make it visible. See this tutorial for more details:JOptionPane (constructor)
Creates a JOptionPane with the specified buttons, icons, message, title, and so on. You must then add the option pane to a JDialog, register a property-change listener on the option pane, and show the dialog. See Stopping Automatic Dialog Closing for details.