1

So for this class in java, here is my code

import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
import javax.swing.border.*;

public class RadioSelection extends JFrame implements ActionListener
{
private ActionListener action;  
private JButton[][] button;
private JPanel bottomPanel;
private LineBorder lineBorder;

private int randomRowLimit;
private int randomColumnLimit;
private Random random;
private int size;
JLabel label = new JLabel("Select the no. of Grid");
public RadioSelection()
{
    randomRowLimit = 0;
    randomColumnLimit = 0;
    random = new Random();
    size = 0;

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationByPlatform(true);

    lineBorder = new LineBorder(Color.BLUE.darker());


    JPanel topPanel = new JPanel();
    topPanel.setLayout(new FlowLayout(FlowLayout.LEFT));

    bottomPanel = new JPanel();

    final JRadioButton threeSquareButton = new JRadioButton("3 X 3", false);
    final JRadioButton fourSquareButton = new JRadioButton("4 X 4", false);
    final JRadioButton fiveSquareButton = new JRadioButton("5 X 5", false);

    threeSquareButton.setBorder(lineBorder);
    fourSquareButton.setBorder(lineBorder);
    fiveSquareButton.setBorder(lineBorder);
    label.setFont(null);
    action = new ActionListener()
    {
        public void actionPerformed(ActionEvent ae)
        {

            if (ae.getSource() == threeSquareButton)
            {
                remove(bottomPanel);
                bottomPanel = getCenterPanel(3);
                add(bottomPanel, BorderLayout.CENTER);
            }
            else if (ae.getSource() == fourSquareButton)
            {
                remove(bottomPanel);
                bottomPanel = getCenterPanel(4);
                add(bottomPanel, BorderLayout.CENTER);
            }
            else if (ae.getSource() == fiveSquareButton)
            {

                remove(bottomPanel);
                bottomPanel = getCenterPanel(5);
                add(bottomPanel, BorderLayout.CENTER);
            }


            invalidate(); // If you are using JDK 1.7 +
            //getContentPane().revalidate(); // if you using JDK 1.6 or lower
              repaint();
          }
      };

    threeSquareButton.addActionListener(action);
    fourSquareButton.addActionListener(action);
    fiveSquareButton.addActionListener(action);

    ButtonGroup bg = new ButtonGroup();
    bg.add(threeSquareButton);
    bg.add(fourSquareButton);
    bg.add(fiveSquareButton);


    topPanel.add(label);
    topPanel.add(threeSquareButton);
    topPanel.add(fourSquareButton);
    topPanel.add(fiveSquareButton);

    add(topPanel, BorderLayout.PAGE_START);

    add(bottomPanel, BorderLayout.CENTER);

    setSize(300, 300);
    //pack();
    setVisible(true);
 }
 private JPanel getCenterPanel(int size)
 {
    JPanel bottomPanel = new JPanel(new GridLayout(size, size));
    button = new JButton[size][size];
    this.size = size;

    for (int row = 0; row < size; row++)
    {
        for (int column = 0; column < size; column++)
        {
            button[row][column] = new JButton();
            button[row][column].setBorder(lineBorder);
            button[row][column].setMargin(new Insets(2, 2, 2, 2));
            button[row][column].addActionListener(this);
            bottomPanel.add(button[row][column]);
        }
    }

    randomRowLimit = random.nextInt(size);
    randomColumnLimit = random.nextInt(size);
    button[randomRowLimit][randomColumnLimit].setText("mouse");

    return bottomPanel;
}

 public void actionPerformed(ActionEvent ae)
{
    JButton button = (JButton) ae.getSource();

    if ((button.getText()).equals("mouse"))
    {
        randomRowLimit = random.nextInt(size);
        randomColumnLimit = random.nextInt(size);
        System.out.println("Row : " + randomRowLimit);
        System.out.println("Column : " + randomColumnLimit);
        button.setText("");
        this.button[randomRowLimit][randomColumnLimit].setText("mouse");            
    }
    else
    {
   JOptionPane.showMessageDialog(this, "Catch the mouse!", "Small Game : ",                     JOptionPane.ERROR_MESSAGE);
    }
}

public static void main(String... args)
{
    SwingUtilities.invokeLater(new Runnable()
    {
        public void run()
        {

            new RadioSelection();
        }
    });
}

}

