So I'm new to Java, and I chose to make a small program to calculate coordinates in a new basis as my first project. However, when i add events to basisButtons (an array of buttons), i keep getting an error of ArrayIndexOutOfBoundsException from indexButtons[], and i just dont get it why. Any help would be appreciated. The code is still far from ready, only looking for solutions for this problem.
package mathsswing;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class MainGUI extends JFrame {
int i,j;
int rownr;
int colnr;
JTextField matrixCells[][];
private javax.swing.JMenuBar menuBar;
private javax.swing.JMenuItem mexitItem;
private javax.swing.JMenuItem mbaseItem;
private javax.swing.JPanel mainPanel;
private javax.swing.JMenu fileMenu;
private javax.swing.JPanel myPanel[];
private javax.swing.JButton basisButtons[][];
JButton indexButtons[];
String colindex[];
String rowindex[];
private void initComponents() {
mainPanel = new javax.swing.JPanel();
menuBar = new javax.swing.JMenuBar();
mbaseItem = new javax.swing.JMenuItem("Basis transformation");
mexitItem = new javax.swing.JMenuItem("Exit");
fileMenu = new javax.swing.JMenu("File");
myPanel = new JPanel[10];
for(i=0;i<10;i++) {
myPanel[i]= new JPanel();
myPanel[i].setVisible(false);
mainPanel.add(myPanel[i]);
}
fileMenu.add(mbaseItem);
fileMenu.add(mexitItem);
menuBar.add(fileMenu);
mainPanel.setBounds(15,30,600,400);
mexitItem.addActionListener(new ExitButtonListener());
mbaseItem.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent e) {
myPanel[1].setVisible(true);
}});
setJMenuBar(menuBar);
setContentPane(mainPanel);
mainPanel.setVisible(true);
int j;
String numbers[] = new String[10];
for(j=1; j<11; j++) { numbers[j-1] = Integer.toString(j); };
final JComboBox rowsCBox = new JComboBox(numbers);
final JComboBox columnsCBox = new JComboBox(numbers);
JButton rowscolsOK = new JButton("OK");
rowscolsOK.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
rownr = Integer.parseInt((String) rowsCBox.getSelectedItem());
colnr = Integer.parseInt((String) columnsCBox.getSelectedItem());
myPanel[1].setVisible(false);
matrixCells = new JTextField[rownr][colnr];
myPanel[2].setLayout(new GridLayout(rownr+1,colnr));
myPanel[2].add(new JLabel("Set values of the matrix"));
JButton addData = new JButton("OK");
addData.addActionListener(new CreateTableListener());
for(i=2;i<colnr;i++) {myPanel[2].add(new JLabel(" "));}
myPanel[2].add(addData);
int k,l;
for(k=0;k<rownr;k++) {
for(l=0;l<colnr;l++) {
matrixCells[k][l]= new JTextField();
myPanel[2].add(matrixCells[k][l]);
}
}
myPanel[2].setVisible(true);
}
});
myPanel[1].setLayout(new GridLayout(3,2));
myPanel[1].add(new JLabel("Number of Rows"));
myPanel[1].add(rowsCBox);
myPanel[1].add(new JLabel("Number of Columns"));
myPanel[1].add(columnsCBox);
myPanel[1].add(rowscolsOK);
}
public MainGUI() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Quantitative Economics");
setSize(800,600);
setResizable(true);
initComponents();
}
class ExitButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
class CreateTableListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
myPanel[2].setVisible(false);
int matrixData[][] = new int[rownr][colnr];
String matrixString[][] = new String[rownr][colnr];
colindex= new String[colnr];
rowindex= new String[rownr];
for(i=0;i<rownr;i++){for(j=0;j<colnr;j++) { matrixData[i][j]=Integer.parseInt(matrixCells[i][j].getText()); matrixString[i][j]=Integer.toString(matrixData[i][j]);}}
myPanel[3].setLayout(new GridLayout(rownr+1,colnr+1));
myPanel[3].add(new JButton(" "));
indexButtons = new JButton[colnr];
for(i=0;i<colnr;i++){ colindex[i] = new String("x" + (i+1)); indexButtons[i]= new JButton(colindex[i]); myPanel[3].add(indexButtons[i]); }
myPanel[3].setVisible(true);
basisButtons = new JButton[rownr][colnr];
for(i=0;i<rownr;i++) {
rowindex[i] = new String("e" + (i+1));
myPanel[3].add(new JButton(rowindex[i]));
for(j=0;j<colnr;j++) {
basisButtons[i][j] = new JButton(matrixString[i][j]);
myPanel[3].add(basisButtons[i][j]);
}
}
for(i=0;i<colnr;i++) {
for(j=0;j<rownr;j++) {
basisButtons[j][i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
indexButtons[i].setText("FOR THIS IM GETTING THE ERROR");
}});
}
}
}
}
public static void main(String[] args) {
MainGUI mainwindow = new MainGUI();
mainwindow.setVisible(true);
}
}
I get "Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException", line 133.
Thanks in advance and sorry for my poor English, I'm not native.
David

New Topic/Question
Reply



MultiQuote



|