Small refactoring in a Java Swing application causes a significant slowdown

I was tasked with refactoring the Java Swing application, which is pretty cool here. My task is to clear the code and make it more dynamic, since we will start using it with the second project, and it should be able to configure it accordingly. There is a small part of a single window containing

  • Button
  • JFormattedTextField, which is used to select dates.
  • 3X4 JLabels table showing data.

The person who originally wrote this just used the GridBagLayout JPanel and then hardcoded everything, including the table and row header label, and left the empty JLabel in the dynamic data position. When dynamic information is received setTextis called with text data. Part of my refactoring will lead to the fact that the whole table will be dynamic in size and also in content, so I decided to make the table a sub-panel with GridLayout and dynamically set the contents and sizes using this code fragment:

    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 checked that this is only ever called once for each window, but the whole window is now incredibly sluggish. It may take> 5 seconds to respond to clicks in other parts of the frame that were used to respond in fractions of a second. Does anyone know what might cause this? Is there something in GridLayouts that I don’t understand?

+3
source share
2 answers

Try calling this code on EDT.

+4
source

, , GridLayouts. , , , , , , .

+3

Source: https://habr.com/ru/post/1784567/


All Articles