Forum Stats

  • 3,734,269 Users
  • 2,246,935 Discussions
  • 7,857,216 Comments

Discussions

JTextPane edit mode (insert or overwrite)???

843805
843805 Member Posts: 49,999
edited Nov 11, 2005 11:26AM in Swing
How can I know which edit mode (insert or overwrite) is used in my JTextPane?

Comments

  • camickr
    camickr Member Posts: 24,931
    In JDK1.4.2 it only supports insert mode.

    Is there a new feature in 1.5?
  • 843805
    843805 Member Posts: 49,999
    In JDK1.4.2 it only supports insert mode.

    Is there a new feature in 1.5?
    I don't know about JTextPane in JDK1.5.
    And I agree with you that JTextPane supports insert mode only in JDK1.4.2 but I need to have overwrite mode too in my JTextPane.
    What should I do?
  • 800351
    800351 Member Posts: 3,898
    An idea:
    Override insertString() and do continuously remove/replace pre-exixting characters.
  • camickr
    camickr Member Posts: 24,931
    but I need to have overwrite mode too in my JTextPane
    Then why didn't you state that in your original question?

    Here is my version that allows you to toggle between both modes:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.text.*;
    import javax.swing.event.*;
    import javax.swing.plaf.*;
    
    public class OvertypeTextArea extends JTextArea
    {
    	private static Toolkit toolkit = Toolkit.getDefaultToolkit();
    	private static boolean isOvertypeMode;
    
    	private Caret defaultCaret;
    	private Caret overtypeCaret;
    
    	public OvertypeTextArea(int row, int column)
    	{
    		super(row, column);
    		setCaretColor( Color.red );
    		defaultCaret = getCaret();
    		overtypeCaret = new OvertypeCaret();
    		overtypeCaret.setBlinkRate( defaultCaret.getBlinkRate() );
    		setOvertypeMode( true );
    	}
    
    	/*
    	 *	Return the overtype/insert mode
    	 */
    	public boolean isOvertypeMode()
    	{
    		return isOvertypeMode;
    	}
    
    	/*
    	 *	Set the caret to use depending on overtype/insert mode
    	 */
    	public void setOvertypeMode(boolean isOvertypeMode)
    	{
    		this.isOvertypeMode = isOvertypeMode;
    		int pos = getCaretPosition();
    
    		if ( isOvertypeMode() )
    		{
    			setCaret( overtypeCaret );
    		}
    		else
    		{
    			setCaret( defaultCaret );
    		}
    
    		setCaretPosition( pos );
    	}
    
    	/*
    	 *  Override method from JComponent
    	 */
    	public void replaceSelection(String text)
    	{
    		//  Implement overtype mode by selecting the character at the current
    		//  caret position
    
    		if ( isOvertypeMode() )
    		{
    			int pos = getCaretPosition();
    
    			if (getSelectedText() == null
    			&&  pos < getDocument().getLength())
    			{
    				moveCaretPosition( pos + 1);
    			}
    		}
    
    		super.replaceSelection(text);
    	}
    
    	/*
    	 *  Override method from JComponent
    	 */
    	protected void processKeyEvent(KeyEvent e)
    	{
    		super.processKeyEvent(e);
    
    	 	//  Handle release of Insert key to toggle overtype/insert mode
    
    	 	if (e.getID() == KeyEvent.KEY_RELEASED
    	 	&&  e.getKeyCode() == KeyEvent.VK_INSERT)
    	 	{
    			setOvertypeMode( ! isOvertypeMode() );
    	 	}
    	}
    
    	/*
    	 *  Paint a horizontal line the width of a column and 1 pixel high
    	 */
    	class OvertypeCaret extends DefaultCaret
    	{
    		/*
    		 *  The overtype caret will simply be a horizontal line one pixel high
    		 *  (once we determine where to paint it)
    		 */
    		public void paint(Graphics g)
    		{
    			if (isVisible())
    			{
    				try
    				{
    					JTextComponent component = getComponent();
    					TextUI mapper = component.getUI();
    					Rectangle r = mapper.modelToView(component, getDot());
    					g.setColor(component.getCaretColor());
    					int width = g.getFontMetrics().charWidth( 'w' );
    					int y = r.y + r.height - 2;
    					g.drawLine(r.x, y, r.x + width - 2, y);
    				}
    				catch (BadLocationException e) {}
    			}
    		}
    
    		/*
    		 *  Damage must be overridden whenever the paint method is overridden
    		 *  (The damaged area is the area the caret is painted in. We must
    		 *  consider the area for the default caret and this caret)
    		 */
    		protected synchronized void damage(Rectangle r)
    		{
    			if (r != null)
    			{
    				JTextComponent component = getComponent();
    				x = r.x;
    				y = r.y;
    				width = component.getFontMetrics( component.getFont() ).charWidth( 'w' );
    				height = r.height;
    				repaint();
    			}
    		}
    	}
    
    	public static void main(String[] args)
    	{
    		JFrame frame = new JFrame();
    		frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    		OvertypeTextArea textArea	= new OvertypeTextArea(5,20);
    		textArea.setFont( new Font("monospaced", Font.PLAIN, 12) );
    		textArea.setText("abcdefg");
    		frame.getContentPane().add( textArea );
    		frame.pack();
    		frame.setVisible(true);
    	}
    }
  • 843805
    843805 Member Posts: 49,999
    thanks camickr your code worked great.
  • camickr
    camickr Member Posts: 24,931
    Glad you found the code helpfull.
This discussion has been closed.