Cursor and Combo Box Demo in Java

Introduction:

In this tutorial we will see changing cursor when it is selected from Combo Box in Java. You will learn about cursor as well as JComboBox. The position of the mouse is represented on the computer's screen by a small image called a cursor. It has a hot spot, which is a Point that specifies the pixel within the image that corresponds to the exact point on the screen where the mouse is pointing. The Cursor class has many predefined cursor which can be used with Cursor.getPredefinedCursor(code) method. Where code represent type of cursor.

Implementation

Step 1: Designing GUI & adding ItemListener We need JComboBox for listing the four cursor types. We set the row count as 3 so that only three items of the combo box are displayed at a time, for more than three items, scroll bar is created. In the constructor of the class JCombo Box is created with the String array which contain the list items. Combo Box is registered with ItemListener for handling item events. The array of String looks like below. Switch statement is used to execute the action based on item selected from the combo box.JComboBox's getSelectedIndex() returns the index of selected item. setCursor() changes the cursor to specified cursor.
  1. private String names[] =
  2. { "Crosshair Cursor", "Wait Cursor", "Move Cursor", "Custom Cursor" };
  3. public CursorComboBoxDemo(){
  4. combo=new JComboBox(names);
  5. combo.setMaximumRowCount( 3 );
  6. combo.addItemListener(new ItemListener(){
  7. public void itemStateChanged(ItemEvent e)
  8. {
  9. if(e.getStateChange()==ItemEvent.SELECTED)
  10. {
  11. int choice=combo.getSelectedIndex();
  12. switch(choice){
  13. case 0:
  14. setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
  15. break;
  16. case 1:
  17. setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
  18. break;
  19. case 2:
  20. setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
  21. break;
  22. case 3:
  23. default:
  24. useCustomCursor("resources/TinySmiley.png");
  25. }
  26. }
  27. }
  28. });
  29. add(combo);
  30. }
Step 2: Creating Custom Cursor with image Sets the current cursor for this component to be a custom cursor defined by an image resource. If the image resource can't be found, then the component's cursor is not changed. Hotspot of the cursor is always set to be the point (7,7) of the image. Toolkit class' createImage() method is used to create the Image from the URL specified and stored in Image reference. Toolkit's createCustomCursor() method creates a cursor with specified image, hotspot and name of the cursor.
  1. private void useCustomCursor(String imageResourceName) {
  2. ClassLoader cl = CursorComboBoxDemo.class.getClassLoader();
  3. URL resourceURL = cl.getResource(imageResourceName);
  4. if (resourceURL != null) {
  5. Toolkit toolkit = Toolkit.getDefaultToolkit();
  6. Image image = toolkit.createImage(resourceURL);
  7. Point hotSpot = new Point(7,7);
  8. Cursor cursor = toolkit.createCustomCursor(image, hotSpot, "smiley"); //creates a cursor with specified image, hotspot and name of the cursor.
  9. setCursor(cursor); //change the curremt cursor
  10. }
  11. }
Step 3: Defining main() method Finally frame is created and above panel(CursorComboBoxDemo) is added to the frame. Different properties are set. Frame is centered on the screen as follows.
  1. public static void main(String[] args) {
  2. JFrame window = new JFrame("CursorDemo");
  3. CursorComboBoxDemo content = new CursorComboBoxDemo();
  4. window.setContentPane(content);
  5. window.setSize(400,200); //sets size of the frame
  6. window.setResizable(false);
  7. Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  8. window.setLocation( (screenSize.width - window.getWidth())/2,
  9. (screenSize.height - window.getHeight())/2 );
  10. window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  11. window.setVisible(true);
  12. }

Add new comment