0

how can ask if a certain button from the array belongs to the matriz? this for example.

JButton matrix = new JButton[8][8];

how can ask if a certain button from the array belongs to the matriz? this for example.

matrix[i+2][j-1]

any help will greatly appreciated i am really new to java and nothing of this was explained to me. So i have been struggling a lot.

2 Answers 2

1

The best solution is to only give the ActionListener (or better AbstractAction) for the array type of buttons to the JButtons that are in fact in the array. In other words, separate ActionListeners or AbstractActions for separate behaviors. Divide and conquer!

For example:

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

@SuppressWarnings("serial")
public class GuiEg extends JPanel {
   private static final String[][] BUTTON_TXTS = { 
      { "1", "2", "3" },
      { "4", "5", "6" }, 
      { "7", "8", "9" }, 
      { "", "0", "" } };
   private JButton[][] calcButtons = new JButton[BUTTON_TXTS.length][BUTTON_TXTS[0].length];

   public GuiEg() {
      // get rid of magic numbers
      setLayout(new BorderLayout(5, 5));
      add(createEastPanel(), BorderLayout.EAST);
      add(createCenterPanel(), BorderLayout.CENTER);
   }

   private JComponent createEastPanel() {
      JPanel eastPanel = new JPanel(new GridLayout(0, 1));
      eastPanel.add(new JButton(new AbstractAction("Start") {

         @Override
         public void actionPerformed(ActionEvent e) {
            System.out.println("Start has been pressed!");
         }
      }));
      eastPanel.add(new JButton(new AbstractAction("Stop") {

         @Override
         public void actionPerformed(ActionEvent e) {
            System.out.println("Stop has been pressed!");
         }
      }));
      eastPanel.add(new JButton(new AbstractAction("Pause") {

         @Override
         public void actionPerformed(ActionEvent e) {
            System.out.println("Pause has been pressed!");
         }
      }));
      eastPanel.add(new JButton(new AbstractAction("Exit") {

         @Override
         public void actionPerformed(ActionEvent e) {
            Window win = SwingUtilities.getWindowAncestor(GuiEg.this);
            win.dispose();
         }
      }));
      return eastPanel;
   }

   private JComponent createCenterPanel() {
      int rows = BUTTON_TXTS.length;
      int cols = BUTTON_TXTS[0].length;
      JPanel centerPanel = new JPanel(new GridLayout(rows , cols));
      ActionListener calcListener = new CalcListener(); 
      for (int r = 0; r < BUTTON_TXTS.length; r++) {
         for (int c = 0; c < BUTTON_TXTS[r].length; c++) {
            String text = BUTTON_TXTS[r][c];
            if (!text.isEmpty()) {
               calcButtons[r][c] = new JButton(text);
               calcButtons[r][c].addActionListener(calcListener);
               centerPanel.add(calcButtons[r][c]);
            } else {
               centerPanel.add(new JLabel());
            }
         }
      }
      return centerPanel;
   }

   private class CalcListener implements ActionListener {
      @Override
      public void actionPerformed(ActionEvent evt) {
         System.out.println(evt.getActionCommand() + " button pressed!");
      }
   }

   private static void createAndShowGui() {
      GuiEg mainPanel = new GuiEg();

      JFrame frame = new JFrame("GuiEg");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Simple nested for loop?

public boolean hasButton(JButton button)
{
    for(int i = 0; i < matrix.length; i++)
    {
        if(matrix[i] != null) for(int j = 0; j < matrix[i].length; j++)
        {
            if(button == matrix[i][j]) return true;
        }
    }

    return false;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.