• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Q: 'MyComboBox extends JComboBox', to include the missing doClick() method

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good day.

In my Swing application, I have two JComboBoxes (energyListSelect and powerRangeSelect). I had like that the 'ActionEvent e' resulting from a selection in one on them (energyListSelect) also triggers the code execution residing currently in of the other one (powerRangeSelect | a 'switch - cases'). More precisely : executing the code corresponding to the selection previously done in that secund JComboBox (powerRangeSelect) (without selection change in it).

Issue : The JComboBox API doesn't offer a 'doClick()' method (like JButton, ...) --> Making an own JComboBox featuring a 'doClick()' method and inheriting from JComboBox.
This solution seems cleaner than adding a layer of methods to my yet heavy code, called by the 'switch - cases' of both of my JComboBoxes and calling themselves the methods that are currently called directly from my JComboBoxes.

But which code should I implement in myComboBox to implement a 'doClick()' method ?
I recall that I need to implement in some cases of the switch of my energyListSelect (JComboBox) a 'powerRangeSelect.doClick();' statement that triggers an 'ActionEvent e' in powerRangeSelect (MyComboBox).

Thanks in advance.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not sure what you are trying to do, a bit confused by the description, but this:

Making an own JComboBox featuring a 'doClick()' method and inheriting from JComboBox. This solution seems cleaner than adding a layer of methods to my yet heavy code, called by the 'switch - cases' of both of my JComboBoxes and calling themselves the methods that are currently called directly from my JComboBoxes.


Sounds very much wrong to me. Why is it cleaner to create a new class which only adds a new method (not clear exactly how you would use that method) which doesn't actually describe the action (doClick() simulate clicks, comboboxs produce item selections) instead of creating a single method both action handlers call? But I guess it doesn't matter, I don't think. I think you would be better off creating Actions for the JComboBox. Produce one Action which does the work you want, instead of two ActionListeners. Assign the one Action to both JComboBoxes. Then the Action is invoked whenever either JComboBox is changed. I think that is what you wanted.

Alternately, if you want it to go one way (energyListSelect triggers powerRangeSelect's Action while also doing its own thing, but powerRangeSelect should not trigger energyListSelect's Action) then produce two actions, the EnergyListSelectAction is assigned to energyListSelect, and PowerRangeSelectAction is set to powerRangeSelect as well as passed to EnergyListSelectAction. When EnergyListSelectAction is called it also calls PowerRangeSelectAction.
 
Charles Van Damme
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK and thanks, Steve Luke.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic