FocusAbfrage innerhalb eines JDialogs

d_ausstroit

Mitglied
Wie kann man den Namen des aktiven Steuerelements abfragen. Ich habe 3 Textfelder auf einem JDialog und möchte ein best. Ereignis auslösen, wenn ich eines der drei Felder verlasse. Allerdings, wenn
ich innerhalb der Felder wechsle, darf es nicht ausgelöst werden.

Kennt jemand die Lösung für dieses Problem?

Dirk
 
Zuletzt bearbeitet:
Ich weiß nicht ob ich dich recht verstehe, aber du kannst doch auf die Ereignisse von den 3 Text Feldern reagieren (evtl. bei Änderung) und dann jeweils von den Komponenten den Focus abfragen?
 
Hai. Entschuldige, dass ich mich erst jetzt wieder melde. Ich möchte beim Verlassen eines Textfeldes prüfen, wo der Fokus hingeht. Wird er in eins der 3 Felder gesetzt soll nix passieren, wird er auf eine andere Komponente gesetzt, soll eine Prüfung stattfinden. Ich prüfe beim Verlassen der 3 Felder mit isFocusOwner welches Feld den Fokus hat. Diese Prüfung findet in der Methode focuslost der 3 Felder statt. Mein Problem ist nun, dass bereits bei dieser Prüfung keines der Felder mehr den Fokus hat (isFocusOwner ist false). Und das verstehe ich nicht.

mfg
 
Du könntest das ganze auch evtl. mit einem MouseListener lösen:

Code:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JTextField;


public class FocusTest extends JFrame implements MouseListener {


    private GridBagLayout gbl = new GridBagLayout();
    private JTextField txtFieldOne = new JTextField();
    private JTextField txtFieldTwo = new JTextField();
    private JTextField txtFieldThree = new JTextField();


    FocusTest() {

        super("FocusTest");
        initializeComponents();
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }


    private void initializeComponents() {

        setLayout(gbl);

        txtFieldOne.addMouseListener(this);
        txtFieldTwo.addMouseListener(this);
        txtFieldThree.addMouseListener(this);

        gbl.setConstraints(txtFieldOne, new GridBagConstraints(0, 0, 1, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(10, 10, 10, 10), 0, 0));
        gbl.setConstraints(txtFieldTwo, new GridBagConstraints(0, 1, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 10, 10), 0, 0));
        gbl.setConstraints(txtFieldThree, new GridBagConstraints(0, 2, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 10, 10), 0, 0));

        add(txtFieldOne);
        add(txtFieldTwo);
        add(txtFieldThree);
    }


    public void mouseClicked(MouseEvent e) {

        System.out.println("mouseClicked");
    }

    public void mouseEntered(MouseEvent e) {

        System.out.println("mouseEntered");
    }


    public void mouseExited(MouseEvent e) {

        System.out.println("mouseExited");
    }


    public void mousePressed(MouseEvent e) {

        System.out.println("mousePressed");
    }


    public void mouseReleased(MouseEvent e) {

        System.out.println("mouseReleased");
    }


    public static void main(String args[]) {

        new FocusTest();
    }
}
 
Noch ein Beispiel mit FocusListener... funktioniert auch. Hm, evtl. hab ich das Problem noch nicht verstanden.

Code:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.JFrame;
import javax.swing.JTextField;


public class FocusTest extends JFrame implements FocusListener {


    private GridBagLayout gbl = new GridBagLayout();
    private JTextField txtFieldOne = new JTextField();
    private JTextField txtFieldTwo = new JTextField();
    private JTextField txtFieldThree = new JTextField();
    private JTextField txtFieldFour = new JTextField();


    FocusTest() {

        super("FocusTest");
        initializeComponents();
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }


    private void initializeComponents() {

        setLayout(gbl);

        // txtFieldOne.addMouseListener(this);
        // txtFieldTwo.addMouseListener(this);
        // txtFieldThree.addMouseListener(this);

        txtFieldOne.addFocusListener(this);
        txtFieldTwo.addFocusListener(this);
        txtFieldThree.addFocusListener(this);

        gbl.setConstraints(txtFieldOne, new GridBagConstraints(0, 0, 1, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(10, 10, 10, 10), 0, 0));
        gbl.setConstraints(txtFieldTwo, new GridBagConstraints(0, 1, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 10, 10), 0, 0));
        gbl.setConstraints(txtFieldThree, new GridBagConstraints(0, 2, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 10, 10), 0, 0));
        gbl.setConstraints(txtFieldFour, new GridBagConstraints(0, 3, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(20, 10, 10, 10), 0, 0));

        add(txtFieldOne);
        add(txtFieldTwo);
        add(txtFieldThree);
        add(txtFieldFour);
    }


    private void checkFocus() {

        if(txtFieldOne.hasFocus()) txtFieldOne.setText("focusGained");
        else txtFieldOne.setText("focusLost");

        if(txtFieldTwo.hasFocus()) txtFieldTwo.setText("focusGained");
        else txtFieldTwo.setText("focusLost");

        if(txtFieldThree.hasFocus()) txtFieldThree.setText("focusGained");
        else txtFieldThree.setText("focusLost");
    }


    public void focusGained(FocusEvent e) {

        System.out.println("focusGained");
        checkFocus();
    }


    public void focusLost(FocusEvent e) {

        System.out.println("focusLost");
        checkFocus();
    }

    public static void main(String args[]) {

        new FocusTest();
    }
}
 
