0

I have a problem. I have a cell panel in which every cell is a jbutton. It's for drawing. The problem is that I can't draw on this panel without releasing the mouse. It's supposed that I press the left mouse button and just drag the mouse and I release it only when I have finished drawing. But when I press a particular jbutton the focus is caprtured by it and I can't do any actions until I release the mouse. I would be very grateful if someone helped me to solve this problem.

public class Canvas extends JPanel {

  private static final long serialVersionUID = 1L;

  private JButton [][] buttons;
  private boolean [][] bValues;


  public Canvas(int m, int n){

    buttons = new JButton[m][n];
    bValues = new boolean[m][n];


    GridLayout gridLayout = new GridLayout(m,n,0,0);
    setLayout(gridLayout);


    for(int i = 0; i < m; i++){
        for(int j = 0; j < n; j++){

            JButton btn = new JButton();
            btn.setBackground(Color.WHITE);
            btn.setName(i + ":" + j);
            btn.setEnabled(false);

            btn.addMouseListener(new MouseAdapter() {


                @Override
                public void mousePressed(MouseEvent e) {

                    JButton srcButton = (JButton)e.getSource();
                    srcButton.setSelected(false);
                    StringTokenizer strTok = new StringTokenizer(srcButton.getName(), ":");
                    int row = Integer.parseInt(strTok.nextToken());
                    int col = Integer.parseInt(strTok.nextToken());


                    if(e.getButton() == MouseEvent.BUTTON1){
                        srcButton.setBackground(Color.BLACK);
                        bValues[row][col] = true;
                        System.out.println(row + ":" + col);
                    }
                    else if(e.getButton() == MouseEvent.BUTTON3){
                        srcButton.setBackground(Color.WHITE);
                        bValues[row][col] = false;
                        System.out.println(row + ":" + col);
                    }

                }

            });


            add(btn);

            bValues[i][j] = false;
        }
    }


}

}

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Max
  • 75
  • 1
  • 10
  • Are you trying to draw over, under or just select the buttons? – trashgod May 02 '15 at 22:52
  • Just select. But I need drawing effect. It's very annoying to select each cell seperately. – Max May 02 '15 at 22:57
  • `JList` allows multiple selections, for [example](http://stackoverflow.com/a/7620726/230513). – trashgod May 02 '15 at 23:17
  • No, you got me absolutely wrong! In my case, buttons simulate a 32x32 pixel canvas. – Max May 02 '15 at 23:45
  • In my case, buttons simulate a 32x32 pixel canvas. Each pixel is represented by a boolean value. I wanted to use buttons here for simplification, because I don't need to work with coordiates in this case. A similar concept is used in the chess gam – Max May 03 '15 at 00:13
  • in this [example](http://stackoverflow.com/a/2901472/230513), moving over a pixel displays its location and RGB value. – trashgod May 03 '15 at 01:48
  • Ok. Maybe I just overengineered. I don't know. – Max May 03 '15 at 11:08

0 Answers0