Abdelaziz Dabebi
Abdelaziz Dabebi

Reputation: 1638

how to declare JFrame buttons with a table

I created a Class that extends from JFrame which has a table of buttons. In the class constructor I added the buttons to the panel but when I run the main nothing happens and I see only an empty frame. So can you hep me to find the problem? This is the code:

public class Tita extends JFrame {
    JButton ff[][] = new JButton[3][3];
    int i = 0, j = 0;

    public static void main(String[] args) { 
        Tita oo = new Tita();
    }

    public Tita() {
        super("Newframe"); 
        setVisible(true);
        for(i = 0; i < 3; i++) {
            for(j = 0; j < 3; j++) {
                ff[i][j].setText("sss");
                this.getContentPane().add(ff[i][i]);
            }
        }
    }

Upvotes: 0

Views: 225

Answers (1)

Jonathan Solorzano
Jonathan Solorzano

Reputation: 7022

What is happening is that you haven't initialized any JButton, also, when you add the button you have getContentPane().add(ff[i][i]);, when it should be getContentPane().add(ff[i][j]);

import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;



public class Tita extends JFrame {
    JButton ff[][] = new JButton[3][3];
    int i = 0, j = 0;

    public static void main(String[] args) { 
        Tita oo = new Tita();
    }

    public Tita() {
        super("Newframe"); 
        setVisible(true);
        setLocationRelativeTo(null);
        setSize(new Dimension(300, 400));
        setLayout(new GridLayout(3, 0));
        for(i = 0; i < 3; i++) {
            for(j = 0; j < 3; j++) {
                ff[i][j] = new JButton("SSS");
                ff[i][j].setSize(30, 10);
                getContentPane().add(ff[i][j],i);
            }
        }
    }
}

Upvotes: 1

Related Questions