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;
}
}
}
}