0

I've created a Table that is basically a JPanel with other panels as rows containing JTextArea components to display data. I didn't want to use a JTable to have full control by myself.

When a row is clicked on I want it to be highlighted through changing its border. I implemented a Mouselistener that does the job for every text area on mouse pressed. It works. It works with 1000 entries, but when I reach more, i.e. 5000 it takes ages for the listener to do his work. Obviously it gets worse the more entries I have. But this doesn't make sense to me, as I only change one rows border, no matter whether there is 1 or 10000 others.

Can anyone point me to the reason for this? Even if I put a System.out right after "mouse pressed" it takes ages for the output. So I guess its not the executed methods (that are not time consuming at all) themselves.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user4591830
  • 35
  • 1
  • 7
  • The cost is due to the lines 243 and 654, says my crystal ball. – JB Nizet Jul 12 '15 at 16:13
  • 2
    You should *really* consider using a table with an appropriate cell renderer for this, as it does all this sort of stuff well (without 'taking ages'). If you want us to help debug the current approach, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). – Andrew Thompson Jul 12 '15 at 16:13
  • 1
    *.. I only change one rows border"* Likely triggering a call to get the preferred size of the ***parent*** container.. – Andrew Thompson Jul 12 '15 at 16:16
  • 1
    @AndrewThompson is correct; as suggested [here](http://stackoverflow.com/a/10263356/230513), `JTable` is an easy way to get [flyweight](https://en.wikipedia.org/wiki/Flyweight_pattern) rendering. – trashgod Jul 12 '15 at 17:28
  • After trying a JTable (still uncustomized) I see there is no other way. It's a pain as I've been working for 2 weeks on a selfmade table with my whole program being finished, if there were no performance issues. For future reference: My custom table consisting of swing elements took roughly 6 minutes to load and display 30000 rows, while JTable needs 2 seconds for the job. So thanks for pushing me into the right direction, even if I've got loads of work in front of me :) – user4591830 Jul 13 '15 at 10:53

0 Answers0