Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

JTextPane edit mode (insert or overwrite)???

843805Sep 27 2005 — edited Nov 11 2005
How can I know which edit mode (insert or overwrite) is used in my JTextPane?

Comments

camickr
In JDK1.4.2 it only supports insert mode.

Is there a new feature in 1.5?
843805
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
An idea:
Override insertString() and do continuously remove/replace pre-exixting characters.
camickr
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
thanks camickr your code worked great.
camickr
Glad you found the code helpfull.
1 - 6
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Dec 9 2005
Added on Sep 27 2005
6 comments
1,658 views