Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You state in comment:</p> <blockquote> <p>The reason why I am not assigning the JButton a reference name is because I am supposed to add an unknown number of JButtons to a JPanel. How I am doing this, is by using a for loop: for (int i = 0; i &lt; numberOfButtonsToAdd; i++) { JPanelName.add(new JButton()); } </p> </blockquote> <p>Then just use either an array or ArrayList of JButton.</p> <pre><code>// in your field declarations List&lt;JButton&gt; buttonList = new ArrayList&lt;JButton&gt;(); // elsewhere in your program for (int i = 0; i &lt; numberOfButtonsToAdd; i++) { JButton button = new JButton(); buttonList.add(button); somePanel.add(button); } </code></pre> <p>Now you have a reference to any button in the list via <code>buttonList.get(someIndex)</code></p> <p>Note also, that most buttons are given ActionListeners that are activated whenever the button is pressed. Without such listeners, the buttons are pretty much useless. You can also get a reference to the pressed JButton from the ActionListener via the ActionEvent object passed into its actionPerformed method:</p> <pre><code>public void actionPerformed(ActionEvent e) { AbstractButton myButton = (AbstractButton) e.getSource(); // now you can use myButton } </code></pre> <p>Note that this is key information that you should have shared with us up front in your original question. </p> <hr> <p><strong>Edit</strong> You state now:</p> <blockquote> <p>and I want all the JButtons to be disabled.</p> </blockquote> <p>Then just make them disabled from the get-go:</p> <pre><code>for (int i = 0; i &lt; numberOfButtonsToAdd; i++) { JButton button = new JButton(); button.setEnabled(false); buttonList.add(button); somePanel.add(button); } </code></pre> <p>Although I am curious -- why all disabled? Why no ActionListener? No text?</p> <hr> <p><strong>Edit 2</strong><br> You state: </p> <blockquote> <p>Would I still be able to access those individual buttons later on, since they are all assigned "button"?</p> </blockquote> <p>Please understand that the variable name is of little importance, and in fact, in my example above, the variable named button does not exist outside of the for loop within which it was declared. Instead what matters is the <strong><em>reference</em></strong> to the JButton object. As has been much discussed in the other answers and as you are aware, this can be obtained by using a variable, but it doesn't have to be a variable directly to the JButton, it could, as is in this case, be the variable to the <code>ArrayList&lt;JButton&gt;</code>. That's why I suggest that you create this entity.</p> <hr> <p><strong>Edit 3</strong><br> for example:</p> <pre><code>import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.util.ArrayList; import java.util.List; import javax.swing.*; public class ButtonLists extends JPanel { private static final int ROWS = 6; private static final int COLS = 6; private List&lt;JButton&gt; allButtons = new ArrayList&lt;JButton&gt;(); private List&lt;JButton&gt; evenButtons = new ArrayList&lt;JButton&gt;(); public ButtonLists() { JPanel gridPanel = new JPanel(new GridLayout(ROWS, COLS)); for (int i = 0; i &lt; ROWS; i++) { for (int j = 0; j &lt; COLS; j++) { int buttonInt = i * COLS + j; String buttonString = String.valueOf(buttonInt); JButton button = new JButton(buttonString); button.setEnabled(false); gridPanel.add(button); allButtons.add(button); if (buttonInt % 2 == 0) { evenButtons.add(button); } } } JPanel bottomPanel = new JPanel(); bottomPanel.add(new JButton(new AbstractAction("Activate All") { @Override public void actionPerformed(ActionEvent e) { for (JButton btn : allButtons) { btn.setEnabled(true); } } })); bottomPanel.add(new JButton(new AbstractAction("Activate Even") { @Override public void actionPerformed(ActionEvent e) { for (JButton btn : allButtons) { btn.setEnabled(false); } for (JButton btn : evenButtons) { btn.setEnabled(true); } } })); bottomPanel.add(new JButton(new AbstractAction("Activate Odd") { @Override public void actionPerformed(ActionEvent e) { for (JButton btn : allButtons) { btn.setEnabled(true); } for (JButton btn : evenButtons) { btn.setEnabled(false); } } })); setLayout(new BorderLayout()); add(gridPanel, BorderLayout.CENTER); add(bottomPanel, BorderLayout.PAGE_END); } private static void createAndShowGui() { JFrame frame = new JFrame("ButtonLists"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new ButtonLists()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGui(); } }); } } </code></pre>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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