pablo pablo - 9 months ago 59
Java Question

Designing a Poker UI with Matrices and JButtons in Java

I have to design a user interface in Java where all the possible combinations of hands for texas hold'em are displayed on the screen, so:
Something Like this

I am a beginner at Java so bare with me... I understand I basically need 169 squares (Jbuttons) or a 13x13 "table". So I have encountered two problems, I can design an interface where I have these 169 individual buttons but how do I assign them? I know i need two for loops (one inside the other) to generate the list but i Cannot grasp how.
A friend of mine suggested I use a matrix instead of creating the buttons using a for loop....

This is my basic idea but i would like to change the colour of the buttons when I press them AND send whatever is written in them to a JText...

Any ideas are appreciated and thanks for taking time to read this

package modelo;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Insets;
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.JPanel;

public class RangoImpreso implements ActionListener{

JButton []ArrayButton = new JButton[169];//Buttons
JPanel jp1;
JLabel jl1, jl2;

private RangoImpreso ()

JFrame frMain = new JFrame("Rango Pre-Flop");
frMain.setLayout(new BorderLayout(10, 20));

jl1 = new JLabel(); //jlabel that outputs number pressed
jl1.setText("AquĆ­ ira el numero que se pulse");


frMain.add(jl1, BorderLayout.NORTH);
frMain.add(jp1, BorderLayout.SOUTH);

frMain.setSize(600, 600);
frMain.setLocation(700, 300);

public void mostrarBot() //method where the Jpanel is
jp1 = new JPanel(new GridLayout(13, 13, 0, 0));

for(int i=ArrayButton.length-1; i>=0; i--) //create buttons, add properties
ArrayButton[i] = new JButton(""+(i+1));
ArrayButton[i].setMargin(new Insets(0, 0, 0, 0));

public static void main(String[] args)
RangoImpreso trin = new RangoImpreso();

public void actionPerformed(ActionEvent e) //Use jlabel to show button pressed


Answer Source

These are the two for loops you need to create the buttons

  • o = index of outer loop
  • i = index of inner loop

both are counting from 0 to 12 so it will create 13*13 buttons

for(int o = 0; o < 13; o++) {
    for(int i = 0; i < 13; i++) {
        //Create you buttons
        JButton btn = new JButton();
        //Add Handlers to change color and send text to textfield here...