Results 1 to 11 of 11
- 10-16-2010, 10:45 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 6
- Rep Power
- 0
ArrayIndexOutOfBounds, JTable.getValueAt
Not sure if this should be in New to Java or AWT/Swing, but here goes.
I've got a JTable. The JTable has a ListSelectionListener.
Whenever I run the program, the first two clicks on the JTable throw double (mousePressed, mouseReleased) ArrayIndexOutOfBoundsExceptions: -1. They point to the last line above. After that, everything works as planned.Java Code:public void valueChanged(ListSelectionEvent l) { int row = table1.getSelectedRow(); int column = table1.getSelectedColumn(); boolean isAdjusting = l.getValueIsAdjusting(); String value = (String) table1.getValueAt(row, 0); [...]
Any help appreciated.
Thanks,
~Zack
-
I'm not sure how we can guess the cause of the error based on a small amount of non-running code. I think that your best bet will be to create an SSCCE and post it here.
Best of luck.
- 10-17-2010, 12:28 AM #3
Member
- Join Date
- Oct 2010
- Posts
- 6
- Rep Power
- 0
Perfect example of the problem. Code should print out "valid" and the contents of the first column, same row cell if the selected cell is in the second column, and "invalid" and the contents of the first column, same row cell if the selected cell is in the first. Instead, the first two clicks throw errors.
The code is a bit messy, but works.
Thanks,Java Code:import javax.swing.*; import javax.swing.event.*; import java.awt.*; public class test { JTable table; public static void main(String[] args) { new test(); } public test() { String[] columns = {"One", "Two"}; String[][] data = {{"bla", "bla"}, {"more", "and more"}}; JFrame frame = new JFrame("Title"); JPanel panel = new JPanel(new BorderLayout()); table = new JTable(data, columns); JScrollPane pane = new JScrollPane(table); panel.add(pane); frame.add(panel); table.setRowSelectionAllowed(true); table.setColumnSelectionAllowed(true); table.setCellSelectionEnabled(true); table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); thing listener = new thing(); ListSelectionModel listSelectionModel = table.getSelectionModel(); listSelectionModel.addListSelectionListener(listener); table.setSelectionModel(listSelectionModel); table.getColumnModel().getSelectionModel().addListSelectionListener(listener); frame.setVisible(true); frame.setSize(400, 400); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } class thing implements ListSelectionListener { public void valueChanged(ListSelectionEvent l) { int row = table.getSelectedRow(); int column = table.getSelectedColumn(); boolean isAdjusting = l.getValueIsAdjusting(); String value = (String) table.getValueAt(row, 0); if (!isAdjusting) { if (column != 0) { System.out.println("valid: " + value); } if (column == 0) { System.out.println("invalid: " + value); } } } } }
~ZackLast edited by ZackO; 10-17-2010 at 12:37 AM. Reason: for default close operation
- 10-17-2010, 01:17 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
So what is the value of row at the time you get the AIOOBE? It may well be that you have a value that causes JTable to choke when you call getValueAt().
- 10-17-2010, 01:40 AM #5
Member
- Join Date
- Oct 2010
- Posts
- 6
- Rep Power
- 0
According to a
System.out.println(row);
before the erroneous line, the value of row is -1 regardless of where I click. However, that's only true for the first two clicks. I'm pretty confused.
~Zack
- 10-17-2010, 02:02 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Are you sure you are getting this error for the first two clicks and not getting this error twice for the first click. This is what I'm seeing... if I click on two cells in the same column. If I click on two cells in the same row I can generate lots of errors.
Since you only want value if the list selection is not changing put the "value=" line inside the if block.
- 10-17-2010, 02:29 AM #7
Member
- Join Date
- Oct 2010
- Posts
- 6
- Rep Power
- 0
This was my original code (inside the valueChanged method):
and this is the code now:Java Code:int row = table1.getSelectedRow(); int column = table1.getSelectedColumn(); boolean isAdjusting = l.getValueIsAdjusting(); String value = (String) table1.getValueAt(row, 0); if (!isAdjusting) { if (column != 0) { setInfoPanelEnabled(true); thetime1.setText("Time: " + value); } if (column == 0) { setInfoPanelEnabled(false); thetime1.setText("Time: N/A"); } }
It works, but I'm still not sure why. Thanks for the help anyway.Java Code:int row = table1.getSelectedRow(); int column = table1.getSelectedColumn(); boolean isAdjusting = l.getValueIsAdjusting(); if (!isAdjusting) { if (column != 0) { String value = (String) table1.getValueAt(row, 0); setInfoPanelEnabled(true) thetime1.setText("Time: " + value); } if (column == 0) { setInfoPanelEnabled(false); thetime1.setText("Time: N/A"); } }
~Zack
- 10-17-2010, 05:57 AM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
You're welcome. As regards the understanding, I'm in the same boat as you. So I guess we have until one of the Swing guys swing by.
(You say you always want value but the method gets called twice per click. So long as the first doesn't result in an exception you will catch value the second time around.)
With your code I am able to see any number of exceptions by clicking alternately on the two cells in the top row. But if I slow down and click s-l-o-w-l-y on one of the cells the selection "takes" and the exceptions disappear.
[Edit]
I swear that was the behaviour a couple of hours ago. After restarting this computer I don't see it. Now the original code gives 2 exceptions on the first click. row==-1 can be avoided by just saying "if(row == -1) return;" before the calculation of value. Note that with either of these solutions the listener method gets called 4 times on the first click which I think has something to do with the problem.Last edited by pbrockway2; 10-17-2010 at 06:07 AM.
- 10-17-2010, 07:03 AM #9
Aw, c'mon, it's the weekend and they're out swingin' ;) I'll try to fill in for them.I guess we have until one of the Swing guys swing by.
Some notes:
1. Please indent your code consistently and correctly. It makes it easier to read and can get you better help sooner. In this case, I glanced at the code, kept the browser tab open and only came back to it when there was nothing else to interest me on 4 forums, and that only because just a few lines were wrongly indented. (Where the indenting appears totally random, I don't come back.)
2. The first two lines of these three are redundant. Take a look at the JTable source for the last method called to know why.1A. Please remove trailing spaces as a courtesy to members who have limited bandwidth.Java Code:table.setRowSelectionAllowed(true); table.setColumnSelectionAllowed(true); table.setCellSelectionEnabled(true);
3. The last line of these three is redundant. Can you see why?4. "thing" is an extremely poor choice of name for a class on two accounts: a class name should be descriptive, and should start with an uppercase letter.Java Code:ListSelectionModel listSelectionModel = table.getSelectionModel(); listSelectionModel.addListSelectionListener(listener); table.setSelectionModel(listSelectionModel);
5. The same listener is added to both the table's ListSelectionModel and the table's ColumnModel's ListSelectionModel. So when both row and column change, two valueChanged events are generated. Since AWT and Swing do not specify the sequence of event delivery, the initially selected column may change while the selected row is still -1 (no selection) or vice versa. Your code has to deal with this.
After you've made necessary changes, post the new code if you still have any problems.
db
- 10-17-2010, 05:08 PM #10
Member
- Join Date
- Oct 2010
- Posts
- 6
- Rep Power
- 0
~ZackJava Code:import javax.swing.*; import javax.swing.event.*; import java.awt.*; public class test { JTable table; public static void main(String[] args) { new Test(); } public Test() { String[] columns = {"One", "Two"}; String[][] data = {{"bla", "bla"}, {"more", "and more"}}; JFrame frame = new JFrame("Title"); JPanel panel = new JPanel(new BorderLayout()); table = new JTable(data, columns); JScrollPane pane = new JScrollPane(table); panel.add(pane); frame.add(panel); table.setCellSelectionEnabled(true); table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); TableListener listener = new TableListener(); ListSelectionModel listSelectionModel = table.getSelectionModel(); listSelectionModel.addListSelectionListener(listener); table.getColumnModel().getSelectionModel().addListSelectionListener(listener); frame.setVisible(true); frame.setSize(400, 400); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } class TableListener implements ListSelectionListener { public void valueChanged(ListSelectionEvent l) { int row = table.getSelectedRow(); int column = table.getSelectedColumn(); boolean isAdjusting = l.getValueIsAdjusting(); System.out.println(row); String value = (String) table.getValueAt(row, 0); if (!isAdjusting) { if (column != 0) { System.out.println("valid: " + value); } if (column == 0) { System.out.println("invalid"); } } } } }
- 10-17-2010, 07:29 PM #11
That's a result of mixing spaces and tabs. All editors don't expand a tab character to the same number of spaces.I indent all my code perfectly, but the texteditor likes to screw it up.
And one of those things that might deter members from taking and interest.I know, I know. Just temporary.
I'm pointing out the ways for you to get better help sooner. I have no other interest in voicing criticism.
Sure, you do need a listener on both the selection models.The reason I'm adding the listener twice is because if I only add it to the table's ListSelectionModel, it won't pick up column changes.
With a boolean test. And remember that it could be the column that's -1.So I understand that two events are being generated, but how do I deal with the possibility of the row being -1?
Untested code.If the columns are movable, you may need to convertColumnIndexToModel.Java Code:class TableListener implements ListSelectionListener { @Override public void valueChanged(ListSelectionEvent l) { if (l.getValueIsAdjusting()) { return; } int row = table.getSelectedRow(); int column = table.getSelectedColumn(); if (row == -1 || column == -1) { return; } System.out.println(row); if (column == 0) { System.out.println("invalid"); } else { System.out.println("valid: " + table.getValueAt(row, 0)); } }
db
Similar Threads
-
ArrayIndexOutofBounds Exception
By atul.goldenstring in forum New To JavaReplies: 10Last Post: 04-10-2010, 10:47 AM -
Adding New JTable in JTable
By anilkumar_vist in forum New To JavaReplies: 0Last Post: 01-27-2010, 08:27 AM -
ArrayIndexOutOfBounds
By SwEeTAcTioN in forum New To JavaReplies: 6Last Post: 12-07-2009, 12:59 AM -
Add a row in JTable
By makpandian in forum AWT / SwingReplies: 4Last Post: 04-15-2009, 08:48 PM -
Jtable duplicates through Hashtable (JTable condition problem) my assignment plz help
By salmanpirzada1 in forum Advanced JavaReplies: 2Last Post: 05-15-2008, 10:15 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks