3
\$\begingroup\$

I am working on an application which first loads a 'settings' panel and then loads the application. It all works fine, loading the settings panel takes a few seconds longer than I would like it to. The problem is in loading the settings panel, not the application itself.

I have a simple main method:

public class Main {
    public static void main(String[] args) {
        new FrameInitializer().init();
    }
}

FrameInitializer class:

public class FrameInitializer {
    JFrame frame;
    JPanel wrapper, settings;
    public JComboBox<Integer> bLevel, wLevel;
    public JRadioButton bHuman, wHuman, bComputer, wComputer;
    public JButton readyButton;

    public FrameInitializer() {}
    public void init() {
        frame = new JFrame();
        settings = new SettingsPanel();
        wrapper = new JPanel();
        wrapper.add(settings);
        frame.setContentPane(wrapper);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setVisible(true);
    }

    class SettingsPanel extends JPanel {

    private static final long serialVersionUID = 1L;

    public SettingsPanel() {

        BlackRadioListener blackRadioListener = new BlackRadioListener();
        WhiteRadioListener whiteRadioListener = new WhiteRadioListener();
        Integer[] levels = {1, 2, 3, 4, 5, 6, 7, 8};
        Font title = new Font("Sans Serif", Font.BOLD, 18);
        setLayout(new BorderLayout());

        JPanel topPanel = new JPanel();
        topPanel.setLayout(new GridLayout(1, 2));
        JLabel black = new JLabel("Black", SwingConstants.CENTER);
        black.setFont(title);
        JLabel white = new JLabel("White", SwingConstants.CENTER);
        white.setFont(title);
        topPanel.add(black);
        topPanel.add(white);

        /* Construct options for black player */
        JPanel blackPanel = new JPanel();
        blackPanel.setLayout(new BoxLayout(blackPanel, BoxLayout.Y_AXIS));
        bHuman = new JRadioButton("Human", true);
        bHuman.addActionListener(blackRadioListener);
        bComputer = new JRadioButton("Computer", false);
        bComputer.addActionListener(blackRadioListener);
        bLevel = new JComboBox<Integer>(levels);
        bLevel.setSelectedIndex(5);
        bLevel.setEnabled(false);

        ButtonGroup blackGroup = new ButtonGroup();
        blackGroup.add(bHuman);
        blackGroup.add(bComputer);

        //blackPanel.add(black);
        blackPanel.add(bHuman);
        blackPanel.add(bComputer);
        blackPanel.add(bLevel);

        /* Construct options for white player */
        JPanel whitePanel = new JPanel();
        whitePanel.setLayout(new BoxLayout(whitePanel, BoxLayout.Y_AXIS));
        wHuman = new JRadioButton("Human", false);
        wHuman.addActionListener(whiteRadioListener);
        wComputer = new JRadioButton("Computer", true);
        wComputer.addActionListener(whiteRadioListener);
        wLevel = new JComboBox<Integer>(levels);
        wLevel.setSelectedIndex(5);
        wLevel.setEnabled(true);

        ButtonGroup whiteGroup = new ButtonGroup();
        whiteGroup.add(wHuman);
        whiteGroup.add(wComputer);

        //whitePanel.add(white);
        whitePanel.add(wHuman);
        whitePanel.add(wComputer);
        whitePanel.add(wLevel);

        /* Organize entire JPanel */
        JPanel centerPanel = new JPanel();
        centerPanel.setLayout(new BorderLayout());
        centerPanel.add(blackPanel, BorderLayout.WEST);
        centerPanel.add(whitePanel, BorderLayout.EAST);

        JPanel southPanel = new JPanel();
        southPanel.setLayout(new FlowLayout());
        readyButton = new JButton("Play Othello");
        readyButton.addActionListener(new ButtonListener());
        southPanel.add(readyButton);

        this.add(topPanel, BorderLayout.NORTH);
        this.add(centerPanel, BorderLayout.CENTER);
        this.add(southPanel, BorderLayout.SOUTH);
    }
    class BlackRadioListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            JRadioButton radio = (JRadioButton)(e.getSource());
            boolean isEnabled = radio.getText().equals("Computer");
            bLevel.setEnabled(isEnabled);
        }
    }
    class WhiteRadioListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            JRadioButton radio = (JRadioButton)(e.getSource());
            boolean isEnabled = radio.getText().equals("Computer");
            wLevel.setEnabled(isEnabled);
        }
    }
    class ButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            //Launches application
        }
    }
}
}

Are there any glaring errors I'm making that would explain a significant increase in load time? Is there any way I can make this code render faster?

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Looks like most of the time is spent in the frame.pack() call. Replacing it with frame.setSize(222, 181); is a lot faster. I simply ran the code once with frame.pack() and added a line to output the resulting frame size. Then replaced the pack() call.

Setting the size statically like this is faster, since the code doesn't have to dynamically evaluate all the underlying components, but it has the drawback that you'll have to manually update the optimum size of the frame every time you change the components.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.