Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Minor Refactoring in Java Swing application causes huge slowdown

Tags:

java

swing

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();
        GridLayout 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?

like image 453
asm Avatar asked Jul 12 '26 22:07

asm


1 Answers

Try calling this code on the EDT.

like image 183
user489041 Avatar answered Jul 14 '26 10:07

user489041



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!