code an Event?

JList right-click select

Page 1 of 1

3 Replies - 3062 Views - Last Post: 03 January 2006 - 07:48 AM Rate Topic: -----

#1 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

code an Event?

Posted 23 December 2005 - 09:57 AM

I am wondering if there is a way to code a MouseEvent, my problem is in a JList, left-click causes the item to be highlighted and invokes my listselection handler, thus updating the new selection. I have a popup menu, thus i check for isPopupTrigger() as well, but on a right-click the item is not highlighted, and no code is called, but my show.menu(etc); is there any way to have the right-click act the same as the left in the JList to select the item, or is there a way to hard code and process a left-click Event, so i can place it before the show line, thus setting all variables before teh menu is shown?
My code is on a stand-alone, thus no code here, but i can add it, tho i do not think it would be of much help here.

Is This A Good Question/Topic? 0
  • +

Replies To: code an Event?

#2 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: code an Event?

Posted 02 January 2006 - 11:00 AM

JTables behave in a similar way. A right-click will not select a cell/row by default. When I want to select a row in my subclass of JTable I pass the MouseEvent to a method I create, called "selectRow( MouseEvent e )."
private void selectRow( MouseEvent e )
	{
    int row = rowAtPoint( e.getPoint() );
    getSelectionModel().setSelectionInterval( row, row );
    // other stuff ...
	}

I haven't looked to closely at JLists, but I believe they have a lot in common with JTables (CellRenderers, CellEditors, and SelectionModels for instance), so this should give you a place to start. Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#3 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: code an Event?

Posted 02 January 2006 - 12:59 PM

Thanks, it is not quite the same in a JList, but very similar, and yes i was using a cellrenderer. What i came up with was this:

private class MouseClick implements MouseListener{
	public void mousePressed(MouseEvent e){
  int index = list.locationToIndex(e.getPoint());
 	 list.setSelectedIndex(index);
 	 selectItem();  //sets item to item selected
 	 //Left Double Click
 	 if(e.getClickCount() == 2){
    //seperate code for this action
 	 }
  }
  public void mouseReleased(MouseEvent e){
 	 //Right Click
 	 if(e.isPopupTrigger()){
    menu.show((Component)e.getSource(), e.getX(), e.getY());
 	 }
  }
  public void mouseExited(MouseEvent e) {}
  public void mouseEntered(MouseEvent e){}
  public void mouseClicked(MouseEvent e){}
	}



my cellrenderer takes care of the rows and columns issue, so i only require to set any part of it to selected.

This does solve my original question, but i still wonder if there is a way to create or "spoof" an event to the dispatcher or kernel without the event actually happening.
Thanks for the help.
Was This Post Helpful? 0
  • +
  • -

#4 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: code an Event?

Posted 03 January 2006 - 07:48 AM

I guess I'm not clear on what you were wanting. What I posted will highlight and select a row on right-click if you call it from you MouseListener. If what you want is to generate MouseEvents then you can use java.awt.Robot.

try
{
   Robot robot = new java.awt.Robot();
   robot.mousePress( InputEvent.BUTTON1_MASK );
   robot.mouseRelease( InputEvent.BUTTON1_MASK );
}
catch ( AWTException e ) {}

Was This Post Helpful? 1

Page 1 of 1