Also, ich hoffe verständlicher.
Ich habe 3 Textfelder, die als Gruppe anzusehen sind. Nämlich Straße, Plz und Ort. Wenn ich innerhalb dieser Gruppe wechsle, z.B. von der Straße in den Ort (egal wie, mit Maus oder Tastatur), darf keine Prüfung auf korrekte Eingaben erfolgen. Wenn ich aber nun diese Gruppe verlasse, z.B. mit Maus oder Tastuatur auf eine andere Komponente gehe, muss die Prüfung erfolgen.

War das verständlich ;)
 
Jetzt hab ich das Problem verstanden bzw. bin selbst darauf gestoßen. Du musst ein kleines Workaround schreiben, weil wenn du mit FocusLost arbeitest, nicht weißt, ob nicht die Komponente Straße, PLZ oder Ort als nächstest den Focus bekommt. Also...

Beispiel:
Code:
FocusEvent: focusLost()
Methode x
FocusEvent: focusGained()

Ich habe es hier so gelöst, evtl. fällt dir etwas Besseres ein.

Code:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;


public class FocusTest extends JFrame implements FocusListener {


    private GridBagLayout gbl = new GridBagLayout();
    private boolean firstVisit = false;
    private JTextField txtFieldFirstName = new JTextField(10);
    private JTextField txtFieldLastName = new JTextField();
    private JTextField txtFieldStreet = new JTextField();
    private JTextField txtFieldZip = new JTextField();
    private JTextField txtFieldPlace = new JTextField();
    private JLabel lblFirstName = new JLabel("First Name:");
    private JLabel lblLastName = new JLabel("Last Name:");
    private JLabel lblStreet = new JLabel("Street:");
    private JLabel lblZip = new JLabel("ZIP:");
    private JLabel lblPlace = new JLabel("Place:");


    FocusTest() {

        super("FocusTest");
        initializeComponents();
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }


    private void initializeComponents() {

        setLayout(gbl);

        txtFieldFirstName.addFocusListener(this);
        txtFieldLastName.addFocusListener(this);
        txtFieldStreet.addFocusListener(this);
        txtFieldZip.addFocusListener(this);
        txtFieldPlace.addFocusListener(this);

        gbl.setConstraints(lblFirstName, new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(10, 10, 0, 10), 0, 0));
        gbl.setConstraints(txtFieldFirstName, new GridBagConstraints(1, 0, 1, 1, 2, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(10, 0, 0, 10), 0, 0));
        gbl.setConstraints(lblLastName, new GridBagConstraints(0, 1, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 10, 0, 10), 0, 0));
        gbl.setConstraints(txtFieldLastName, new GridBagConstraints(1, 1, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 0, 10), 0, 0));

        gbl.setConstraints(lblStreet, new GridBagConstraints(0, 2, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(10, 10, 0, 10), 0, 0));
        gbl.setConstraints(txtFieldStreet, new GridBagConstraints(1, 2, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(10, 0, 0, 10), 0, 0));
        gbl.setConstraints(lblZip, new GridBagConstraints(0, 3, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 10, 0, 10), 0, 0));
        gbl.setConstraints(txtFieldZip, new GridBagConstraints(1, 3, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 0, 10), 0, 0));
        gbl.setConstraints(lblPlace, new GridBagConstraints(0, 4, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 10, 10, 10), 0, 0));
        gbl.setConstraints(txtFieldPlace, new GridBagConstraints(1, 4, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 10, 10), 0, 0));

        add(txtFieldFirstName);
        add(txtFieldLastName);
        add(txtFieldStreet);
        add(txtFieldZip);
        add(txtFieldPlace);

        add(lblFirstName);
        add(lblLastName);
        add(lblStreet);
        add(lblZip);
        add(lblPlace);
    }


    private void showInfo() {


        JOptionPane.showMessageDialog(this, "Check Data for Street, Zip and Place", "Validate", JOptionPane.INFORMATION_MESSAGE);
        firstVisit = false;
    }


    public void focusGained(FocusEvent e) {

        Object o = e.getSource();

        if(o.equals(txtFieldStreet)) { firstVisit = true; }
        else if(o.equals(txtFieldZip)) { firstVisit = true; }
        else if(o.equals(txtFieldPlace)) { firstVisit = true; }
        else if(o.equals(txtFieldFirstName) && firstVisit) { showInfo(); }
        else if(o.equals(txtFieldLastName) && firstVisit) { showInfo(); }
    }


    public void focusLost(FocusEvent e) {

    }

    public static void main(String args[]) {

        new FocusTest();
    }
}
 
Hallo BillieJoe, biste noch dabei?

Danke schon mal für Deine Hilfe. Aber ich möchte die Prüfung durchführen, wenn das Feld den Fokus verliert. Ich habe das in der Methode focusLost implementiert, aber das klappt irgendwie nicht. Hast Du noch eine Idee?

Gleich eine weitere Frage. Kann ich alle Komponenten eines Dialogs oder Frames in einer Schleife durchlaufen? Wenn ja, wie?-)

Dirk
 
Zuletzt bearbeitet:
Die Lösung mit dem FocusListener ist schon richtig.

Du implementierst einen FocusListener und registrierst ihn bei deinen 3 Textfeldern.

Das war es.


Gruss,
Torsten
 
Ich muss Ihn doch bei allen Komponenten des Dialogs registrieren, oder? Daher meine weitere Frage: Kann ich alle Komponenten eines Dialogs oder Frames in einer Schleife durchlaufen? Wenn ja, wie?-)

mfg
 
Zurück