I have a JTable of which one column is pre-filled with 30min time slots (6.30-24.00).
Now I have another table which has a list of movie titles which contains a column with the duration of the movie (in minutes – e.g. 140 minutes).
Now I have a button that does this. I made a piece of code, which funnily enough, sometimes works and sometimes doesn’t (after I add 3-4 titles). It adds to the time slots according to the math equation.It gives me :
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "DRAMA"
This is the code:
btnAddProg.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
int dur = Integer.parseInt(progTableModel.getValueAt(listTable.getSelectedRow(), listTable.getSelectedColumn()+1).toString()) / 30;
int durT = Integer.parseInt(progTableModel.getValueAt(listTable.getSelectedRow(), listTable.getSelectedColumn()+1).toString());
if(durT % 30 != 0)
{
dur += 1;
}
for(int i = 0; i < dur; i++)
{
String value = progTableModel.getValueAt(listTable.getSelectedRow(), listTable.getSelectedColumn()).toString();
String value2 = progTableModel.getValueAt(listTable.getSelectedRow(), listTable.getSelectedColumn()+2).toString();
channel1DataTitle.set(chOneTable.getSelectedRow()+i, value);
channel1DataGenre.set(chOneTable.getSelectedRow()+i, value2);
}
chOneTable.repaint();
} catch (IndexOutOfBoundsException f) {
JOptionPane.showMessageDialog(frame,
"Please select a row in the Channel table!",
"Channel row not selected",
JOptionPane.PLAIN_MESSAGE);
}
}
});
Can anyone tell me what’s wrong?
It works when you click on the proper column and fails when you click on another, doesn’t it? You have fixed logic (parsing the duration number) applied to a variable column (depending on the exact column the user clicked). Access the column with a fixed number, don’t check for the selected column index.