Search code examples
javaarraysswingjbutton

Issue with displaying information on a program using swing, JButtons,etc


I'm making a multiplication table using swing.Its basically made up of JButtons. The table is formed from input from the user. The user selects the size of the table by entering a number. The last thing i need to do with this is create a heading that displays the numbers of the table created. Here is my sample code, if you run it, you'll see that its done for the vertical numbers. How can i get the numbers above and properly formatted to represent each column. Thank you.

package lab7;

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

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class GUIMultiplicationTable{
JFrame theFrame;
int number = 0;
JPanel panel, answerPanel, topPanel, leftPanel;
JLabel answerLabel, topLabel, leftLabel;

private void createAndShowGui(){
    String x;
    do{
        x = JOptionPane.showInputDialog(null, "Enter the number");
        number = Integer.parseInt(x);
    }while (number <= 0);
    theFrame = new JFrame("Multiplication Table");
    panel = new JPanel(new GridLayout(number, number));

    answerPanel = new JPanel();     
    answerLabel = new JLabel();

    topPanel = new JPanel();
    topLabel = new JLabel();

    leftPanel = new JPanel();
    leftLabel = new JLabel();


    for (int i = 0; i < number; i++){
        JLabel blah = new JLabel(Integer.toString(i + 1));
        panel.add(blah);//add center to label
        for (int j = 0; j < number; j++){

            JButton button = new JButton();
            if (i == 0){

                button.setText(String.valueOf(j + 1));
            }
            if (j == 0){
                button.setText(String.valueOf(i + 1));
            }
            for (int k = 1; k < number; k++)
            {
                if (i == k)
                {
                    button.setText(String.valueOf((j + 1) * (k + 1)));
                }
            }
            button.addActionListener(new ButtonsTableActionListener(i, j));
            panel.add(button);
        }   
    }

    answerPanel.add(answerLabel);
    theFrame.add(answerPanel, BorderLayout.SOUTH);

    topPanel.add(topLabel);
    theFrame.add(topPanel, BorderLayout.NORTH);

    theFrame.add(panel);
    theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    theFrame.pack();
    theFrame.setLocationRelativeTo(null);
    theFrame.setVisible(true);
}
public static void main(String[] args){
    SwingUtilities.invokeLater(new Runnable(){
        public void run(){
            GUIMultiplicationTable h = new GUIMultiplicationTable();
            h.createAndShowGui();
        }
    });
}

private class ButtonsTableActionListener implements ActionListener{
    private int theRow, theColumn;

    public ButtonsTableActionListener(int row, int column){
        theRow = row;
        theColumn = column;
    }

    @Override
    public void actionPerformed(ActionEvent e){
        int value = (theRow + 1) * (theColumn + 1);
        answerLabel.setText("The value is: " + value + ".\nI got that by multiplying \n" +    (theRow + 1) + "x" + (theColumn + 1));
    }
};
}

Solution

  • An easy way to do this is to store the position of the button in the ActionListener, you can accomplish this by making your own class extending ActionListener, instead of doing an anonymous class. This way the code executed by the button will already have the information it needs to accomplish whatever you want.

    Also you don't need the array of buttons, just add a button in the panel at a time, and at the same time add the actionListener.

    This is your code cleaned up and working properly. Now, instead of showing a dialog do whatever you want to do.

    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class GUIMultiplicationTable
    {
        JFrame theFrame;
        int number = 0;
        JPanel panel;
    
        private void createAndShowGui()
        {
            String x;
            do
            {
                x = JOptionPane.showInputDialog(null, "Enter the number");
                number = Integer.parseInt(x);
            } while (number <= 0);
            theFrame = new JFrame("Multiplication Table");
            panel = new JPanel(new GridLayout(number, number));
            for (int i = 0; i < number; i++)
            {
                for (int j = 0; j < number; j++)
                {
                    JButton button = new JButton();
                    if (i == 0)
                    {
                        button.setText(String.valueOf(j + 1));
                    }
                    if (j == 0)
                    {
                        button.setText(String.valueOf(i + 1));
                    }
                    for (int k = 1; k < number; k++)
                    {
                        if (i == k)
                        {
                            button.setText(String.valueOf((j + 1) * (k + 1)));
                        }
                    }
                    button.addActionListener(new ButtonsTableActionListener(i, j));
                    panel.add(button);
                }
            }
            theFrame.add(panel);
            theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            theFrame.pack();
            theFrame.setLocationRelativeTo(null);
            theFrame.setVisible(true);
    
        }
    
        public static void main(String[] args)
        {
            SwingUtilities.invokeLater(new Runnable() {
                public void run()
                {
                    GUIMultiplicationTable h = new GUIMultiplicationTable();
                    h.createAndShowGui();
                }
            });
    
        }
    
        private class ButtonsTableActionListener implements ActionListener
        {
            private int _row, _column;
    
            public ButtonsTableActionListener(int row, int column)
            {
                _row = row;
                _column = column;
            }
    
            @Override
            public void actionPerformed(ActionEvent e)
            {
                // /do something
                int value = (_row + 1) * (_column + 1);
                String message = "I'm the button in the position (" + _row + ", " + _column + ")\nMy value is " + value + " = " + (_row + 1) + "*" + (_column + 1);
                JOptionPane.showMessageDialog(theFrame, message);
            }
        };
    }