G
Guest
Gast
Ich habe mich mal an einer Progressbar versucht, leider aktualisiert sie während des Ablaufes nicht und steht dann am Ende bei 100 %.
Wie kann ich das ändern ?
Alles was mit ###### gekennzeichnet ist, hat mit der Progressbar zu tun.
Wie kann ich das ändern ?
Alles was mit ###### gekennzeichnet ist, hat mit der Progressbar zu tun.
Code:
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.sql.*;
import java.io.*;
public class Info extends JFrame implements ActionListener {
public JTextField Datenbank;
public JTextField Creator;
public JTextField Instnr;
public JTextField Hostname;
public JTextField User;
public JPasswordField Pass;
public JTextArea Fehlercode;
public JButton ok;
public JButton back;
public JButton abbruch;
public ResultSet rec;
public FileWriter fw;
public JProgressBar progressBar;
public int sqlanzahlint;
public int kontrolle;
public Thread runner;
public Info() {
// Panel-Deklaration
super("Reporting-Tool für Sys-Watch V 1.1 by Bastian Haas");
setLocation(320,230);
setSize(450,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
JPanel pane = new JPanel();
pane.setLayout(gridbag);
// Datenbank-Label
buildConstraints(constraints, 0, 0, 1, 1, 10, 40);
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.EAST;
JLabel datenbank = new JLabel("Datenbanksystem: ",JLabel.LEFT);
gridbag.setConstraints(datenbank, constraints);
pane.add(datenbank);
// Datenbank-Textfeld
buildConstraints(constraints, 1, 0, 1, 1, 90, 0);
constraints.fill = GridBagConstraints.HORIZONTAL;
Datenbank = new JTextField();
gridbag.setConstraints(Datenbank, constraints);
pane.add(Datenbank);
// Creator-Label
buildConstraints(constraints, 0, 1, 1, 1, 10, 40);
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.EAST;
JLabel creator = new JLabel("Creator: ",JLabel.LEFT);
gridbag.setConstraints(creator, constraints);
pane.add(creator);
// Creator-Textfeld
buildConstraints(constraints, 1, 1, 1, 1, 90, 0);
constraints.fill = GridBagConstraints.HORIZONTAL;
Creator = new JTextField("G000");
gridbag.setConstraints(Creator, constraints);
pane.add(Creator);
// Instnr-Label
buildConstraints(constraints, 0, 2, 1, 1, 10, 40);
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.EAST;
JLabel instnr = new JLabel("Institutsnummer: ",JLabel.LEFT);
gridbag.setConstraints(instnr, constraints);
pane.add(instnr);
// Instnr-Textfeld
buildConstraints(constraints, 1, 2, 1, 1, 90, 0);
constraints.fill = GridBagConstraints.HORIZONTAL;
Instnr = new JTextField();
gridbag.setConstraints(Instnr, constraints);
pane.add(Instnr);
// Hostname-Label
buildConstraints(constraints, 0, 3, 1, 1, 10, 40);
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.EAST;
JLabel hostname = new JLabel("Hostname: ",JLabel.LEFT);
gridbag.setConstraints(hostname, constraints);
pane.add(hostname);
// Hostname-Textfeld
buildConstraints(constraints, 1, 3, 1, 1, 90, 0);
constraints.fill = GridBagConstraints.HORIZONTAL;
Hostname = new JTextField();
gridbag.setConstraints(Hostname, constraints);
pane.add(Hostname);
// User-Label
buildConstraints(constraints, 0, 4, 1, 1, 10, 40);
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.EAST;
JLabel user = new JLabel("Username: ",JLabel.LEFT);
gridbag.setConstraints(user, constraints);
pane.add(user);
// User-Textfeld
buildConstraints(constraints, 1, 4, 1, 1, 90, 0);
constraints.fill = GridBagConstraints.HORIZONTAL;
User = new JTextField();
gridbag.setConstraints(User, constraints);
pane.add(User);
// Passwort-Label
buildConstraints(constraints, 0, 5, 1, 1, 10, 40);
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.EAST;
JLabel pass = new JLabel("Passwort: ",JLabel.LEFT);
gridbag.setConstraints(pass, constraints);
pane.add(pass);
// Passwort-Textfeld
buildConstraints(constraints, 1, 5, 1, 1, 90, 0);
constraints.fill = GridBagConstraints.HORIZONTAL;
Pass = new JPasswordField();
Pass.setEchoChar('*');
gridbag.setConstraints(Pass, constraints);
pane.add(Pass);
// OK-Button
buildConstraints(constraints, 1, 6, 1, 1, 0, 40);
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.WEST;
ok = new JButton("Abfrage");
gridbag.setConstraints(ok, constraints);
pane.add(ok);
// Zurücksetzen-Button
buildConstraints(constraints, 1, 6, 1, 1, 0, 40);
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.CENTER;
back = new JButton("Zurücksetzen");
gridbag.setConstraints(back, constraints);
pane.add(back);
// Abbruch-Button
buildConstraints(constraints, 1, 6, 1, 1, 0, 40);
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.EAST;
abbruch = new JButton("Beenden");
gridbag.setConstraints(abbruch, constraints);
pane.add(abbruch);
// Progress-Bar
buildConstraints(constraints, 1, 7, 1, 1, 0, 40);
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.WEST;
progressBar = new JProgressBar(0,2);
progressBar.setValue(0);
progressBar.setStringPainted(true);
gridbag.setConstraints(progressBar, constraints);
pane.add(progressBar);
// ActionListener für Abbruch- und OK-Button
ok.addActionListener(this);
abbruch.addActionListener(this);
back.addActionListener(this);
Fehlercode = new JTextArea();
setContentPane(pane);
setVisible(true);
}
public void actionPerformed(ActionEvent evt) {
Object Source = evt.getSource();
kontrolle=0;
if (Source == ok) {
// Verbindung mit dem Datenbanksystem
String data = "jdbc:odbc:"+Datenbank.getText();
// Verbindung mit dem Datenbanksystem
try {
// Verbinden mit der Datenbank
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection(data, User.getText(), Pass.getText());
Statement st = conn.createStatement();
// Verbinden mit der Datenbank
//--------------------------------------------------------------
// Öffnen der Properties.ini
Properties prop = new Properties();
prop.load(new BufferedInputStream(
new FileInputStream("properties.ini")));
// Öffnen der Properties.ini
//--------------------------------------------------------------
// Lesen der SQL-Abfragenanzahl
String sqlanzahl=prop.getProperty("Gesamtanzahl");
Integer aInt = new Integer(sqlanzahl);
sqlanzahlint = aInt.intValue();
String sql;
// Lesen der SQL-Abfragenanzahl
//--------------------------------------------------------------
fw = new FileWriter("Html/blau.htm");
fw.write( "<html><head><title></title></head><style><!--a{ text-decoration: none;color: white;"
+"font-size : 11pt; font-weight:bold; font-family: Arial }a:hover { color: #8CA2B5;}"
+"a:active { color: #8CA2B5; }// -->"
+"</style><body bgcolor=\"#003063\" topmargin=\"2\"><table border=\"0\" width=\"100%\">");
fw.close();
//--------------------------------------------------------------
for (int x=1;x<=sqlanzahlint;x++) {// Schleife für die SQL-Blockdurchläufe
progressBar.setValue(x);
// Lesen der SQL-spezifischen Überschrift + Spaltenüberschriften
String ueber= prop.getProperty("sql"+x+"-spaltenueberschrift");
String[] stringArray = ueber.split(",");
String ueberschrift=prop.getProperty("sql"+x+"-ueberschrift");
// Lesen der SQL-spezifischen Überschrift + Spaltenüberschriften
//----------------------------------------------------------
// Schreiben der Links
int zahl=100/sqlanzahlint;
fw = new FileWriter("Html/blau.htm",true);
fw.write("<tr><td><a href=\""+x+".htm\" target=\"main\">"+ueberschrift+"</a></td></tr><tr></tr>");
fw.close();
// Schreiben der Links
//----------------------------------------------------------
String zaehler1=prop.getProperty("sql"+x+"-where");
String[] r = zaehler1.split("PARM1");
int anzahl1 = r.length;
// SQL-spezifische Daten aus der Properties.ini + Replace
sql=prop.getProperty("sql"+x+"-select")
+" "+prop.getProperty("sql"+x+"-from")
+" "+prop.getProperty("sql"+x+"-where");
sql=sql.replaceAll( "CREATOR", Creator.getText() );
sql=sql.replaceAll( "PARM2", Instnr.getText() );
sql=sql.replaceAll( "PARM1", Hostname.getText() );
// SQL-spezifische Daten aus der Properties.ini + Replace
//--------------------------------------------------------------
// Zählen der SQL-spezifischen Spalten
String zaehler=prop.getProperty("sql"+x+"-spaltenueberschrift");
String[] w = zaehler.split(",");
int anzahl = w.length;
// Zählen der SQL-spezifischen Spalten
//--------------------------------------------------------------
if (anzahl1>=2)
{
// Schreiben des Dateikopfes der x.htm
fw = new FileWriter("Html/"+x+".htm");
fw.write("<html><head></head><body bgcolor=\"#D6E3EF\" text=\"#ffffff\"><center>"
+"<font face=Arial color=\"#003063\" size=\"4\" weigth=\"bold\">"
+"Abfrageergebnis für Host "+Hostname.getText()+" des Instituts "+Instnr.getText()
+"</font>
<table border=\"1\" bgcolor=\"#003063\">");
fw.close();
// Schreiben des Dateikopfes der x.
}
else
{
fw = new FileWriter("Html/"+x+".htm");
fw.write("<html><head></head><body bgcolor=\"#D6E3EF\" text=\"#ffffff\"><center>"
+"<table border=\"1\" bgcolor=\"#003063\">");
fw.close();
}
// Schreiben der SQL-spezifischen Spaltenüberschriften
for (int z=0;z<=anzahl-1;z++) { //anzahl-1
fw = new FileWriter("Html/"+x+".htm",true);
fw.write("<th>"+stringArray[z]+"</th>");
fw.close();
}
// Schreiben der SQL-spezifischen Spaltenüberschriften
//--------------------------------------------------------------
// Schreiben des SQL-spezifischen Überschriftenendes
fw = new FileWriter("Html/"+x+".htm",true);
fw.write("</tr>");
fw.close();
// Schreiben des SQL-spezifischen Überschriftenendes
//--------------------------------------------------------------
// Lesen der SQL-spezifischen Ausgaben
ResultSet rec = st.executeQuery(sql);
// Lesen der SQL-spezifischen Ausgaben
//--------------------------------------------------------------
// Schreiben der SQL-spezifischen Ausgaben
while(rec.next()) {
try {
fw = new FileWriter("Html/"+x+".htm",true);
fw.write("<tr>");
fw.close();
for (int i=1;i<=anzahl;i++) {
fw = new FileWriter("Html/"+x+".htm",true);
fw.write("<td>" +rec.getString(i)+"</td>");
fw.close();
}
fw = new FileWriter("Html/"+x+".htm",true);
fw.write("</tr>");
fw.close();
}
catch ( IOException e ) {
JOptionPane.showMessageDialog(null, "Konnte Datei nicht erstellen" );
}
finally {
try {
if ( fw != null ) fw.close();
}
catch (IOException e) {}
}
}
// Schreiben der SQL-spezifischen Ausgaben
//--------------------------------------------------------------
fw = new FileWriter("Html/"+x+".htm",true);
fw.write("</table></center></body></html>");
fw.close();
}// Ende der for-Schleife für die SQL-Block-Durchläufe
//--------------------------------------------------------------
// Schließen der Verbindung
st.close();
conn.close();
// Schließen der Verbindung
//--------------------------------------------------------------
// Abschließen der blau.htm
fw = new FileWriter("Html/blau.htm",true);
fw.write("</tr></table></body></html>");
fw.close();
// Abschließen der blau.htm
}
catch (SQLException s) {
Fehlercode.setText("Fehler:"+ s.getErrorCode() + " " + s.getSQLState());
JOptionPane.showMessageDialog(null, Fehlercode.getText());
}
catch (Exception e) {
Fehlercode.setText("Error: " + e.toString() + e.getMessage());
JOptionPane.showMessageDialog(null, Fehlercode.getText());
}
}
else {
// Zurücksetzen der Eingaben
if (Source == back) {
Creator.setText("");
Instnr.setText("");
Hostname.setText("");
User.setText("");
Pass.setText("");
Datenbank.setText("");
// Zurücksetzen der Eingaben
}
else
// Programm Beenden
System.exit(0);
// Programm Beenden
}
}
void buildConstraints(GridBagConstraints gbc, int gx, int gy, int gw, int gh, int wx, int wy) {
gbc.gridx = gx;
gbc.gridy = gy;
gbc.gridwidth = gw;
gbc.gridheight = gh;
gbc.weightx = wx;
gbc.weighty = wy;
}
public static void main(String[] arguments) {
Info in= new Info();
in.show();
}
}