Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do you access items in an uneditable JComboBox so a method can be run upon selection?
    primarykey
    data
    text
    <p>I know a little Python but unfortunately no Java. I'm trying to get a minimal Jython example to work, where a method is executed each time an item in my JComboBox is selected. Instinct tells me I should set an actionPerformed = object for each item, where object is the name of a method I want to run. I found an example online for JMenuItems, and I got this to work:</p> <pre><code>menubar = swing.JMenuBar() optionMenu = swing.JMenu('Option') optionsList = ['First', 'Second', 'Third'] for option in optionsList: choice = swing.JMenuItem(option) choice.actionPerformed = self.doUponSelection optionMenu.add(choice) menuBar.add(optionMenu) </code></pre> <p>When I tried something similar with swing.JComboBoxItem, it told me no such attribute existed. Using <code>dir()</code> I found javax.swing attributes of JCheckBoxMenuItem, JMenuItem, and JRadioButtonMenuItem. Does anyone know how I can access the components of a JList or JComboBox?</p> <p>Note: I have access to Jython 2.5.0</p> <p>Edit: I've seen the Java documentation (not that I understand the code) that describes explicitly adding an actionListener but did not think that was supposed to be done in Jython. I know Jython JButtons have an actionPerformed attribute which appears to implement an actionListener and executes actions (combining Java steps). If it is the JComboBox itself that will fire the event, I'm even more confused because I tried using <code>myJComboBox.actionPerformed = self.doUponSelection</code> and it gave me a TypeError saying it was a read only attribute. It works fine for me with <code>myJButton.actionPerformed = self.doUponSelection</code>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload