views:

842

answers:

4

(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);

    }

}
/*----------------------------------------------------------*/
A: 

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)

Dejw
Thanks for the advice. I'll be sure to remember that. I have initialized the field by putting "Field f;" or am i wrong?
Initialized means creating an instance of a Field class and assigning it to f, e.g. Field f = new Field(new Box(...), this);
Dejw
So, i should have put " Field f = new Field(); "? But then, i got an error again. "Cannot resolve symbol constructor Field() loaction class field"
where do i have to put the initialization Field on? more detail if you wouldn't mind.
Field constructor is public Field(Box box, KeyListener kl), so You cannot write Field f = new Field(); You have to give arguments, in this case: Box instance, and KeyListener.Initializations can be placed where the variable declaration is or in contructor of Lat1 class.
Dejw
in the Box instance(argument) there is int,color,int,char,actionlistner. What should i replace the char with? (If i want to getText from Field f and make it show up on JTable)?
* not JTable but JButton.
Char may be replaced with anything. The real problem is a design of Your classes. I suggest You to rewrite whole program.
Dejw
Ok.. i think i'll rewrite again.
A: 

repainting will work, but i recommend calling

SwingUtilities.updateComponentTreeUI(b);

that will refresh the entire component plus any children.

N

Nico
Thanks. where do i have to put the code on?
-1 updateComponentTreeUI(...) is for LAF changes and should not be used just because you don't know what the underlying problem is.
camickr
Because i am not familiar with it, so i do not use it.
my bad. while this will work, it's not the correct way to do it.
Nico
A: 

You should be using an ActionListener and not a KeyListener. Although it is not the cuase of your problem it is the better design when using a JTextField.

camickr
and what do i use for the if-else? e.g. String s = e.getActionCommand(); if(s.equalas("???")) What should i replace the "???" with? The JButton doesn't even have name on it (e.g. main = new Button("main").
A: 

You don't actually add the TextField to anything as far as I can tell either - and your processing inside actionPerformed fails to compile due to a mismatch between Object (the class returned by "e.getSource()" and int (your "b.index" value).

Also, "b" is always the last "box" you create. You're a long way from a solution here.

M1EK
Is there a way to add the TextField without messing the gridLayout?? Because i already set the GridLayout to COLS and ROWS.
make another panel with a different Layout (for now, try something simple like BorderLayout); add your existing panel to it as the CENTER component; add the textfield as SOUTH, stick this new panel in the jframe instead of your existing one, then try it out and see what happens.
M1EK
works great...^V^
now my only problem is the buttons(Box). How can i make it so only the white buttons works? If i can't use b, then what should i use?