Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Create an action listener:</p> <pre><code>ActionListener al = new ActionListener() { public void actionPerformed(ActionEvent evt) { Object src = evt.getSource(); if (evt instanceof JButton) { currentLetter = ((JButton) src).getText(); // do something with letter } // else something seriously wrong } }; </code></pre> <p>Then add this action listener to each of the buttons in <code>alphabet</code>:</p> <pre><code>for (JButton button : alphabet) { button.addActionListener(al); } </code></pre> <p><strong>EDIT</strong> I don't see the implementation of <code>initComponents()</code> in your updated code. Regardless, adding an action listener to each button is something that needs to be done only once. here's how I would start implementing <code>initComponents()</code>:</p> <pre><code>private static String[] letters = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" }; private JButton[] letterButtons = new JButton[letters.length]; private void initComponents() { ActionListener al = new ActionListener() { public void actionPerformed(ActionEvent evt) { Object src = evt.getSource(); if (evt instanceof JButton) { currentLetter = ((JButton) src).getText(); // do something with letter } // else something seriously wrong } }; for (int i = 0; i &lt; letters.length; ++i) { letterButtons[i] = new JButton(letters[i]); letterButtons[i].addActionListener(al); } . . . // add all the buttons to the user interface . . . } </code></pre> <p>Please simplify your code by using more arrays! I certainly would not have a separate variable for each letter button or 26 separate methods to deal separately with each letter click.</p>
 

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