2012-04-22 25 views
3

特定の設定で編集できないJTextAreaがあります。ただし、この設定では、ユーザーはスペースキーとバックスペースキーを引き続き使用できます。スペースに対応するために、私は次のコードを持っています。Javaが編集不可能なJTextAreaにバックスペースを挿入する

if (e.getKeyChar() == KeyEvent.VK_SPACE) { 
    editor.insert(" ", editor.getCaretPosition()); 
} 

私はバックスペースに問題があります。私はこれを試しました

if (e.getKeyChar() == KeyEvent.VK_BACK_SPACE) { 
    editor.insert("\b", editor.getCaretPosition()); 
} 

これは、バックスペースを押すと小さなスペースを追加するようです。それはスペースではなく、一度押すとほとんど目立ちません。それは間違いなくバックスペースではありません。ひどい場合は、すべての文字をキャレット位置-1までコピーして、キャレット位置の後にすべての文字に追加する必要がありますが、私はその解決策が気に入らないのです。

+0

ジャスト(!s.equals( ""))S = s.substring場合**使用(あなたが言うように)私は、しばらく前に書いたインターフェースを見て、そしてI (0、s.length() - 1); **(私のインターフェイスはカラットを必要としませんでした)また、JTextAreaがあなたのために処理するかどうかわかりませんが、Alt、Tab、Escape 、Controlなど(** KeyEvent.isActionKey()**によって捕らえられていないものも、小さなスペースが挿入される(印刷できない文字)。 – lynks

答えて

3

キーバインディングを使用すると、スペースキーとバックスペースキーに関連付けられたアクションを許可し、バックスペースを押した場合にJTextAreaのドキュメントから文字を削除できます。

は例えば、

import java.awt.event.ActionEvent; 
import java.awt.event.KeyEvent; 

import javax.swing.*; 
import javax.swing.text.BadLocationException; 
import javax.swing.text.PlainDocument; 

@SuppressWarnings("serial") 
public class TextAreaFun extends JPanel { 
    public static final String SPACE = "space"; 
    public static final String BACK_SPACE = "back space"; 
    private JTextArea textArea = new JTextArea(15, 50); 

    public TextAreaFun() { 
     // create our key bindings 
     // only allow key presses to initiate an action if the JTextArea has focus 
     int condition = JComponent.WHEN_FOCUSED; 
     InputMap taInputMap = textArea.getInputMap(condition); 
     ActionMap taActionMap = textArea.getActionMap(); 

     taInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), SPACE); 
     taInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, 0), 
      BACK_SPACE); 
     taActionMap.put(SPACE, new KeyAction(textArea, SPACE)); 
     taActionMap.put(BACK_SPACE, new KeyAction(textArea, BACK_SPACE)); 

     // checkbox that stops all editing except for that specified in the 
     // key bindings above 
     JCheckBox chkBox = new JCheckBox(new AbstractAction("Prevent Editing") { 
     { 
      putValue(SELECTED_KEY, Boolean.FALSE); // default to unchecked 
      putValue(MNEMONIC_KEY, KeyEvent.VK_P); 
     } 

     @Override 
     public void actionPerformed(ActionEvent evt) { 
      boolean selection = (Boolean) getValue(SELECTED_KEY); 
      textArea.setEditable(!selection); 
     } 
     }); 
     JPanel bottomPanel = new JPanel(); 
     bottomPanel.add(chkBox); 

     setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS)); 
     add(new JScrollPane(textArea)); 
     add(Box.createVerticalStrut(10)); 
     add(bottomPanel); 
    } 

    private static void createAndShowGui() { 
     TextAreaFun mainPanel = new TextAreaFun(); 

     JFrame frame = new JFrame("TextAreaFun"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(mainPanel); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowGui(); 
     } 
     }); 
    } 
} 

@SuppressWarnings("serial") 
// action to be initiated by key bindings 
class KeyAction extends AbstractAction { 
    private PlainDocument textAreaDocument; 
    private String title; 

    public KeyAction(JTextArea textArea, String title) { 
     this.textAreaDocument = (PlainDocument) textArea.getDocument(); 
     this.title = title; 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     if (title.equals(TextAreaFun.SPACE)) { 
     try { 
      textAreaDocument.insertString(textAreaDocument.getLength(), " ", 
        null); 
     } catch (BadLocationException e1) { 
      e1.printStackTrace(); 
     } 
     } else if (title.equals(TextAreaFun.BACK_SPACE)) { 
     if (textAreaDocument.getLength() == 0) { 
      return; 
     } 
     try { 
      textAreaDocument.remove(textAreaDocument.getLength() - 1, 1); 
     } catch (BadLocationException e1) { 
      e1.printStackTrace(); 
     } 
     } 
    } 
} 
+0

残念ながら、textareaは編集不可能なので、あなたがあなたが編集できないものを編集しようとしていることを知らせる、Windows上の厄介な高音を耳にします。 – gsingh2011

+0

@ gsingh2011:ようこそ。私のコードは私のために働き、私はその音を得ないので、私は何とかこれを再現することができるまで、私は何の提案も提供できません。上記のような[sscce](http://sscce.org)の作成と投稿を検討してください。この問題を再現した後、私にコメントしてみましょう。 –

関連する問題