Solved

JTable repaint()

Posted on 2002-04-08
16
1,635 Views
Last Modified: 2012-08-14
When I call the repaint() method of JTable, the row selection is vanished. I would like to restore the selection status.
Holding the selected rows indices is a bad idea, since their order might be changed after repainting (if the table is sorted, for example).
Any idea?
0
Comment
Question by:s_lavie
  • 6
  • 6
  • 4
16 Comments
 
LVL 16

Accepted Solution

by:
heyhey_ earned 50 total points
ID: 6925293
> since their order might be changed after repainting

WRONG. you never need to call repaint() - all you have to do is make changes inside your models (TableModel, SelectionModel) and fire appropriate events. TableUI will handle these events and repaints itself if needed.
0
 
LVL 92

Expert Comment

by:objects
ID: 6926949
> When I call the repaint() method of JTable,
> the row selection is vanished.

Then the row must no longer be selected.
0
 
LVL 2

Author Comment

by:s_lavie
ID: 6927532
Okay, no more calling to repaint() and the selection is not vanished, but the wrong line is now selected, i.e. for example I have a table like this:

0     a
1     b
2     c
3     d

Now, suppose row index 1 is selected (1     b), when I sort the table it looks like this:

3     d
2     c
1     b
0     a

And again row index 1 is selected (2     c), whereas row number 3 is supposed to be selected ((1     b) as before).

Now, what do I miss here, and how I fix it?
0
 
LVL 2

Author Comment

by:s_lavie
ID: 6927536
In row number 3 I meant row index 2
0
 
LVL 92

Expert Comment

by:objects
ID: 6927551
How's your sort implemented?
0
 
LVL 2

Author Comment

by:s_lavie
ID: 6927598
In MyTableModel (that extends DefaultTableModel) I have a sort method:

public void sortTable(JTable table, Comparator comparator)
{
     for (int i = 0; i < getColumnCount(); i++)
     {
          TableColumn column = colModel.getColumn(i);
          column.setHeaderValue(getColumnName(column.getModelIndex()));     // getColumnName() is overriding the getColumnName() in DefaultTableModel.
     }
     table.getTableHeader().repaint();

     Collections.sort(dataVector, comparator);
}
0
 
LVL 92

Expert Comment

by:objects
ID: 6927611
You're not firing any event informing listenerer that you have changed the model.
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 6927707
and another thing - the overall algorithm should be:

- save the selected row id
- sort
- find the new index for the row with saved id
- select this index.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 92

Expert Comment

by:objects
ID: 6927723
I actually think the selection model listens to the table model, and updates itself. (But I could be wrong).
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 6927730
> I actually think the selection model listens to the table model

I agree, but when you sort the table the only possible event to throw is

TableModelEvent(source, 0, max, ALL_COLUMNS, UPDATE);

and SelectionModel will use the same selection index.
0
 
LVL 2

Author Comment

by:s_lavie
ID: 6927767
I added the line:
table.tableChanged(new TableModelEvent(this, 0, table.getRowCount() - 1, TableModelEvent.ALL_COLUMNS, TableModelEvent.UPDATE));
After the line:
Collections.sort(dataVector, comparator);

> - save the selected row id
> - sort
> - find the new index for the row with saved id
> - select this index.

That's exactly what I was afraid of:
Find the new index for the row with saved id is quiet heavy isn't it? it means for every selected row I have to run over the entire table!
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 6927781
> That's exactly what I was afraid of:

you have to find out the new index for the selected row somehow - there isn't magic solution :)
0
 
LVL 2

Author Comment

by:s_lavie
ID: 6927807
Do you have any intelligent solution for saving and finding those indices?
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 6927829
ok, let's define the REAL problem:

- you have collection of Objects;
- you rearrange (sort) the items in the collection;
- you want to find the new index for some item, given the old index.

I cannot think of any good solution right now, but maybe somebody else will be able to help you
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 6927845
hey, thanks for the points !
0
 
LVL 2

Author Comment

by:s_lavie
ID: 6927886
You earned them!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Application launch issue with Apache Tomcat 5 23
Printing to a specific printer tray (HP 806dn printer) 3 83
Image decoding from Camera 3 72
tomcat not starting 6 31
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now