This one is working but if you notice, I used invalidate(); here. If it is revalidate();, it will not run. However, my concern here is when a radio-button is clicked (e.g 3x3), the button will not show automatically. The frame should be adjusted first before the gird buttons appear. How can I work with this one?

Shahbaz
  • 46,337
  • 19
  • 116
  • 182

2 Answers2

3

Try your hands on this code :

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class RadioSelection extends JFrame
{
    private ActionListener action;  
    private JPanel bottomPanel;
    private LineBorder lineBorder ;

    public RadioSelection()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationByPlatform(true);

        lineBorder = new LineBorder(Color.BLUE.darker());

        JPanel topPanel = new JPanel();
        topPanel.setLayout(new FlowLayout(FlowLayout.LEFT));

        bottomPanel = new JPanel();

        final JRadioButton threeSquareButton = new JRadioButton("3 X 3", false);
        final JRadioButton fourSquareButton = new JRadioButton("4 X 4", false);

        threeSquareButton.setBorder(lineBorder);
        fourSquareButton.setBorder(lineBorder);

        action = new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                if (ae.getSource() == threeSquareButton)
                {
                    remove(bottomPanel);
                    bottomPanel = getCenterPanel(3);
                    add(bottomPanel, BorderLayout.CENTER);
                }
                else if (ae.getSource() == fourSquareButton)
                {
                    remove(bottomPanel);
                    bottomPanel = getCenterPanel(4);
                    add(bottomPanel, BorderLayout.CENTER);
                }
                revalidate(); // If you are using JDK 1.7 +
                // getContentPane().revalidate(); // if you using JDK 1.6 or lower
                repaint();
            }
        };

        threeSquareButton.addActionListener(action);
        fourSquareButton.addActionListener(action);

        ButtonGroup bg = new ButtonGroup();
        bg.add(threeSquareButton);
        bg.add(fourSquareButton);

        topPanel.add(threeSquareButton);
        topPanel.add(fourSquareButton);

        add(topPanel, BorderLayout.PAGE_START);
        add(bottomPanel, BorderLayout.CENTER);

        setSize(300, 300);
        setVisible(true);
    }

    private JPanel getCenterPanel(int size)
    {
        JPanel bottomPanel = new JPanel(new GridLayout(size, size));

        for (int row = 0; row < size; row++)
        {
            for (int column = 0; column < size; column++)
            {
                JButton button = new JButton("Button " + row + " " + column);
                button.setBorder(lineBorder);
                button.setMargin(new Insets(2, 2, 2, 2));
                bottomPanel.add(button);
            }
        }

        return bottomPanel;
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new RadioSelection();
            }
        });
    }
}

Here is the output of this :

3 X 3 MATRIX and 4 X 4 MATRIX

Added this new code to answer a new thing :

import java.awt.*;
import java.awt.event.*;

import java.util.Random;

import javax.swing.*;
import javax.swing.border.*;

public class RadioSelection extends JFrame implements ActionListener
{
    private ActionListener action;  
    private JButton[][] button;

    private JPanel bottomPanel;
    private LineBorder lineBorder;

    private int randomRowLimit;
    private int randomColumnLimit;
    private Random random;
    private int size;

