Cursor issues in JTextarea
Cursor issues in JTextarea
November 27, 2011 - 03:07
Hi,
I want to create an linux terminal like interface using java swing.The fllowing code i have written.But i'm facing issues while pressing arrow keys in textarea.Please run this code to understand the problem.
- package poc.gui;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Rectangle;
- import javax.swing.JFrame;
- import javax.swing.JTextArea;
- import javax.swing.plaf.TextUI;
- import javax.swing.text.BadLocationException;
- import javax.swing.text.DefaultCaret;
- import javax.swing.text.JTextComponent;
- public class CaretDemo extends JFrame
- {
- JTextArea area=null;
- CaretDemo()
- {
- setSize(400,400);
- area = new JTextArea();
- area.setCaret(new NewCaret());
- add(area);
- }
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- CaretDemo dc = new CaretDemo();
- dc.setVisible(true);
- }
- }
- class NewCaret extends DefaultCaret
- {
- protected void damage(Rectangle r)
- {
- //System.out.println("damage : x="+x+", y="+y+", height="+height+", width="+width+", dot="+getDot()+", mark="+getMark());
- //super.damage(r);
- if(r!=null)
- getComponent().repaint(r.x,r.y,8,r.height);
- }
- public void paint(Graphics g)
- {
- //super.paint(g);
- g.setColor(Color.GREEN);
- JTextComponent comp = getComponent();
- Rectangle rec = null;
- TextUI ui = comp.getUI();
- try {
- rec = ui.modelToView(comp, getDot());
- } catch (BadLocationException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- g.fillRect(rec.x, rec.y, 8, rec.height);
- }
- } //Help is expected



