This is my complete code and also I have added logic from Simons answer here selected rows should be 0,1,2,4,5,7,8 but with logic provided by Simon its not working. please suggest over logic in catch
block
public static void main(String[] args)
{
JFrame frame = new JFrame();
int[] arr = { 0, 1, 2, 4, 5, 7, 8 };
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTable t = new JTable(10, 1);
frame.add(new JScrollPane(t));
t.getSelectionModel().clearSelection();
frame.pack();
frame.setVisible(true);
try
{
throw new Exception();
}
catch (Exception e)
{
int start = -1;
int end = -1;
for(int j = 0; j < arr.length - 1; j++)
{
if(arr[j] + 1 == arr[j + 1])
{
if(start == -1)
start = j;
end = j + 1;
}
else
{
t.getSelectionModel().addSelectionInterval(arr[start != -1 ? start : j], arr[end]);
}
}
}
}
selectedRows
is array of int. – eatSleepCode Feb 3 at 12:01j == SelecterdRows.length - 1
because it will produce ArrayIndexOutOfBoundsException. – rolfl♦ Feb 3 at 12:14