Java – get input Text from JTextField (string to char) to show up in JButton

actionlistenerbuttonjavaswing

(new rewritten code).. I just know that "b" will always the last box that i created.So i can't use b as the equals in actionPerformed. How to include all the buttons? Anyone can help me with this?
import java.awt.;
import javax.swing.
;
import java.awt.event.*;

public class Lat1 extends JFrame implements ActionListener
{
    final int ROWS = 12;

    final int COLS = 12;

    final static int topLeftNum[][]= {
        {-1, 1, 0, 2, 0, 0, 3, -1, 4, 0, 5, 0},
        {6, 0, 0, 0, -1, -1, 0, -1, -1, -1, 0, -1},
        {-1, 0, -1, 0, -1, 7, 0, 0, 8, -1, 0, -1},
        {9, 0, 0, 0, 10, -1, -1, -1, 11, 0, 0, -1},
        {0, -1, -1, 12, 0, 0, 13, -1, 0, -1, -1, -1},
        {0, -1, 14, -1, 0, -1, 0, -1, 15, 0, 0, 16},
        {17, 0, 0, 18, 0, -1, 19, 20, 0, -1, -1, 0},
        {0, -1, 0, 0, -1, 21, 0, 0, 0, -1, -1, 0},
        {22, 23, 0, 0, -1, 0, -1, 0, -1,24, 0, 0},
        {-1, 0, -1, 25, 0, 0, -1, 0, -1, 0, -1, -1},
        {26, 0, 0, -1, -1, 0, -1, 27, 0, 0, 0, -1},
        {-1, -1, -1, -1, -1, 0, -1, 0, -1, 0, -1, -1}
        };

     Box b;

     JTextField t;

     char answer;

     public static void main(String[] args)
    {

        SwingUtilities.invokeLater(new Runnable() 
        {

            public void run(){  new Lat1(); }
        });
    }

    /*--------------------------------------------------------*/
    public Lat1() 
    {   

    this.setSize(1000,1000);
    this.setVisible(true);
        JPanel p1 = new JPanel();   
        p1.setLayout(new GridLayout(ROWS,COLS));
        for (int j=0; j<ROWS; j++) {
            for (int i=0; i<COLS; i++)  {
                b = new Box(i, //the boxes index
                (topLeftNum[j][i] < 0) ? Color.BLACK : Color.WHITE, //pick the color
                topLeftNum[j][i], //the topleft number
                answer, //the char inside
                this); //the action listener for the button
                p1.add(b);
            }
        }
        p1.setVisible(true);
        this.getContentPane().add(p1,BorderLayout.CENTER);
        this.pack();

        JPanel p2 = new JPanel();
        t = new JTextField(10);
        p2.add(t);
        p2.setVisible(true);
        this.getContentPane().add(p2,BorderLayout.SOUTH);

    }
/*-----------------------------------------------------------*/
public void actionPerformed(ActionEvent e) 
{
    for (int j=0; j<ROWS; j++) {
        for (int i=0; i<COLS; i++)  {
    String numstr = String.valueOf(topLeftNum[j][i]);       
    if(e.getSource() == numstr && (t.getText()).length() == 1)
    answer = t.getText().charAt(0);
    b.setText(""+ answer);

    //b.setText(String.valueOf(t.getText().charAt(0));
        }
    }
}   
}

/*------------------------------------------------------------*/

class Box extends JButton

{

    public int index;

    private String topLeftNum;

    public Box(int index, Color color, int topLeftNum, char c, ActionListener al) 

    {


    this.setBackground(color);

        if (color != Color.BLACK) {

            this.setFont(new Font("SansSerif", Font.ITALIC, 20));

            this.setText(""+ c);

            this.addActionListener(al);

            this.index = index;

            if (topLeftNum != 0)

            this.topLeftNum = topLeftNum+"";    }

        else {

            this.setText("");

            this.setEnabled(false);

            return; }

    }

    public void paintComponent(Graphics g) 

    {

    super.paintComponent(g); // paints background

    g.setFont(new Font("SansSerif", Font.PLAIN, 8));

        if (topLeftNum != null) g.drawString(topLeftNum, 5, 10);

    }

}
/*----------------------------------------------------------*/

Best Answer

First of all, You have NullPointerException thrown at line 59 from constructor of main class. You attempt to getText from field which is not initialized.

Secondly You can call repaint method, so label changing code might look as follows:

public void keyReleased(KeyEvent e) {
  if(e.getKeyCode() == KeyEvent.VK_ENTER) {
    Field f = (Field)e.getSource();
    Box b = f.box;
    b.setText(f.getText());
    b.repaint();
    this.remove(f);
    this.add(f.box, b.index);
  }
}

This should probably force JButton to change its look.

(Advice for Your next posts: add indentation to Your code, please)

Related Topic