3

To hide a column from only the view of JTable, i am using the removeColumn() method. But it throws the exception

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 7 >= 7
at java.util.Vector.elementAt(Vector.java:470)
at javax.swing.table.DefaultTableColumnModel.getColumn(DefaultTableColumnModel.java:294)
at javax.swing.plaf.basic.BasicTableHeaderUI.paint(BasicTableHeaderUI.java:648)

i think, after removing column from the view, if i modified the model, then this exception pops out. is it because of there is no column in view, while the model is updating the table ?

What is the best way to hide the column in view in JTable ? insteading of setting the sizes to 0

EDIT: The exception is not occuring regularly. it is a random exception. Anyway here is the code:

    @Override
        protected Object doInBackground() throws Exception {
        ........
        resultDTO=//get data from database
        tableModel.setDataVector(resultDTO.getAllRows(), tableModel.getColumnNames());    
        // hide column
        table.removeColumn(table.getColumnModel().getColumn(7));
            System.out.println("table column count : " + table.getColumnCount());
            System.out.println("model column count : " + tableModel.getColumnCount());
        ........
        .........       
        }



initial result (with out any data in table, at application startup): 
table column count : 7
model column count : 8

after populating data (first running of above method):
table column count : 7
model column count : 8

after few times executing : 
table column count : 7
model column count : 8
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 7 >= 7
    at java.util.Vector.elementAt(Vector.java:470)
    at javax.swing.table.DefaultTableColumnModel.getColumn(DefaultTableColumnModel.java:294)
    at javax.swing.plaf.basic.BasicTableHeaderUI.paint(BasicTableHeaderUI.java:648)
    at javax.swing.plaf.synth.SynthTableHeaderUI.paint(SynthTableHeaderUI.java:173)

Some times the above exception occures when i first load the data, and sometimes its not.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Sanjeev
  • 397
  • 1
  • 8
  • 20
  • I see that the exception is thrown from the model while attempting to paint. Is it possible that the model is still reporting that there are 8 total columns after you've removed one and reduced it to 7? There really isn't much information here for us to go on. – arcy Sep 01 '12 at 03:34
  • Looks similar to [Bug ID: 6586009](http://bugs.sun.com/view_bug.do?bug_id=6586009) – tenorsax Sep 01 '12 at 03:38
  • Did you try & remove the colmn from the model outside the EDT? – MadProgrammer Sep 01 '12 at 04:04

2 Answers2

1

You're trying to update UI components out side of the Event Dispatching Thread, this is NEVER a good idea

protected Object doInBackground() throws Exception {
    ........
    resultDTO=//get data from database
    // This shouldn't be done here
    tableModel.setDataVector(resultDTO.getAllRows(), tableModel.getColumnNames());  
    // and neither should this
    // hide column
    table.removeColumn(table.getColumnModel().getColumn(7));

Thumbs up for using a SwingWorker though. The problem is, Swing components are not thread safe and you should never try to update them outside of he EDT, because they cause unexpected results (like you've just encounted).

Rather then setting the row data directly, I'd suggest using the publish/process methods. If you can't decide when to remove the column, I'd remove it either before the worker executes or in the workers done method

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • ok, and i just want to know one thing about swing worker. if any exception occurs during the execution of doInBackground(), then what happens? – Sanjeev Sep 01 '12 at 06:59
  • @sanjeev That's a dam good question! `doInBackground` is called from a `Callable` which in turn is launched by a `ExecutorService`...generally, they get absorbed...There's no way to obtain a reference to the `Callable` after the execution. You can, however, add a try/catch block your code and trap any execution that might be thrown, I would then maintain a reference that could be obtained later, such as in the `done` method – MadProgrammer Sep 01 '12 at 07:32
1

Read tutorial about SwingWorker and to use

  • process()

  • publish()

  • setProgress()

for notify, add, remove, modify Swing JComponents from doInBackground()

mKorbel
  • 109,525
  • 20
  • 134
  • 319