tags:

views:

17

answers:

1

How can the screen be adjusted such that the cursor should point to the highlighted text on maximizing/minimizing or expanding the window size in java UI

+1  A: 

You should look at the java.awt.Robot class, which can be used to move the mouse pointer using the mouseMove(int x, int y) method.

The it is a simply a case of obtaining the bounds of the Component containing the text and relocating the mouse to the center of the bounds; e.g.

// Application frame definition:
JFrame frame = ...

// Text field embedded within frame:
JTextField txtFld = ...

// Add WindowListener responsible for detecting when window state changes.
frame.addWindowListener(new WindowAdapter() {
  public void windowStateChanged(WindowEvent e) {
    // Reposition mouse over text field providing window isn't iconified.
    if (frame.getExtendedState() != JFrame.ICONIFIED) {
      Rectangle bounds = txtFld.getBounds();
      Robot.moveMouse(bounds.x / 2, bounds.y / 2);
    }
  }
});
Adamski