Results 1 to 7 of 7
- 12-12-2012, 10:33 PM #1
Member
- Join Date
- Dec 2012
- Posts
- 4
- Rep Power
- 0
Creating UI became very slow in Java 1.7
Some software I am maintaining has a JPanel containing a large number of JLabels. When run in JRE 1.6, the UI is created and displayed in a few seconds, but when run in JRE 1.7 it takes a couple *minutes* to display. When I profile or debug the code, it looks like the biggest change is that it is now spending a ton of time in Component.updateZOrder() which is called by Component.addNotify() which is recursively called on every component in the JFrame once it is made visible. In JRE 1.6 this particular function did not exist and the equivalent stacking order code in addNotify() doesn't seem to have any performance problems; in particular ContainerPeer.isRestackSupported() always returns false, so restack() is never called.
Has anyone run into this problem before? Is there a work-around?
- 12-13-2012, 12:37 AM #2
Member
- Join Date
- Dec 2012
- Posts
- 4
- Rep Power
- 0
Re: Creating UI became very slow in Java 1.7
Here is a simple example that demonstrates the problem.
I should note that once the UI is finally displayed the real application works and performs just fine in both 1.6 an 1.7.Java Code:import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; /** * This example starts immediately in Java 1.6 but takes several seconds to load in Java 1.7 */ public class ManyLabelExample extends JPanel { private static final int ROWS = 128; private static final int COLS = 512; private static final int ROW_HEIGHT = 12; private static final int COL_WIDTH = 4 * 6 + 20; public ManyLabelExample () { setLayout(null); for (int r = 0; r < ROWS; r++) { for (int c = 0; c < COLS; c++ ) { JLabel label = new JLabel(); label.setText(r +","+ c); label.setBounds(c*COL_WIDTH, r*ROW_HEIGHT, COL_WIDTH, ROW_HEIGHT); add(label); } } } public static void main (String[] args) { JFrame frame = new JFrame(); JPanel panel = new ManyLabelExample(); frame.add(panel); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); long startTime = System.nanoTime(); frame.setVisible(true); System.out.println((System.nanoTime() - startTime)/1e9); } }
- 12-13-2012, 04:35 AM #3
Re: Creating UI became very slow in Java 1.7
All Swing constructors and methods (with a few exceptions) should be invoked on the EDT. Do you find a difference if you wrap the code in your main(...) method in a SwingUtilities#invokeLater(...) ?
db
edit No, it doesn't make any significant differenceLast edited by DarrylBurke; 12-13-2012 at 04:54 AM.
Why do they call it rush hour when nothing moves? - Robin Williams
- 12-13-2012, 05:11 AM #4
Re: Creating UI became very slow in Java 1.7
I think you need to adopt an approach that uses a renderer component: JTable or JList. For example, this uses a JList:
dbJava Code:import java.awt.BorderLayout; import java.awt.Dimension; import javax.swing.*; public class ManyLabelExample extends JPanel { private static final int ROWS = 128; private static final int COLS = 512; private static final int ROW_HEIGHT = 12; private static final int COL_WIDTH = 4 * 6 + 20; public ManyLabelExample() { setLayout(new BorderLayout()); DefaultListModel model = new DefaultListModel(); for (int r = 0; r < ROWS; r++) { for (int c = 0; c < COLS; c++) { model.addElement(r + "," + c); } } JList list = new JList(model); list.setVisibleRowCount(ROWS); list.setLayoutOrientation(JList.HORIZONTAL_WRAP); list.setCellRenderer(new DefaultListCellRenderer() { final Dimension preferredSize = new Dimension(COL_WIDTH, ROW_HEIGHT); @Override public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { return super.getListCellRendererComponent(list, value, index, false, false); } @Override public Dimension getPreferredSize() { return preferredSize; } }); add(new JScrollPane(list)); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { JFrame frame = new JFrame(); JPanel panel = new ManyLabelExample(); frame.add(panel); frame.setSize(800, 800); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); long startTime = System.nanoTime(); frame.setVisible(true); System.out.println((System.nanoTime() - startTime) / 1e9); } }); } }Last edited by DarrylBurke; 12-13-2012 at 05:15 AM. Reason: Added getListCellRendererComponent override
Why do they call it rush hour when nothing moves? - Robin Williams
- 12-13-2012, 06:23 PM #5
Member
- Join Date
- Dec 2012
- Posts
- 4
- Rep Power
- 0
Re: Creating UI became very slow in Java 1.7
While the simple example I posted has everything aligned in a simple grid, this isn't the case with the real application, so JTable or JList won't work there. I have verified that using a LayoutManager has no effect on the start up performance and significantly decreases the run-time performance of the application, so that isn't a solution either. If push comes to shove, I could abandon using JLabels/JPanels altogether and draw everything directly onto a single canvas, although that is definitely not desirable; the code is much cleaner this way.
And of course, we really don't want to completely rewrite a bunch of code that was working perfectly fine yesterday if we can instead find a simple work-around to this performance regression.Last edited by pavon; 12-13-2012 at 06:26 PM.
- 12-13-2012, 08:57 PM #6
Re: Creating UI became very slow in Java 1.7
The only workaround I can think of is to bundle a JRE 1.6 with your application, along with a launcher that ensures ot is used regardless of a later version being already installed. Or, and note that I've never tried anything remotely like that, maybe including a JRE6 rt.jar on the classpath would do it.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 01-10-2013, 12:48 AM #7
Member
- Join Date
- Dec 2012
- Posts
- 4
- Rep Power
- 0
Re: Creating UI became very slow in Java 1.7
What I ended up doing to solve this without a complete rewrite was to group the display into a bunch of sub-panels. For example in the simple code I posted, putting all the JLabels in a single column into their own JPanel. That way instead of having (M*N)^2 Z-Order comparisons, you would end up with M*(N^2)+M^2 comparisons. Again, my real application wasn't arranged in a grid, but there was a natural way to hierarchically nest the components that drastically decreased the total number of components in each container, and made performance reasonable again.
Similar Threads
-
java event handling is too slow
By yemista in forum AWT / SwingReplies: 4Last Post: 05-16-2012, 04:58 AM -
java 1.7.0 update 2 very slow?
By Matthew Doucette in forum Advanced JavaReplies: 0Last Post: 02-02-2012, 08:40 PM -
System Freeze/Slow-down using Java
By Grunker in forum New To JavaReplies: 12Last Post: 06-22-2011, 03:03 PM -
Why my java lan game programm slow!
By arnelpogs in forum NetworkingReplies: 2Last Post: 12-07-2009, 04:00 PM -
Why JAVA is so slow?
By aRTx in forum New To JavaReplies: 2Last Post: 05-11-2009, 10:19 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks