Visualizzazione dei risultati da 1 a 1 su 1

Discussione: Problema con KeyStroke

  1. #1
    Utente di HTML.it
    Registrato dal
    Jan 2014
    Messaggi
    305

    Problema con KeyStroke

    Ho la seguente classe :

    codice:
    package jCallRemember.libreria;
    
    
    import java.awt.Dialog;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.InputEvent;
    import java.awt.event.KeyEvent;
    import java.util.Observable;
    import java.util.Observer;
    import javax.swing.AbstractAction;
    import javax.swing.JTextArea;
    import javax.swing.KeyStroke;
    import javax.swing.SwingUtilities;
    import javax.swing.text.DefaultCaret;
    import javax.swing.text.DefaultEditorKit;
    import javax.swing.text.Keymap;
    
    
    /**
     * @author Linux Implementa un Form per scambiare messaggi. La classe estende
     * JFrame e implementa Observer nel caso arrivino dei messaggi.
     */
    public class FormChat extends javax.swing.JFrame implements Observer {
    
    
        /**
         *
         * @return il nickname di chi utilizza il form
         */
        public String getNickName() {
            return yourNickName;
        }
    
    
        private class MyObservable extends Observable{
            @Override
            public void setChanged(){
                super.setChanged();
            }
        }
        
        /**
         * Costruttore che prende in input i nickname di chi conversa, e il titolo
         * del form.Inizializza i componenti grafici.
         *
         * @param yourNickName chi utilizza il form
         * @param hisNickName interlocutore remoto
         * @param title titolo
         */
        public FormChat(String yourNickName, String hisNickName, String title) {
            this.yourNickName = yourNickName;
            this.hisNickName = hisNickName;
            this.setTitle(title);
            initComponents();
            DefaultCaret c = (DefaultCaret) this.textChat.getCaret();
            c.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
        }
    
    
        /**
         * Imposta alcune proprietà del form
         */
        public void setup() {
            this.setModalExclusionType(Dialog.ModalExclusionType.APPLICATION_EXCLUDE);
            this.setResizable(false);
        }
    
    
        /**
         * Utilizza un OkChatButtonListener come listener di default per il button
         * del form, istanzia l'oggetto e lo registra al button, fa si inoltre che
         * prmendo invio nella textarea del messaggio esso viene inviato , mentre
         * premendo CTRL_ENTER si va nella nuova linea.
         */
        public void useDefaultListenerForOkButton() {
            if (sender != null) {
                final OkChatButtonListener listener = new OkChatButtonListener(this.textChat,
                        this.textMsg, this.yourNickName, this.sender);
                Keymap keyMap = textMsg.getKeymap();
    
    
                keyMap.addActionForKeyStroke(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, InputEvent.CTRL_DOWN_MASK),
                        new DefaultEditorKit.InsertBreakAction());
                keyMap.addActionForKeyStroke(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),
                        new AbstractAction(){
    
    
                    @Override
                    public void actionPerformed(ActionEvent e) {
                       listener.actionPerformed(null);
                    }
                            
                        });
                
               
                this.okButton.addActionListener(listener);
            }
    
    
        }
    
    
        /**
         * Imposta l'osservatore della chat ovvero il thread che si occupa ogni
         * volta che viene premuto ok di inviare il messaggio.
         *
         * @param o un oggetto che implementa Observer, che si occupa di inviare il
         * messaggio digitato
         */
        public void setSender(Observer o) {
            this.sender = o;
        }
    
    
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
    
    
            jScrollPane3 = new javax.swing.JScrollPane();
            jScrollPane4 = new javax.swing.JScrollPane();
            textMsg = new javax.swing.JTextArea();
            okButton = new javax.swing.JButton();
    
    
            setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    
    
            textChat.setEditable(false);
            textChat.setColumns(20);
            textChat.setRows(5);
            jScrollPane3.setViewportView(textChat);
    
    
            textMsg.setColumns(20);
            textMsg.setRows(5);
            jScrollPane4.setViewportView(textMsg);
    
    
            okButton.setText("OK");
    
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 410, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(0, 0, Short.MAX_VALUE))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jScrollPane4)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(okButton)))
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 232, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                        .addComponent(okButton, javax.swing.GroupLayout.DEFAULT_SIZE, 41, Short.MAX_VALUE))
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
    
    
            pack();
        }// </editor-fold>                        
        /**
         * Permette eventualmente di impostare un ActionListener diverso per il
         * button della chat
         *
         * @param action
         */
        public void setOkButtonListener(ActionListener action) {
            this.okButton.addActionListener(action);
        }
    
    
        /**
         *
         * @return la textarea dove vengono visualizzati i messaggi scritti
         */
        public JTextArea getTextChat() {
            return textChat;
        }
    
    
        /**
         *
         * @return la textarea dove si scrive il mess
         */
        public JTextArea getTextMsg() {
            return textMsg;
        }
    
    
        /**
         *
         * @return il nickname dell'interlocutore remoto
         */
        public String gethisNickName() {
            return this.hisNickName;
        }
    
    
        /**
         * Verifica l'argomento passato sia un istanza di string e aggiunge il
         * messaggio ricevuto alla textarea del form. Facendo pero svolgere tale
         * lavoro all'edt.
         *
         * @param o
         * @param arg
         */
        @Override
        public void update(Observable o, Object arg) {
            if (arg instanceof String) {
                final String mess = arg.toString();
                SwingUtilities.invokeLater(new Runnable() {
    
    
                    @Override
                    public void run() {
                        synchronized (textChat) {
                            textChat.append((String) mess);
                        }
                        if (!FormChat.this.isVisible()) {
                            setVisible(true);
                        }
                    }
    
    
                });
    
    
            }
    
    
        }
    
    
        private final String yourNickName;
        private final String hisNickName;
        private Observer sender;
        // Variables declaration - do not modify                     
        private javax.swing.JScrollPane jScrollPane3;
        private javax.swing.JScrollPane jScrollPane4;
        private javax.swing.JButton okButton;
        private final javax.swing.JTextArea textChat = new javax.swing.JTextArea();
        private javax.swing.JTextArea textMsg;
        // End of variables declaration                   
    
    
    }
    il problema è che se apro due form di questa classe e premo invio per inviare il messaggio (vedi metodo usedefaultlistenerforokbutton) quello che succede e che il software fa riferimento allo stesso form. ovvero è come se premessi invio sempre sullo stesso form
    Ultima modifica di linux_r; 10-07-2014 a 21:13

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.