Minor Refactoring in Java Swing application causes huge slowdown
I've been tasked with doing refactoring to a Java Swing application we have here that's pretty crufty. My job is to clean up the code and make it more dynamic as we're going to start using it with a second project and it needs to be able to adjust appropriately. There is a small portion of one window that contains
- A button
- A JFormattedTextField that is used to select dates.
- A 3X4 table of JLabels that display data.
The person who originally wrote this simply used a GridBagLayout JPanel and then hardcoded everything, including the table's header and row label's and left empty JLabel's in the dynamic data position. When the dynamic information is received setText
is called with the text data. Part of my refactoring will cause the entire table to be dynamic in dimension as well as content so I decided to make the table a sub-panel with a GridLayout and dynamically set the contents and dimensions with this piece of code:
public void updateInfoPanel(ArrayList rows) { System.out.println("Updating Info Panel!"); //genericInfo is the new sub panel in question. genericInfo.removeAll(); Grid开发者_开发百科Layout layout = new GridLayout(); layout.setColumns(rows.get(0).length); layout.setRows(rows.size()); genericInfo.setLayout(layout); for(String[] row : rows) { for(String element : row) { genericInfo.add(new Label(element)); } } }
I have verified that this is only ever getting called one time per window creation but the entire window is now incredibly sluggish. It can take >5 seconds to respond to clicks in other parts of the frame that used to respond in fractions of a second. Does anyone know what would cause this? Is there something about GridLayouts that I don't understand?
Try calling this code on the EDT.
No it appears you understand GridLayouts. The problem is elsewhere, look at other code that you might have changed, and do some profiling to determine the true source of the slowdown.
精彩评论