Question

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.

Was it helpful?

Solution

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

OTHER TIPS

Read tutorial about SwingWorker and to use

  • process()

  • publish()

  • setProgress()

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top