• 0

Button Event Handler in a JList, Please help.


Question

Hi everyone,

I have created a small Java application which has a JList. The JList uses a custom cell renderer I named SmartCellRenderer. The SmartCellRenderer extends JPanel and implements the ListCellRenderer. I have added two buttons on the right side inside the JPanel of the SmartCellRenderer, since I want to buttons for each list item, and I have added mouse/action listeners for both buttons. However, they don't respond. It seems that the JList property overcomes the buttons underneath it. So the buttons never really get clicked because before that happens the JList item is being selected beforehand. I've tried everything. I've put listeners in the Main class, called Editor, which has the JList and also have listeners in the SmartCellRenderer itself and none of them get invoked.

I also tried a manual solution. Every time the event handler for the JList was invoked (this is the handler for the JList itself and not the buttons), I sent the mouse event object to the SmartCellRenderer to manually check if the point the click happened was on one of the buttons in order to handle it.

I used:

// Inside SmartCellRenderer.java

// e is the mouse event object being passed from the Editor whenever

// a JList item is selected or clicked on

Component comp = this.getComponent (e.getX(), e.getY())

if(!(comp instanceof JButton)) {

System.out.println("Recoqnizedt, but not a button click...");

//return;

} else {

System.out.println("Recognizedt, IT IS A MOUSE CLICK, PROCESSING...");

}

System.out.println("VALUEomp.toString());

What I realized is that not only this still doesn't work (it never realizes the component as a JButton) it also throws an exception for the last line saying comp is null. Meaning with the passed x,y position the getComponent() returns a null which happens when the coordinates passed to it are outside the range of the Panel. Which is a whole other problem?

I have yet to find an example on the web, using Google, that demonstrated using buttons inside a JList.

Can anyone help me with this. Thanks.

3 answers to this question

Recommended Posts

  • 0

might help if you post your code....

what you have to do is

create a button, assign the button a listener and then assign to the jlist.

I have no jdk at the moment, but here is some code

class MyList extends JList implements ActionListener
{

    JButton b1 = new JButton();
    JButton b2 = new JButton();

    public MyList()
   {
       super(); 
      b1.addActionListener(this);
      b2.addActionListener(this);
      this.add(b1);
      this.add(b2);
   }

   public actionPerformed(ActionEvent e)
  { 
     System.out.println(e.toString());
   }
}

  • 0

I know the above code isnt what you are looking for, but your key problem is that you have not hooked your buttons into a listener. ie. addActionListener(this).

this is why you never here calls from the button being pressed, because nothing registered to listening for the event

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Sony announces new State of Play games showcase for tomorrow by Pulasthi Ariyasinghe Game publishers love to get their showcases and announcements to happen in June, and following many other companies, Sony has now thrown its hat into the ring as well. There's not long to wait either. The PlayStation maker will be hosting a showcase of its own tomorrow, June 4, focused on titles coming to its platforms. The June State of Play will go live on Wednesday, June 4, at 2pm PT / 5pm ET / 11pm CEST. Fans can catch the show across Sony's YouTube and Twitch channels live. It seems that unlike previous State of Play shows, a Japanese broadcast will not be happening, with only an English stream being confirmed this time. "Watch live for news and updates on must-play games coming to PS5," says the announcement today. "The show highlights a selection of great games from creators across the globe." Other than that, Sony has not given any hints or teasers about what fans can expect to see at the show. We still may get new looks at upcoming first-party titles like Ghost of Yotei from Ghost of Tsushima developer Sucker Punch Productions, Marathon from Destiny studio Bungie, and maybe even Insomniac’s Wolverine title that's set in its Spider-Man universe. Sony still has some live service titles in development that may get some time in the spotlight here too. As always, more PC ports have a chance of being revealed here as well, continuing Sony's multiplatform venture. Following the State of Play showcase, Summer Game Fest will officially kick off with its own event later this week. Microsoft is also preparing to go live with its own Xbox Games Showcase, with a separate Direct stream focused on its next RPG release, The Outer Worlds 2 from Obsidian Entertainment.
    • Hello All Every day MS App store lists the same apps with update available,  select update all, and then think all is fine, and next day same list shows up again.  Any advice on how to fix this for good?   Should i resort to clean install? On the AMD Ryzen 7 7700X Desktop    
    • Battery life for devices like this should - at least - a week. It's not a bl**dy phone you need to charge every day. 'rolls eyes'
  • Recent Achievements

    • Week One Done
      Adam Todd earned a badge
      Week One Done
    • Contributor
      Ed B went up a rank
      Contributor
    • One Month Later
      moporcho earned a badge
      One Month Later
    • One Month Later
      Parotel earned a badge
      One Month Later
    • Reacting Well
      Cryptecks earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      195
    2. 2
      snowy owl
      139
    3. 3
      ATLien_0
      132
    4. 4
      Xenon
      120
    5. 5
      +FloatingFatMan
      104
  • Tell a friend

    Love Neowin? Tell a friend!