try {   Point p = t.modelToView(t.getCaret().getDot()).getLocation();  int cur = t.getCaretPosition(); int y = p.y;  while (y == p.y && cur > 0) y = t.modelToView(--cur).getLocation().y; if (cur != 0) cur++;  t.getCaret().moveDot(cur); } catch (BadLocationException ble) {  }
if(offs > 0) { Caret c = t.getCaret(); c.moveDot(offs); try { c.setMagicCaretPosition(t.modelToView(offs).getLocation()); } catch(BadLocationException ble) {  } } }
      public void actionPerformed(ActionEvent event)      {        JTextComponent t = getTextComponent(event);                try        {          // TODO: There is a more efficent solution, but          // viewToModel doesn't work properly.          Point p = t.modelToView(t.getCaret().getDot()).getLocation();                    int cur = t.getCaretPosition();          int y = p.y;                    while (y == p.y && cur > 0)            y = t.modelToView(--cur).getLocation().y;          if (cur != 0)            cur++;                    t.getCaret().moveDot(cur);        }        catch (BadLocationException ble)        {          // Do nothing here.        }      }