38853


内部クラスを使って`DocumentEvent`を使いたいのですが、`addDocumentListener`の使い方がいまいちわかりません。どこを修正すべきでしょうか？
よろしくお願いします。

[code]

    import java.awt.*;
    import java.awt.event.*;
    import javax.lang.model.element.Element;
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    import javax.swing.event.DocumentEvent;
    import javax.swing.event.DocumentListener;
    import javax.swing.text.Caret;
    import javax.swing.text.Document;
    
    class CaesarFrame{
        JFrame frame;
        JTextField textField1, textField2;
        JButton button;
        JPanel panel1, panel2;
        JComboBox<Object> comboBox1;
        Object[] ob = {'A','B','C','D','E','F','G','H','I','J','K','L'
                ,'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
        Container contentPane;
        JLabel label;
    
        // Constructor
        CaesarFrame(){  
            frame = new JFrame("SwingLab");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(400, 110);
            frame.setLayout(new BorderLayout());
            frame.setLocationRelativeTo(null);
    
            contentPane = frame.getContentPane();
            textField1 = new JTextField(20);
            textField2 = new JTextField(28);
            textField2.setEditable(false);
            button = new JButton("Code!");
            comboBox1 = new JComboBox<Object>(ob);
            label = new JLabel("Output:");
            panel1 = new JPanel();
            panel2 = new JPanel();
    
            panel1.add(comboBox1);
            panel1.add(textField1);
            panel1.add(button);
            panel2.add(label);
            panel2.add(textField2);
    
            contentPane.add(panel1, BorderLayout.NORTH);
            contentPane.add(panel2,BorderLayout.WEST);
    
            frame.setVisible(true);
    
            ActionListener buttonCode = new OkButtonActionListener();
            button.addActionListener(buttonCode);
    
            KeyAdapter key = new InputFieldKeyListener();
            textField1.addKeyListener(key);
            //この箇所がよくわかりません。
            KeyAdapter key2 = new InputFieldKeyListener();
            Document doc = ((DocumentEvent) key2).getDocument();
            doc.addDocumentListener((DocumentListener) key);
    
        }
    
        class InputFieldKeyListener extends KeyAdapter implements DocumentListener{
            public void keyTyped(KeyEvent e) {
                char temp = e.getKeyChar();
                String ch =String.valueOf(temp);
                char offset = (char) comboBox1.getSelectedItem();
                textField2.setText(Caesar.caesarCode(ch,offset));
            }
    
            @Override
            public void changedUpdate(DocumentEvent e) {
                System.out.println("Changed");
                }
    
            @Override
            public void insertUpdate(DocumentEvent e) {
                // TODO Auto-generated method stub
                System.out.println("Inserted");
                }
    
            @Override
            public void removeUpdate(DocumentEvent e) {
                // TODO Auto-generated method stub
                System.out.println("Removed");
                }
    
    
        }
    
    
    
        private class OkButtonActionListener implements ActionListener{
            @Override
            public void actionPerformed(ActionEvent e) {
                String message = textField1.getText();
                char offset = (char) comboBox1.getSelectedItem();
                textField2.setText(Caesar.caesarCode(message,offset));
            }
        }
    }
    
[/code]


CC BY-SA 3.0
2017-10-18T16:48:26.133
38843
2017-10-19T05:49:41.813
2017-10-19T05:49:41.813
46
22984
question
java swing
SwingのDocumentEventの追加について


> [JTextField (Java Platform SE 8
> )](https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/JTextField.html)の冒頭から引用:
>
>
> JTextComponentベースのコンポーネントでは、DocumentEvent経由でモデルからDocumentListenersに変更が送られます。DocumentEventは、必要に応じて、変更位置と変更の種類を提供します。この部分のコードは次のようになります。
[code]

>     DocumentListener myListener = ??;
>     JTextField myArea = ??;
>     myArea.getDocument().addDocumentListener(myListener);
>  
[/code]

とあるように、[JTextComponent#getDocument()](https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/text/JTextComponent.html#getDocument--)メソッドを使用すると良さそうです。

[code]

    //この箇所がよくわかりません。
    //InputFieldKeyListener key2 = new InputFieldKeyListener();
    // Document doc = ((DocumentEvent) key2).getDocument();
    // doc.addDocumentListener((DocumentListener) key);
    // DocumentEvent#getDocument()ではなく、JTextComponent#getDocument()を使うべき
    textField1.getDocument().addDocumentListener(key);
    
[/code]

**サンプルコード**

[code]

    import java.awt.*;
    import java.awt.event.*;
    import javax.lang.model.element.Element;
    import javax.swing.*;
    import javax.swing.event.DocumentEvent;
    import javax.swing.event.DocumentListener;
    import javax.swing.text.Caret;
    import javax.swing.text.Document;
    
    public class CaesarFrameTest {
      Object[] ob = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L'
        , 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
      };
      private final JComboBox<Object> comboBox1 = new JComboBox<>(ob);
      private final JTextField textField1 = new JTextField(20);
      private final JTextField textField2 = new JTextField(28);
      private final JButton button = new JButton("Code!");
    
      public Container makeUI() {
        textField2.setEditable(false);
    
        JLabel label = new JLabel("Output:");
        JPanel panel1 = new JPanel();
        JPanel panel2 = new JPanel();
    
        panel1.add(comboBox1);
        panel1.add(textField1);
        panel1.add(button);
        panel2.add(label);
        panel2.add(textField2);
    
        ActionListener buttonCode = new OkButtonActionListener();
        button.addActionListener(buttonCode);
    
        InputFieldKeyListener key = new InputFieldKeyListener();
        textField1.addKeyListener(key);
    
        //この箇所がよくわかりません。
        //InputFieldKeyListener key2 = new InputFieldKeyListener();
        // Document doc = ((DocumentEvent) key2).getDocument();
        // doc.addDocumentListener((DocumentListener) key);
        textField1.getDocument().addDocumentListener(key);
    
        Container contentPane = new JPanel(new BorderLayout());
        contentPane.add(panel1, BorderLayout.NORTH);
        contentPane.add(panel2, BorderLayout.WEST);
        return contentPane;
      }
    
      public static void main(String... args) {
        EventQueue.invokeLater(() -> {
          JFrame f = new JFrame("SwingLab");
          f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          f.getContentPane().add(new CaesarFrameTest().makeUI());
          f.setSize(400, 110);
          f.setLocationRelativeTo(null);
          f.setVisible(true);
        });
      }
    
      class InputFieldKeyListener extends KeyAdapter implements DocumentListener {
        public void keyTyped(KeyEvent e) {
          char temp = e.getKeyChar();
          System.out.println(temp);
          String ch = String.valueOf(temp);
          Object offset = comboBox1.getSelectedItem();
          textField2.setText(String.format("%s %s", ch, offset));
          //char offset = (char) comboBox1.getSelectedItem();
          //textField2.setText(Caesar.caesarCode(ch,offset));
        }
    
        @Override
        public void changedUpdate(DocumentEvent e) {
          System.out.println("Changed");
        }
    
        @Override
        public void insertUpdate(DocumentEvent e) {
          // TODO Auto-generated method stub
          System.out.println("Inserted");
        }
    
        @Override
        public void removeUpdate(DocumentEvent e) {
          // TODO Auto-generated method stub
          System.out.println("Removed");
        }
    
    
      }
      private class OkButtonActionListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
          String message = textField2.getText();
          Object offset = comboBox1.getSelectedItem();
          //textField2.setText(Caesar.caesarCode(message,offset));
        }
      }
    }
    
[/code]


CC BY-SA 3.0
2017-10-19T04:54:58.310
38853
2017-10-19T04:54:58.310
46
38843
answer