IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Agents de placement/Fenêtres Java Discussion :

Demande d'aide sur les Handlers


Sujet :

Agents de placement/Fenêtres Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Profil pro
    Inscrit en
    Février 2009
    Messages
    48
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2009
    Messages : 48
    Par défaut Demande d'aide sur les Handlers
    Bonjour a tous,

    Je cherche a écouter une JFrame, dans le but de récupérer sa position actuelle. J'ai pas mal regardé la Doc Java de Sun, mais rien de vraiment concluant.

    Évidemment je n'ai pas accès aux codes sources de la JFrame, sinon ce serait bien plus simple

    Des idées?

  2. #2
    Rédacteur/Modérateur

    Avatar de bouye
    Homme Profil pro
    Information Technologies Specialist (Scientific Computing)
    Inscrit en
    Août 2005
    Messages
    6 919
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 49
    Localisation : Nouvelle-Calédonie

    Informations professionnelles :
    Activité : Information Technologies Specialist (Scientific Computing)
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Août 2005
    Messages : 6 919
    Billets dans le blog
    54
    Par défaut
    Effectivement c'est qq chose qui manque et ca ne m'a jamais traverse l'esprit. Mais je dirais un truc du genre peut faire l'affaire mais il peut y avoir plus simple :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     
    import java.util.EventListener;
     
    /**
     * Interface that defines a class interrested into listening windows location event.
     * @author Fabrice Bouyé (fabriceb@spc.int)
     */
    public interface WindowLocationListener extends EventListener {
        /**
         * This method is called whenever a window is moved accros the sceeen.
         * @param event The window location event.
         */
        public void windowMoved(WindowLocationEvent event);
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    import java.awt.AWTEvent;
    import java.awt.Point;
    import java.awt.Window;
     
    /**
     * Class used for broadcasting windows location events.
     * @author Fabrice Bouyé (fabriceb@spc.int)
     */
    public class WindowLocationEvent extends AWTEvent {
     
        /**
         * The last location of the window.
         */
        private Point location;
     
        /**
         * Creates a new instance.
         * @param source The source window.
         * @param location The last location of the window.
         */
        public WindowLocationEvent(Window source, Point location) {
            super(source, 0);
            this.location = location;
        }
     
        /**
         * {@inheritDoc}
         */
        @Override
        public Window getSource() {
            return (Window) super.getSource();
        }
     
        /**
         * Gets the last known location of the window.
         */
        public Point getLocation() {
            return location;
        }
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
     
    import java.awt.Point;
    import java.awt.Window;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowEvent;
    import java.awt.event.WindowListener;
    import javax.swing.Timer;
    import javax.swing.event.EventListenerList;
     
    /**
     * A tracker that broadcasts a window's location change.
     * <br/>This class uses a Swing <code>Timer</code> internally and will listen to the window's state to start and stop this timer.
     * @author Fabrice Bouyé (fabriceb@spc.int)
     */
    public class WindowLocationTracker {
     
        /**
         * Default timer delay.
         */
        public static final int DEFAULT_DELAY = 75;
        /**
         * Inner listener.
         */
        private InnerListener innerListener = new InnerListener();
        /**
         * The target <code>Window</code>.
         */
        private Window window;
        /**
         * The timer.
         */
        private Timer timer;
        /**
         * Timer refresh delay.
         */
        private int delay = DEFAULT_DELAY;
     
        /**
         * Creates a new instance.
         * @param window The target <code>Window</code>.
         * @throws IllegalArgumentException If <code>window</code> is <code>null</code>.
         */
        public WindowLocationTracker(Window window) throws IllegalArgumentException {
            if (window == null) {
                throw new IllegalArgumentException("Window cannot be null.");
            }
            this.window = window;
            timer = new Timer(delay, innerListener);
            timer.setRepeats(true);
            window.addWindowListener(innerListener);
        }
     
        /**
         * Start the tracker.
         */
        public void start() {
            if (!timer.isRunning()) {
                timer.start();
            }
        }
     
        /**
         * Stop the tracker.
         */
        public void stop() {
            if (timer.isRunning()) {
                timer.stop();
            }
        }
        /**
         * The last known screen location of the window.
         */
        private Point lastLocation = null;
        /**
         * List that hold listeners.
         */
        private EventListenerList listenerList = new EventListenerList();
     
        /**
         * Registers a listener interrested into listener for window location event.
         * @param listener The listener.
         */
        public void addWindowLocationListener(WindowLocationListener listener) {
            listenerList.add(WindowLocationListener.class, listener);
        }
     
        /**
         * Unregisters a listener interrested into listener for window location event.
         * @param listener The listener.
         */
        public void removeWindowLocationListener(WindowLocationListener listener) {
            listenerList.remove(WindowLocationListener.class, listener);
        }
     
        /**
         * Fires a window location event to registered listeners.
         */
        protected void fireWindowMoved() {
            // Guaranteed to return a non-null array
            Object[] listeners = listenerList.getListenerList();
            // Process the listeners last to first, notifying those that are interested in this event
            WindowLocationEvent event = null;
            for (int i = listeners.length - 2; i >= 0; i -= 2) {
                if (listeners[i] == WindowLocationListener.class) {
                    // Lazily create the event:
                    if (event == null) {
                        event = new WindowLocationEvent(window, lastLocation);
                    }
                    ((WindowLocationListener) listeners[i + 1]).windowMoved(event);
                }
            }
        }
     
        /**
         * Inner listener class.
         * @author Fabrice Bouyé (fabriceb@spc.int)
         */
        private class InnerListener implements WindowListener, ActionListener {
     
            /**
             * {@inheritDoc}
             */
            @Override
            public void windowActivated(WindowEvent e) {
                start();
            }
     
            /**
             * {@inheritDoc}
             */
            @Override
            public void windowClosed(WindowEvent e) {
                stop();
            }
     
            /**
             * {@inheritDoc}
             */
            @Override
            public void windowClosing(WindowEvent e) {
                stop();
            }
     
            /**
             * {@inheritDoc}
             */
            @Override
            public void windowDeactivated(WindowEvent e) {
                stop();
            }
     
            /**
             * {@inheritDoc}
             */
            @Override
            public void windowDeiconified(WindowEvent e) {
                start();
            }
     
            /**
             * {@inheritDoc}
             */
            @Override
            public void windowIconified(WindowEvent e) {
                stop();
            }
     
            /**
             * {@inheritDoc}
             */
            @Override
            public void windowOpened(WindowEvent e) {
                start();
            }
     
            /**
             * {@inheritDoc}
             */
            @Override
            public void actionPerformed(ActionEvent e) {
                Point location = window.getLocationOnScreen();
                if (lastLocation == null || !lastLocation.equals(location)) {
                    lastLocation = location;
                    fireWindowMoved();
                }
            }
        }
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
     
    import java.awt.Point;
    import java.awt.Window;
    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
     
    /**
     * Test the <code>WindowLocationTracker</code>.
     * @author Fabrice Bouyé (fabriceb@spc.int)
     */
    public class WindowLocationTrackerTest {
     
        public static void main(String... args) {
            SwingUtilities.invokeLater(new Runnable() {
     
                /**
                 * {@inheritDoc}
                 */
                @Override
                public void run() {
                    JFrame frame = new JFrame("Test");
                    frame.setName("Test");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    WindowLocationTracker tracker = new WindowLocationTracker(frame);
                    tracker.addWindowLocationListener(new WindowLocationListener() {
     
                        /**
                         * {@inheritDoc}
                         */
                        @Override
                        public void windowMoved(WindowLocationEvent event) {
                            Window window = event.getSource();
                            Point location = event.getLocation();
                            System.out.printf("Window \"%s\" at location \"%s\".", window.getName(), location).println();
                        }
                    });
                    frame.setVisible(true);
                }
            });
        }
    }
    A voir aussi si ca ne vaut pas le coup d'ajouter un support de WindowFocusListener a l'interieur du WindowLocationTracker.

    C'est facilement adaptable pour tracker la taille de la fenetre egalement (quoi que pour ce dernier c'est un peu inutile car c'est beaucoup plus simple de mettre un ComponentListener sur son contenu ou son contentpane).

    Et pour finir :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     
    public class MyFrame extends JFrame {
      private WindowLocationTracker tracker = new WindowLocationTracker(this);
     
     [...]
     
     public void addWindowLocationListener(WindowLocationListener listener) {
       tracker.addWindowLocationListener(listener);
     }
     
     public void removeWindowLocationListener(WindowLocationListener listener) {
       tracker.removeWindowLocationListener(listener);
     }
    }
    Merci de penser au tag quand une réponse a été apportée à votre question. Aucune réponse ne sera donnée à des messages privés portant sur des questions d'ordre technique. Les forums sont là pour que vous y postiez publiquement vos problèmes.

    suivez mon blog sur Développez.

    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning. ~ Rich Cook

  3. #3
    Rédacteur/Modérateur

    Avatar de bouye
    Homme Profil pro
    Information Technologies Specialist (Scientific Computing)
    Inscrit en
    Août 2005
    Messages
    6 919
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 49
    Localisation : Nouvelle-Calédonie

    Informations professionnelles :
    Activité : Information Technologies Specialist (Scientific Computing)
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Août 2005
    Messages : 6 919
    Billets dans le blog
    54
    Par défaut
    Ah oui et sinon le code source de JFrame, JWindow, Frame et Window est disponible dans le fichier src.zip qui est present a la racine du JDK... cependant il est possible que le deplacement des fenetres soit tout simplement gerer par l'OS et non pas par Java.
    Merci de penser au tag quand une réponse a été apportée à votre question. Aucune réponse ne sera donnée à des messages privés portant sur des questions d'ordre technique. Les forums sont là pour que vous y postiez publiquement vos problèmes.

    suivez mon blog sur Développez.

    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning. ~ Rich Cook

  4. #4
    Membre chevronné Avatar de ngpub
    Profil pro
    Inscrit en
    Mai 2008
    Messages
    449
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2008
    Messages : 449
    Par défaut
    Je cherche a écouter une JFrame, dans le but de récupérer sa position
    Il existe le ComponentListener qui normalement permet de faire cela via la méthode componentMoved.

  5. #5
    Rédacteur/Modérateur

    Avatar de bouye
    Homme Profil pro
    Information Technologies Specialist (Scientific Computing)
    Inscrit en
    Août 2005
    Messages
    6 919
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 49
    Localisation : Nouvelle-Calédonie

    Informations professionnelles :
    Activité : Information Technologies Specialist (Scientific Computing)
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Août 2005
    Messages : 6 919
    Billets dans le blog
    54
    Par défaut
    Et la moi je fais /DOH.... trop de JavaFX tue le Java...
    Merci de penser au tag quand une réponse a été apportée à votre question. Aucune réponse ne sera donnée à des messages privés portant sur des questions d'ordre technique. Les forums sont là pour que vous y postiez publiquement vos problèmes.

    suivez mon blog sur Développez.

    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning. ~ Rich Cook

  6. #6
    Membre averti
    Profil pro
    Inscrit en
    Février 2009
    Messages
    48
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2009
    Messages : 48
    Par défaut
    J'essaye vos solutions et je vous tiens informés

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. demande d'aide sur les APIs java win32
    Par mogo062 dans le forum Entrée/Sortie
    Réponses: 0
    Dernier message: 18/02/2009, 14h25
  2. [MediaWiki] Demande d'aide sur les pages
    Par ndsaerith dans le forum EDI, CMS, Outils, Scripts et API
    Réponses: 4
    Dernier message: 15/10/2008, 14h29
  3. demande d'aide sur les bouton
    Par naruto01 dans le forum VB 6 et antérieur
    Réponses: 4
    Dernier message: 29/01/2007, 18h20
  4. Réponses: 4
    Dernier message: 31/08/2006, 16h31
  5. Demande d'aide sur les regexp
    Par Uld dans le forum Langage
    Réponses: 1
    Dernier message: 18/08/2006, 22h15

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo