Logindialog

nieselfriem

Bekanntes Mitglied
Hallo!

Ich möchte ein Logindialog verwenden, der als erstes Angezeigt wird bevor das hauptfenster der Anwendung startet. Das Fenster der Anwendung soll erst nach einer Erfolgreichen Anmeldung geöffnet werden.

dazu habe ich folgenden Dialog;

Java:
package georgdb;

public class JLoginDialog extends javax.swing.JDialog {
    private String password = "geheim";
    private boolean succeeded = false;

    public JLoginDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
  
    private void initComponents() {

        jTextField1 = new javax.swing.JTextField();
        jPasswordField1 = new javax.swing.JPasswordField();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(86, 86, 86)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jButton1)
                    .addComponent(jPasswordField1, javax.swing.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE)
                    .addComponent(jTextField1))
                .addContainerGap(193, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(105, 105, 105)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addContainerGap(96, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        if (password.equals(jPasswordField1.getText())) {
            System.out.println("Vergleich");
            succeeded = true;
            System.out.println(succeeded);
        }
        
    }

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
    }
  
    public boolean getOk() {
           return succeeded;
    }

    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JPasswordField jPasswordField1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration

}

und die dazugehörige Main

Java:
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        
        DBPanel db = new DBPanel();
        JLoginDialog login = new JLoginDialog(db, true);
        login.setVisible(true);//Dialog öffnen
        if(login.getOk()) {//wenn Anmeldung erfolgreich
            login.setVisible(false);//Schliesse den Dialog
            db.setVisible(true);//Öffne das Hauptfenster
        }
    }
}

Jedoch hinkt die Herangeheinsweise. In der Mein wird die If-Anweisung nicht ausgeführt, wenn ich den OK-Button im Dialog drücke. Kann mir jemand einen Rat geben wie ich da besser an das Thema herangehe.

Gruß niesel
 

Antoras

Top Contributor
Du darfst die Abfrage natürlich erst ausführen nachdem sich der Benutzer versucht hat einzuloggen. Momentan wird sie sofort nach dem Anzeigen des Dialogs ausgeführt, weshalb natürlich nur die Initialisierungswerte gelesen werden können.
 

nieselfriem

Bekanntes Mitglied
Habe es wie folgt gelöst

Java:
public class JLoginDialog extends javax.swing.JDialog {
    private String password = "geheim";
    private boolean succeeded = false;
    private DBPanel db;

    /** Creates new form JLoginDialog */
    public JLoginDialog(DBPanel parent, boolean modal) {
       
        super(parent, modal);
        this.db=parent;
        initComponents();
    }
...
 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        if (password.equals(jPasswordField1.getText())) {
            setVisible(false);
            db.setVisible(true);
            
        }
        else {
            System.out.println("Fehler");
        }
   }

in der Main

Java:
       DBPanel db = new DBPanel();
        JLoginDialog login = new JLoginDialog(db, true);
        login.setVisible(true);

Gruß
 

Antoras

Top Contributor
Ich empfehle das Hauptfenster erst nach dem Einloggen zu erstellen - spart Ressourcen und beschleunigt den Start des Logindialogs.
 
G

Gast2

Gast
Und das PWD nicht als String speichern!!!
Wenn du swingx verwenden darfst, gibts das alles schon fertig.
 

Neue Themen


Oben