0

this is my first post here, hope i'll get it right!!

what i did i created a NxN board with JButtons indicating the coordinates from [0, 0] to [9, 9]. each time a button is clicked the console shows the coordinates and what i tried to do is to save those coordinates in an ArrayList that will be displayed by pressing a second button in another window...nothing fancy, sorry, just wrapping my head around basic concepts...

the problem is that i can't get the values to be saved into the ArrayList and i can't then recall it once i press the second button... attached the codes for my classes...each one is in a different file.

Board.java

public class Board{

public Board(){

    JFrame win = new JFrame ();
    GridLayout layout = new GridLayout(10, 10);
    win.setLayout(layout);
    for (int row1 = 0; row1 < 10 ; row1 = row1+1){
        for (int col1 = 0; col1 < 10; col1 = col1+1){
            JPanel jp = new JPanel();
            JButton jb = new JButton("[" + row1 + "," + col1 + "]");
            jb.addActionListener(new ButtonEventHandler(row1, col1));
            jp.add(jb);
            win.add(jp);
        }
        win.setVisible(true);
        win.pack();
}

    JFrame win2 = new JFrame("Stored values");
    win2.setVisible(true);
    JPanel jp2 = new JPanel();
    win2.add(jp2);
    JButton jb2 = new JButton("Check Log");
    jb2.addActionListener(new Position(win2, jb2));
    jp2.add(jb2);
    win2.pack();
}}

ButtonEventHandler.java

public class ButtonEventHandler implements ActionListener {


private int _row1;
private int _col1;
private ArrayList<Number> _list;


public ButtonEventHandler(int row1, int col1){

    _row1 = row1;
    _col1 = col1;
    _list = new ArrayList<Number>();
}

@Override
public void actionPerformed(ActionEvent e) {
    System.out.println("Position: " + _row1 + ", " + _col1);
    _list.add(_row1);
    _list.add(_col1);
}

public ArrayList<Number> getList(){

    return _list;
}}

Position.java

public class Position implements ActionListener  {

private JFrame _win2;

private JButton _jb2;
private int _row1;
private int _col1;
private ArrayList<Number> _list;

private ButtonEventHandler beh = new ButtonEventHandler(_row1, _col1);


public Position(JFrame win2, JButton jb2){
    _win2 = win2;
    _jb2 = jb2;     
}

@Override
public void actionPerformed(ActionEvent e) {

    System.out.println(beh.getList());      
}
}

thanks so much for the help!!

Seb

1
  • tnx for formatting the post properly! Commented May 4, 2012 at 3:25

2 Answers 2

1

The problem in your code is that you don't have one array list: you have many array lists. Each button handler has its own!

You should make one array list, and share it among all your handlers by passing it to their constructors.

public Position(JFrame win2, JButton jb2, AttayList<Number> list){
    _win2 = win2;
    _jb2 = jb2;
    _list = list;
}

public ButtonEventHandler(int row1, int col1, AttayList<Number> list) {
    _row1 = row1;
    _col1 = col1;
    _list = list;
}

public Board(){
    JFrame win = new JFrame ();
    GridLayout layout = new GridLayout(10, 10);
    win.setLayout(layout);
    ArrayList myList = new ArrayList<Number>();
    // In the code below, use myList as the last parameter to the constructors of ButtonEventHandler and Position
    ...

}

Sign up to request clarification or add additional context in comments.

4 Comments

tnx for the reply...i see what u mean...but if i put the code like that when i click the button to check print the ArrayList out i get only "null" in the console....hmmmm....do i need to create i.e. a setList where i set the value of the list before the accessor method?
@Seb No, you already created an instance of the list in your Board constructor. You need to make sure that all 100 buttons get this list as the third parameter of their constructors, and that the Position gets the same instance as its last constructor parameter too.
both ButtonEventHandlerand Position in the constructor, and in their instantiation in the Board class, have as third parameter list...in Position i have it like this private ArrayList<Number> _list; private ButtonEventHandler beh = new ButtonEventHandler(_row1, _col1, _list);...would that be ok?
@Seb Could you update the question? The comments are not as readable as the question because of the formatting issues.
0

ok, i had it to work :)

i changed the constructors as suggested, in the Position class i got the rid of

private ButtonEventHandler beh = new ButtonEventHandler(_row1, _col1, _list);

and changed the ActionPerformed to:

@Override
public void actionPerformed(ActionEvent e) {
    System.out.println(beh.getList()); //previous code, changed with the one below
    System.out.println(_list);
}

in this way i didn't need anymore the getList method in the Board class!

thanks for the help!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.