Multiple clickable JPanels












0















I've currently got 108 auto generated jPanels, each containting a random number.
Is there a way to easily make each of these clickable?
Here's my code..



Creating JPanels:



        for (int row = 0; row < numbers.length; row++) 
{
for (int col = 0; col < numbers[row].length; col++)
{
int tempNumber = (int)(Math.random() * 9 + 1);
numbers[row][col] = tempNumber;
np1 = new NumberPanel(tempNumber);
np1.setLocation(row*np1.getWidth(), row*getWidth());
add(np1);
}
}


The number panel class:



     public NumberPanel(int randomNumber)
{
String number = Integer.toString(randomNumber);
setPreferredSize(new Dimension(40, 40));
setBackground(Color.red);
JLabel label = new JLabel(number, JLabel.LEFT);
label.setFont(new Font("Serif", Font.BOLD, 35));
add(label);


}









share|improve this question



























    0















    I've currently got 108 auto generated jPanels, each containting a random number.
    Is there a way to easily make each of these clickable?
    Here's my code..



    Creating JPanels:



            for (int row = 0; row < numbers.length; row++) 
    {
    for (int col = 0; col < numbers[row].length; col++)
    {
    int tempNumber = (int)(Math.random() * 9 + 1);
    numbers[row][col] = tempNumber;
    np1 = new NumberPanel(tempNumber);
    np1.setLocation(row*np1.getWidth(), row*getWidth());
    add(np1);
    }
    }


    The number panel class:



         public NumberPanel(int randomNumber)
    {
    String number = Integer.toString(randomNumber);
    setPreferredSize(new Dimension(40, 40));
    setBackground(Color.red);
    JLabel label = new JLabel(number, JLabel.LEFT);
    label.setFont(new Font("Serif", Font.BOLD, 35));
    add(label);


    }









    share|improve this question

























      0












      0








      0








      I've currently got 108 auto generated jPanels, each containting a random number.
      Is there a way to easily make each of these clickable?
      Here's my code..



      Creating JPanels:



              for (int row = 0; row < numbers.length; row++) 
      {
      for (int col = 0; col < numbers[row].length; col++)
      {
      int tempNumber = (int)(Math.random() * 9 + 1);
      numbers[row][col] = tempNumber;
      np1 = new NumberPanel(tempNumber);
      np1.setLocation(row*np1.getWidth(), row*getWidth());
      add(np1);
      }
      }


      The number panel class:



           public NumberPanel(int randomNumber)
      {
      String number = Integer.toString(randomNumber);
      setPreferredSize(new Dimension(40, 40));
      setBackground(Color.red);
      JLabel label = new JLabel(number, JLabel.LEFT);
      label.setFont(new Font("Serif", Font.BOLD, 35));
      add(label);


      }









      share|improve this question














      I've currently got 108 auto generated jPanels, each containting a random number.
      Is there a way to easily make each of these clickable?
      Here's my code..



      Creating JPanels:



              for (int row = 0; row < numbers.length; row++) 
      {
      for (int col = 0; col < numbers[row].length; col++)
      {
      int tempNumber = (int)(Math.random() * 9 + 1);
      numbers[row][col] = tempNumber;
      np1 = new NumberPanel(tempNumber);
      np1.setLocation(row*np1.getWidth(), row*getWidth());
      add(np1);
      }
      }


      The number panel class:



           public NumberPanel(int randomNumber)
      {
      String number = Integer.toString(randomNumber);
      setPreferredSize(new Dimension(40, 40));
      setBackground(Color.red);
      JLabel label = new JLabel(number, JLabel.LEFT);
      label.setFont(new Font("Serif", Font.BOLD, 35));
      add(label);


      }






      java graphics jpanel






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jun 5 '14 at 0:12









      LarmLarm

      817




      817
























          4 Answers
          4






          active

          oldest

          votes


















          1














          Why are you creating a panel to contain a JLabel? Why not just add the label directly to the parent panel?



          Instead of using a JLabel to display the random number use a JButton. Then you can add an ActionListener to each of the buttons.



          You can make the button look like a label by using:



          button.setBorderPainted( false );


          So basically, instead of creating 108 panels that contain a JLabel, you just create 108 JButtons and add the buttons directly to the parent panel.






          share|improve this answer


























          • I can do buttons.. But because they're created through a loop, how would I go about having a mouse click?

            – Larm
            Jun 5 '14 at 0:34











          • I already stated that you would add an ActionListener to the buttons. You can add the same ActionListener to every button. In the ActionListener code you use the getSource() method of the ActionEvent to get the button that was clicked.

            – camickr
            Jun 5 '14 at 0:44













          • I get that, but how will it actually know which button seeing as won't they all be created with the same name 'np1'?

            – Larm
            Jun 5 '14 at 0:51











          • @Liam Use getActionCommand from the ActionEvent passed to the actionPeformed method or cast the source of the ActionEvent to a JButton and use getText. Alterntivly, you could place the buttons in an array or List and look them up (comparing the source of the ActionEvent with those elements in the list)

            – MadProgrammer
            Jun 5 '14 at 1:34











          • but how will it actually know which button - I already stated that the getSource() method will return the button that was clicked. All you need to do is cast the object to a JButton. ie. JButton button = (JButton)event.getSource();

            – camickr
            Jun 5 '14 at 2:21



















          1














          First of all, I think you should avoid using Absolute Position Layout (null layout). As you already know the number of rows and columns, it would be easier to have a GridLayout and keep adding components accordingly.



          On the other hand, just add an ActionListener and use the getSource() from the Event to get whatever was clicked. With the panel (or label) clicked, you can get the Text to know whats its value.



          Assuming the ActionListener was added to the JLabel, you can do:



          if (evt.getSource() instanceof JLabel) {
          Integer value = Integer.valueOf(((JLabel) evt.getSource()).getText());
          }


          I actually like more the idea to have everything decoupled, so I'll just send a FirePropertyChange and receive it wherever I need to process the value.






          share|improve this answer



















          • 1





            Assuming the ActionListener was added to the JLabel - you can't add an ActionListener to a JLabel. just add an ActionListener and use the getSource() from the Event - as has already been suggested, although the OP also would need to use a JButton for this so you can use an ActionListener.

            – camickr
            Jun 5 '14 at 2:13











          • You are right, I was meant to say MouseListener (MouseAdapter->MouseClicked) if its the case he wants to use a JLabel. thx for pointing this out.

            – JDDelgado
            Jun 5 '14 at 6:13



















          0














          If NumberPanel extends from JPanel, inside the constructor you can add Mouse listener.



          addMouseListener(new MouseListener() {
          ....
          });






          share|improve this answer































            0














            If you want to have clickable JPanel with saved "state" you can use JToggleButton:



            public class NumberButton extends JToggleButton {

            public NumberButton(int randomNumber) {
            setBorder(BorderFactory.createEmptyBorder());
            String number = Integer.toString(randomNumber);
            setText(number);
            setFont(new Font("Serif", Font.BOLD, 35));
            setPreferredSize(new Dimension(40, 40));
            setBackground(Color.red);
            }


            }






            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f24049657%2fmultiple-clickable-jpanels%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              4 Answers
              4






              active

              oldest

              votes








              4 Answers
              4






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              Why are you creating a panel to contain a JLabel? Why not just add the label directly to the parent panel?



              Instead of using a JLabel to display the random number use a JButton. Then you can add an ActionListener to each of the buttons.



              You can make the button look like a label by using:



              button.setBorderPainted( false );


              So basically, instead of creating 108 panels that contain a JLabel, you just create 108 JButtons and add the buttons directly to the parent panel.






              share|improve this answer


























              • I can do buttons.. But because they're created through a loop, how would I go about having a mouse click?

                – Larm
                Jun 5 '14 at 0:34











              • I already stated that you would add an ActionListener to the buttons. You can add the same ActionListener to every button. In the ActionListener code you use the getSource() method of the ActionEvent to get the button that was clicked.

                – camickr
                Jun 5 '14 at 0:44













              • I get that, but how will it actually know which button seeing as won't they all be created with the same name 'np1'?

                – Larm
                Jun 5 '14 at 0:51











              • @Liam Use getActionCommand from the ActionEvent passed to the actionPeformed method or cast the source of the ActionEvent to a JButton and use getText. Alterntivly, you could place the buttons in an array or List and look them up (comparing the source of the ActionEvent with those elements in the list)

                – MadProgrammer
                Jun 5 '14 at 1:34











              • but how will it actually know which button - I already stated that the getSource() method will return the button that was clicked. All you need to do is cast the object to a JButton. ie. JButton button = (JButton)event.getSource();

                – camickr
                Jun 5 '14 at 2:21
















              1














              Why are you creating a panel to contain a JLabel? Why not just add the label directly to the parent panel?



              Instead of using a JLabel to display the random number use a JButton. Then you can add an ActionListener to each of the buttons.



              You can make the button look like a label by using:



              button.setBorderPainted( false );


              So basically, instead of creating 108 panels that contain a JLabel, you just create 108 JButtons and add the buttons directly to the parent panel.






              share|improve this answer


























              • I can do buttons.. But because they're created through a loop, how would I go about having a mouse click?

                – Larm
                Jun 5 '14 at 0:34











              • I already stated that you would add an ActionListener to the buttons. You can add the same ActionListener to every button. In the ActionListener code you use the getSource() method of the ActionEvent to get the button that was clicked.

                – camickr
                Jun 5 '14 at 0:44













              • I get that, but how will it actually know which button seeing as won't they all be created with the same name 'np1'?

                – Larm
                Jun 5 '14 at 0:51











              • @Liam Use getActionCommand from the ActionEvent passed to the actionPeformed method or cast the source of the ActionEvent to a JButton and use getText. Alterntivly, you could place the buttons in an array or List and look them up (comparing the source of the ActionEvent with those elements in the list)

                – MadProgrammer
                Jun 5 '14 at 1:34











              • but how will it actually know which button - I already stated that the getSource() method will return the button that was clicked. All you need to do is cast the object to a JButton. ie. JButton button = (JButton)event.getSource();

                – camickr
                Jun 5 '14 at 2:21














              1












              1








              1







              Why are you creating a panel to contain a JLabel? Why not just add the label directly to the parent panel?



              Instead of using a JLabel to display the random number use a JButton. Then you can add an ActionListener to each of the buttons.



              You can make the button look like a label by using:



              button.setBorderPainted( false );


              So basically, instead of creating 108 panels that contain a JLabel, you just create 108 JButtons and add the buttons directly to the parent panel.






              share|improve this answer















              Why are you creating a panel to contain a JLabel? Why not just add the label directly to the parent panel?



              Instead of using a JLabel to display the random number use a JButton. Then you can add an ActionListener to each of the buttons.



              You can make the button look like a label by using:



              button.setBorderPainted( false );


              So basically, instead of creating 108 panels that contain a JLabel, you just create 108 JButtons and add the buttons directly to the parent panel.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Jun 5 '14 at 0:24

























              answered Jun 5 '14 at 0:17









              camickrcamickr

              277k16127241




              277k16127241













              • I can do buttons.. But because they're created through a loop, how would I go about having a mouse click?

                – Larm
                Jun 5 '14 at 0:34











              • I already stated that you would add an ActionListener to the buttons. You can add the same ActionListener to every button. In the ActionListener code you use the getSource() method of the ActionEvent to get the button that was clicked.

                – camickr
                Jun 5 '14 at 0:44













              • I get that, but how will it actually know which button seeing as won't they all be created with the same name 'np1'?

                – Larm
                Jun 5 '14 at 0:51











              • @Liam Use getActionCommand from the ActionEvent passed to the actionPeformed method or cast the source of the ActionEvent to a JButton and use getText. Alterntivly, you could place the buttons in an array or List and look them up (comparing the source of the ActionEvent with those elements in the list)

                – MadProgrammer
                Jun 5 '14 at 1:34











              • but how will it actually know which button - I already stated that the getSource() method will return the button that was clicked. All you need to do is cast the object to a JButton. ie. JButton button = (JButton)event.getSource();

                – camickr
                Jun 5 '14 at 2:21



















              • I can do buttons.. But because they're created through a loop, how would I go about having a mouse click?

                – Larm
                Jun 5 '14 at 0:34











              • I already stated that you would add an ActionListener to the buttons. You can add the same ActionListener to every button. In the ActionListener code you use the getSource() method of the ActionEvent to get the button that was clicked.

                – camickr
                Jun 5 '14 at 0:44













              • I get that, but how will it actually know which button seeing as won't they all be created with the same name 'np1'?

                – Larm
                Jun 5 '14 at 0:51











              • @Liam Use getActionCommand from the ActionEvent passed to the actionPeformed method or cast the source of the ActionEvent to a JButton and use getText. Alterntivly, you could place the buttons in an array or List and look them up (comparing the source of the ActionEvent with those elements in the list)

                – MadProgrammer
                Jun 5 '14 at 1:34











              • but how will it actually know which button - I already stated that the getSource() method will return the button that was clicked. All you need to do is cast the object to a JButton. ie. JButton button = (JButton)event.getSource();

                – camickr
                Jun 5 '14 at 2:21

















              I can do buttons.. But because they're created through a loop, how would I go about having a mouse click?

              – Larm
              Jun 5 '14 at 0:34





              I can do buttons.. But because they're created through a loop, how would I go about having a mouse click?

              – Larm
              Jun 5 '14 at 0:34













              I already stated that you would add an ActionListener to the buttons. You can add the same ActionListener to every button. In the ActionListener code you use the getSource() method of the ActionEvent to get the button that was clicked.

              – camickr
              Jun 5 '14 at 0:44







              I already stated that you would add an ActionListener to the buttons. You can add the same ActionListener to every button. In the ActionListener code you use the getSource() method of the ActionEvent to get the button that was clicked.

              – camickr
              Jun 5 '14 at 0:44















              I get that, but how will it actually know which button seeing as won't they all be created with the same name 'np1'?

              – Larm
              Jun 5 '14 at 0:51





              I get that, but how will it actually know which button seeing as won't they all be created with the same name 'np1'?

              – Larm
              Jun 5 '14 at 0:51













              @Liam Use getActionCommand from the ActionEvent passed to the actionPeformed method or cast the source of the ActionEvent to a JButton and use getText. Alterntivly, you could place the buttons in an array or List and look them up (comparing the source of the ActionEvent with those elements in the list)

              – MadProgrammer
              Jun 5 '14 at 1:34





              @Liam Use getActionCommand from the ActionEvent passed to the actionPeformed method or cast the source of the ActionEvent to a JButton and use getText. Alterntivly, you could place the buttons in an array or List and look them up (comparing the source of the ActionEvent with those elements in the list)

              – MadProgrammer
              Jun 5 '14 at 1:34













              but how will it actually know which button - I already stated that the getSource() method will return the button that was clicked. All you need to do is cast the object to a JButton. ie. JButton button = (JButton)event.getSource();

              – camickr
              Jun 5 '14 at 2:21





              but how will it actually know which button - I already stated that the getSource() method will return the button that was clicked. All you need to do is cast the object to a JButton. ie. JButton button = (JButton)event.getSource();

              – camickr
              Jun 5 '14 at 2:21













              1














              First of all, I think you should avoid using Absolute Position Layout (null layout). As you already know the number of rows and columns, it would be easier to have a GridLayout and keep adding components accordingly.



              On the other hand, just add an ActionListener and use the getSource() from the Event to get whatever was clicked. With the panel (or label) clicked, you can get the Text to know whats its value.



              Assuming the ActionListener was added to the JLabel, you can do:



              if (evt.getSource() instanceof JLabel) {
              Integer value = Integer.valueOf(((JLabel) evt.getSource()).getText());
              }


              I actually like more the idea to have everything decoupled, so I'll just send a FirePropertyChange and receive it wherever I need to process the value.






              share|improve this answer



















              • 1





                Assuming the ActionListener was added to the JLabel - you can't add an ActionListener to a JLabel. just add an ActionListener and use the getSource() from the Event - as has already been suggested, although the OP also would need to use a JButton for this so you can use an ActionListener.

                – camickr
                Jun 5 '14 at 2:13











              • You are right, I was meant to say MouseListener (MouseAdapter->MouseClicked) if its the case he wants to use a JLabel. thx for pointing this out.

                – JDDelgado
                Jun 5 '14 at 6:13
















              1














              First of all, I think you should avoid using Absolute Position Layout (null layout). As you already know the number of rows and columns, it would be easier to have a GridLayout and keep adding components accordingly.



              On the other hand, just add an ActionListener and use the getSource() from the Event to get whatever was clicked. With the panel (or label) clicked, you can get the Text to know whats its value.



              Assuming the ActionListener was added to the JLabel, you can do:



              if (evt.getSource() instanceof JLabel) {
              Integer value = Integer.valueOf(((JLabel) evt.getSource()).getText());
              }


              I actually like more the idea to have everything decoupled, so I'll just send a FirePropertyChange and receive it wherever I need to process the value.






              share|improve this answer



















              • 1





                Assuming the ActionListener was added to the JLabel - you can't add an ActionListener to a JLabel. just add an ActionListener and use the getSource() from the Event - as has already been suggested, although the OP also would need to use a JButton for this so you can use an ActionListener.

                – camickr
                Jun 5 '14 at 2:13











              • You are right, I was meant to say MouseListener (MouseAdapter->MouseClicked) if its the case he wants to use a JLabel. thx for pointing this out.

                – JDDelgado
                Jun 5 '14 at 6:13














              1












              1








              1







              First of all, I think you should avoid using Absolute Position Layout (null layout). As you already know the number of rows and columns, it would be easier to have a GridLayout and keep adding components accordingly.



              On the other hand, just add an ActionListener and use the getSource() from the Event to get whatever was clicked. With the panel (or label) clicked, you can get the Text to know whats its value.



              Assuming the ActionListener was added to the JLabel, you can do:



              if (evt.getSource() instanceof JLabel) {
              Integer value = Integer.valueOf(((JLabel) evt.getSource()).getText());
              }


              I actually like more the idea to have everything decoupled, so I'll just send a FirePropertyChange and receive it wherever I need to process the value.






              share|improve this answer













              First of all, I think you should avoid using Absolute Position Layout (null layout). As you already know the number of rows and columns, it would be easier to have a GridLayout and keep adding components accordingly.



              On the other hand, just add an ActionListener and use the getSource() from the Event to get whatever was clicked. With the panel (or label) clicked, you can get the Text to know whats its value.



              Assuming the ActionListener was added to the JLabel, you can do:



              if (evt.getSource() instanceof JLabel) {
              Integer value = Integer.valueOf(((JLabel) evt.getSource()).getText());
              }


              I actually like more the idea to have everything decoupled, so I'll just send a FirePropertyChange and receive it wherever I need to process the value.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Jun 5 '14 at 1:32









              JDDelgadoJDDelgado

              242312




              242312








              • 1





                Assuming the ActionListener was added to the JLabel - you can't add an ActionListener to a JLabel. just add an ActionListener and use the getSource() from the Event - as has already been suggested, although the OP also would need to use a JButton for this so you can use an ActionListener.

                – camickr
                Jun 5 '14 at 2:13











              • You are right, I was meant to say MouseListener (MouseAdapter->MouseClicked) if its the case he wants to use a JLabel. thx for pointing this out.

                – JDDelgado
                Jun 5 '14 at 6:13














              • 1





                Assuming the ActionListener was added to the JLabel - you can't add an ActionListener to a JLabel. just add an ActionListener and use the getSource() from the Event - as has already been suggested, although the OP also would need to use a JButton for this so you can use an ActionListener.

                – camickr
                Jun 5 '14 at 2:13











              • You are right, I was meant to say MouseListener (MouseAdapter->MouseClicked) if its the case he wants to use a JLabel. thx for pointing this out.

                – JDDelgado
                Jun 5 '14 at 6:13








              1




              1





              Assuming the ActionListener was added to the JLabel - you can't add an ActionListener to a JLabel. just add an ActionListener and use the getSource() from the Event - as has already been suggested, although the OP also would need to use a JButton for this so you can use an ActionListener.

              – camickr
              Jun 5 '14 at 2:13





              Assuming the ActionListener was added to the JLabel - you can't add an ActionListener to a JLabel. just add an ActionListener and use the getSource() from the Event - as has already been suggested, although the OP also would need to use a JButton for this so you can use an ActionListener.

              – camickr
              Jun 5 '14 at 2:13













              You are right, I was meant to say MouseListener (MouseAdapter->MouseClicked) if its the case he wants to use a JLabel. thx for pointing this out.

              – JDDelgado
              Jun 5 '14 at 6:13





              You are right, I was meant to say MouseListener (MouseAdapter->MouseClicked) if its the case he wants to use a JLabel. thx for pointing this out.

              – JDDelgado
              Jun 5 '14 at 6:13











              0














              If NumberPanel extends from JPanel, inside the constructor you can add Mouse listener.



              addMouseListener(new MouseListener() {
              ....
              });






              share|improve this answer




























                0














                If NumberPanel extends from JPanel, inside the constructor you can add Mouse listener.



                addMouseListener(new MouseListener() {
                ....
                });






                share|improve this answer


























                  0












                  0








                  0







                  If NumberPanel extends from JPanel, inside the constructor you can add Mouse listener.



                  addMouseListener(new MouseListener() {
                  ....
                  });






                  share|improve this answer













                  If NumberPanel extends from JPanel, inside the constructor you can add Mouse listener.



                  addMouseListener(new MouseListener() {
                  ....
                  });







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jun 5 '14 at 0:20









                  user3709120user3709120

                  1




                  1























                      0














                      If you want to have clickable JPanel with saved "state" you can use JToggleButton:



                      public class NumberButton extends JToggleButton {

                      public NumberButton(int randomNumber) {
                      setBorder(BorderFactory.createEmptyBorder());
                      String number = Integer.toString(randomNumber);
                      setText(number);
                      setFont(new Font("Serif", Font.BOLD, 35));
                      setPreferredSize(new Dimension(40, 40));
                      setBackground(Color.red);
                      }


                      }






                      share|improve this answer




























                        0














                        If you want to have clickable JPanel with saved "state" you can use JToggleButton:



                        public class NumberButton extends JToggleButton {

                        public NumberButton(int randomNumber) {
                        setBorder(BorderFactory.createEmptyBorder());
                        String number = Integer.toString(randomNumber);
                        setText(number);
                        setFont(new Font("Serif", Font.BOLD, 35));
                        setPreferredSize(new Dimension(40, 40));
                        setBackground(Color.red);
                        }


                        }






                        share|improve this answer


























                          0












                          0








                          0







                          If you want to have clickable JPanel with saved "state" you can use JToggleButton:



                          public class NumberButton extends JToggleButton {

                          public NumberButton(int randomNumber) {
                          setBorder(BorderFactory.createEmptyBorder());
                          String number = Integer.toString(randomNumber);
                          setText(number);
                          setFont(new Font("Serif", Font.BOLD, 35));
                          setPreferredSize(new Dimension(40, 40));
                          setBackground(Color.red);
                          }


                          }






                          share|improve this answer













                          If you want to have clickable JPanel with saved "state" you can use JToggleButton:



                          public class NumberButton extends JToggleButton {

                          public NumberButton(int randomNumber) {
                          setBorder(BorderFactory.createEmptyBorder());
                          String number = Integer.toString(randomNumber);
                          setText(number);
                          setFont(new Font("Serif", Font.BOLD, 35));
                          setPreferredSize(new Dimension(40, 40));
                          setBackground(Color.red);
                          }


                          }







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Jan 2 at 16:33







                          user10827203





































                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f24049657%2fmultiple-clickable-jpanels%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              MongoDB - Not Authorized To Execute Command

                              How to fix TextFormField cause rebuild widget in Flutter

                              in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith