How do I use the InvokeLater to show all my components in JFrame?

36 Views Asked by At

I am trying to show different JPanels using JSplitPane.

I use SwingUtilities.InvokeLater to show all the components to avoid sluggishness in UI.

But the JPanel on the bottom which is pinkPane doesn't show until I resize the window.

I tried my chance using several threads to show the JPanel at the bottom:

-I have used 2-3 threads. One to initialize the class. One to revalidate the splitPane. The last one was the SwingUtilities.InvokeLater(new Runnable...) (Hoping one to solve the issue)

Here is my code:

    package changing;
    
    import java.awt.Color;
    import java.awt.Dimension;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JSplitPane;
    import javax.swing.SwingUtilities;
    import javax.swing.WindowConstants;
    
    public class splitPaneFrame {
        
        private static JFrame mainFrame = new JFrame();
        private static JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
        private static JSplitPane middle = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
        
        private int width = 1500;
        private int height = 860;
        
        public splitPaneFrame() {
            // TODO Auto-generated constructor stub
            mainFrame.setSize(width, height);
            mainFrame.setVisible(true);
            mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            initialization();
            mainFrame.add(splitPane);
        }
        
        private static void initialization() {
            JSplitPane mostRight = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
    
            JPanel cyanPane = new JPanel();
            cyanPane.setBackground(Color.CYAN);
    
            JPanel orangePane = new JPanel();
            orangePane.setBackground(Color.ORANGE);
            
            mostRight.setDividerSize(0);
            mostRight.setDividerLocation(400);
            mostRight.setLeftComponent(cyanPane);
            mostRight.setRightComponent(orangePane);
            mostRight.setOneTouchExpandable(false);
            
            JPanel yellowPane = new JPanel();
            yellowPane.setBackground(Color.YELLOW);
            
            yellowPane.setSize(new Dimension(600, 800));
            yellowPane.setMaximumSize(yellowPane.getSize());
            yellowPane.setPreferredSize(yellowPane.getSize());
            middle.setDividerSize(0);
            middle.setDividerLocation(600);
            middle.setLeftComponent(yellowPane);
            middle.setRightComponent(mostRight);
            middle.setOneTouchExpandable(false);
            
            JPanel magentaPane = new JPanel();
            magentaPane.setBackground(Color.MAGENTA);
            
            JSplitPane mostLeft = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
            
            mostLeft.setDividerLocation(250);
            mostLeft.setDividerSize(0);
            mostLeft.setLeftComponent(magentaPane);
            mostLeft.setRightComponent(middle);
            mostLeft.setOneTouchExpandable(false);
            
            // vertically medium panes end here
            ////////////////////////////////////////////////////////////////////////////////
            
            // Top and Bottom Horizontal Panes start here
            // splitPane on the most bottom
            JSplitPane bottom = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
            
            JPanel pinkPane = new JPanel();
            pinkPane.setBackground(Color.PINK);
            
            pinkPane.setSize(new Dimension(1500,30));
            pinkPane.setPreferredSize(new Dimension(1500,30));
            pinkPane.setMaximumSize(pinkPane.getPreferredSize());
            
            bottom.setDividerLocation(800);
            bottom.setDividerSize(0);
            bottom.setTopComponent(mostLeft);
            bottom.setBottomComponent(pinkPane);
            bottom.setOneTouchExpandable(false);
            bottom.setVisible(true);
            pinkPane.setVisible(true);
            
            JPanel redPane = new JPanel();
            redPane.setBackground(Color.RED);
            redPane.setSize(new Dimension(1500,30));
    
            // main splitPane
            splitPane.setDividerLocation(30);
            splitPane.setDividerSize(0);
            splitPane.setTopComponent(redPane);
            splitPane.setBottomComponent(bottom);
            splitPane.setOneTouchExpandable(false);
        }
        
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            SwingUtilities.invokeLater(new Runnable() {
                
                public void run() {
                    // TODO Auto-generated method stub
                    splitPaneFrame frame = new splitPaneFrame();
                }
            });
        }
    
    }
0

There are 0 best solutions below