    public RadioSelection()
    {
        randomRowLimit = 0;
        randomColumnLimit = 0;
        random = new Random();
        size = 0;

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationByPlatform(true);

        lineBorder = new LineBorder(Color.BLUE.darker());

        JPanel topPanel = new JPanel();
        topPanel.setLayout(new FlowLayout(FlowLayout.LEFT));

        bottomPanel = new JPanel();

        final JRadioButton threeSquareButton = new JRadioButton("3 X 3", false);
        final JRadioButton fourSquareButton = new JRadioButton("4 X 4", false);
        final JRadioButton fiveSquareButton = new JRadioButton("5 X 5", false);

        threeSquareButton.setBorder(lineBorder);
        fourSquareButton.setBorder(lineBorder);
        fiveSquareButton.setBorder(lineBorder);

        action = new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                if (ae.getSource() == threeSquareButton)
                {
                    remove(bottomPanel);
                    bottomPanel = getCenterPanel(3);
                    add(bottomPanel, BorderLayout.CENTER);
                }
                else if (ae.getSource() == fourSquareButton)
                {
                    remove(bottomPanel);
                    bottomPanel = getCenterPanel(4);
                    add(bottomPanel, BorderLayout.CENTER);
                }
                else if (ae.getSource() == fiveSquareButton)
                {
                    remove(bottomPanel);
                    bottomPanel = getCenterPanel(5);
                    add(bottomPanel, BorderLayout.CENTER);
                }
                revalidate(); // If you are using JDK 1.7 +
                // getContentPane().revalidate(); // if you using JDK 1.6 or lower
                repaint();
            }
        };

        threeSquareButton.addActionListener(action);
        fourSquareButton.addActionListener(action);
        fiveSquareButton.addActionListener(action);

        ButtonGroup bg = new ButtonGroup();
        bg.add(threeSquareButton);
        bg.add(fourSquareButton);
        bg.add(fiveSquareButton);

        topPanel.add(threeSquareButton);
        topPanel.add(fourSquareButton);
        topPanel.add(fiveSquareButton);

        add(topPanel, BorderLayout.PAGE_START);
        add(bottomPanel, BorderLayout.CENTER);

        setSize(300, 300);
        //pack();
        setVisible(true);
    }

    private JPanel getCenterPanel(int size)
    {
        JPanel bottomPanel = new JPanel(new GridLayout(size, size));
        button = new JButton[size][size];
        this.size = size;

        for (int row = 0; row < size; row++)
        {
            for (int column = 0; column < size; column++)
            {
                button[row][column] = new JButton();
                button[row][column].setBorder(lineBorder);
                button[row][column].setMargin(new Insets(2, 2, 2, 2));
                button[row][column].addActionListener(this);
                bottomPanel.add(button[row][column]);
            }
        }

        randomRowLimit = random.nextInt(size);
        randomColumnLimit = random.nextInt(size);
        button[randomRowLimit][randomColumnLimit].setText("X");

        return bottomPanel;
    }

    public void actionPerformed(ActionEvent ae)
    {
        JButton button = (JButton) ae.getSource();

        if ((button.getText()).equals("X"))
        {
            randomRowLimit = random.nextInt(size);
            randomColumnLimit = random.nextInt(size);
            System.out.println("Row : " + randomRowLimit);
            System.out.println("Column : " + randomColumnLimit);
            button.setText("");
            this.button[randomRowLimit][randomColumnLimit].setText("X");            
        }
        else
        {
            JOptionPane.showMessageDialog(this, "Please Click on X Mark to follow it.", "Small Game : ", JOptionPane.ERROR_MESSAGE);
        }
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new RadioSelection();
            }
        });
    }
}
nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
  • ,thank you really for this.this is the one i really need. How can i thank you for this.?thank you!! – shielomarie Mar 07 '12 at 12:19
  • @shielomarie : Your Welcome and Keep Smiling :-) – nIcE cOw Mar 07 '12 at 12:23
  • ,for this code,im going to add another radiobutton for 5x5 buttons.really thank you! Heres my smile ^-^ – shielomarie Mar 07 '12 at 12:27
  • hehe :-), that is really nice. – nIcE cOw Mar 07 '12 at 12:32
  • For additional question regarding the codes. For scenario, a radio button is clicked,which is 3x3 then 9 buttons will be shown in the output.if you click a button out of 9 buttons,a message box will appear'catch the mouse!' if you click on the button with x on it, the x will transfer to the other button randomly. Do i make sense here?just want to know if this is possible? Thank you in advance!! – shielomarie Mar 07 '12 at 13:34
  • @shielomarie : Please do see this code now is this what you want ? – nIcE cOw Mar 07 '12 at 13:58
  • You are a genius!this is the one ive been looking for.i dont know how to thank you! Thank u so much!!very much appreciated your effort here coding.thanks!! – shielomarie Mar 07 '12 at 14:50
  • That is really nice of you. Thinking mind is a genius's mind. So even you are a genius too :-) Who knows, what might you will create in future :-) – nIcE cOw Mar 07 '12 at 15:02
  • sir i cant find how to run this one? revalidate has an underline. :( – shielomarie Mar 07 '12 at 23:12
  • wow.its working.i change the revalidate(); to invalidate();but when i click radiobutton,the buttons wont come out.maybe because the size of the frame is not declared..to be able to see the grid buttons,the user should adjust the side of the frame. isn't it automatic? it shouldbe when a radiobutton is click,(e.g 3x3) 9 buttons will appear even without adjusting the frame. .thanks anyway! – shielomarie Mar 07 '12 at 23:53
  • @shielomarie : Please do read the comments in my attached program in my answer, if your `JDK is 1.6 or lower`, then `revalidate()` won't work, you have to use `frameObject.getContentPane().revalidate();`.`revalidate();` works for JDK 1.7 or higher. How was it working before ? Do try to copy this code again and paste it at your end. It's working fine at my end. – nIcE cOw Mar 08 '12 at 02:54
  • @shielomarie : Never use `invalidate()`, it is completely opposite in meaning to `revalidate()`, we use `revalidate()` to undo `invalidate()`. LOL and you are deliberately using `invalidate()`. If nothing works, edit your question with the code you are trying :-) – nIcE cOw Mar 08 '12 at 03:03
  • @Gagandeep >>invalidate(); here is working sir.. the code you give to me is working,though when a radiobutton is click,the buttons appear if the frame is adjusted. – shielomarie Mar 08 '12 at 04:24
2

I'm not sure I understand the question or the code, but I'd expect to see an ActionListener on the 3x3 button that would create an array of JRadioButton instances using a method that's something like this:

private JRadioButton [][] createRadioButtonArray(int squareSize) {
    JRadioButton [][] arrayOfButtons = new JRadioButton[squareSize][squareSize];
    for (int i = 0; i < squareSize; ++i) {
        for (int j = 0; j < squareSize; ++j) {
            arrayOfButtons[i][j] = new JRadioButton("button" + i + "," + j,false);
        }
    }
    return arrayOfButtons;
}
duffymo
  • 305,152
  • 44
  • 369
  • 561
  • Yes that's what im looking for. thank you for your help. Sorry if you cant understand my question im not that really good in english. Uhm whats wih the int square? – shielomarie Mar 07 '12 at 10:33
  • Do you mean "squareSize"? That's the size of the array: 3 for your 3x3 case; 4 for your 4x4 case; etc. I'd prefer that you accept the answer to your thanks. – duffymo Mar 07 '12 at 10:35
  • What are the imports?need for this application?thank you very much! – shielomarie Mar 07 '12 at 10:35
  • 1
    Sorry, I'm not writing your app for you. You're already importing what you need for this method. – duffymo Mar 07 '12 at 10:35
  • I know.i mean imports do i have to put import javax.awt.*; – shielomarie Mar 07 '12 at 10:38
  • I know what you mean. No, you should not use * in import. You should just import JRadioButton for the method I wrote. Make sure you understand what import is doing. It just saves keystrokes, nothing more. – duffymo Mar 07 '12 at 10:41
  • I understand what imports do. Im just having hard time thinking about how to show array of buttons when a radio button is clicked. – shielomarie Mar 07 '12 at 10:45
  • Good luck - accept this answer if it helps. I'm finished with your question, hard time or not. – duffymo Mar 07 '12 at 10:48
  • @duffymo +1 maybe reason for implementing ButtonGroup – mKorbel Mar 07 '12 at 11:11
  • How can i close this question? – shielomarie Mar 07 '12 at 11:32
  • For additional question regarding the codes. For scenario, a radio button is clicked,which is 3x3 then 9 buttons will be shown in the output.if you click a button out of 9 buttons,a message box will appear'catch the mouse!' if you click on the button with x on it, the x will transfer to the other button randomly. Do i make sense here?just want to know if this is possible? Thank you in advance!! – shielomarie Mar 07 '12 at 13:33
  • Put it in a class of your choosing and run it. – duffymo Mar 08 '12 at 00:15
  • when i put this one,fatal error comes out and it doesn't even run. – shielomarie Mar 08 '12 at 02:47