Search code examples
javaswinguser-interfacejcombobox

Gui JCombobox Text gets blurred


Recently I have been getting into some Gui programming in Java. I noticed that when I created a JComboBox and tried to display in my gui that the text doesn't come in full. Alot of the time it is blurred, as shown as below. I have tried increasing the size of the GridBagConstraint but it still occurs. This would also happen to the button once I press it as well.

enter image description here

Class 1:

    public class load {

        private JFrame frame;

        public static void main(String args[]) throws InvocationTargetException,
                InterruptedException {

            EventQueue.invokeAndWait(new Runnable() {
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager
                                .getSystemLookAndFeelClassName());
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                    // Create the UI here
                    load window = new load();

                    window.frame.setVisible(true);
                }
            });
        }

        private void loadGui() {
            JButton create = new JButton();
            create.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent arg0) {
                    // TODO Auto-generated method stub

                    SelectionView a = new SelectionView();

                    // VVPRIMARY ERROR CURRENTLY VV
                    // unable to setvisible false without the nextframe losing pixel
                    frame.setVisible(false);
                    frame.dispose();


                }

            });

            frame.setSize(400, 400);
            frame.add(create);


        }

    }

Class 2:

public class SelectionView extends JFrame {

    public SelectionView() {
        // intialize frame
        JFrame selection = new JFrame("Sport Selection");
        JPanel a = new JPanel();

        a.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        selection.setSize(300, 500);

        final JComboBox box = createDropdown();
        JButton load = new JButton("Load");

        load.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                int value = box.getSelectedIndex();

                switch (value) {
                case 0:
                    TableTennisView a = new TableTennisView();
                    break;
                case 1:
                    BasketBallView b = new BasketBallView();
                    break;
                default:
                    System.out.println("Nothing");
                    break;
                }
            }

        });
        // create comboBox


        a.add(box, c);

        a.add(load, c);

        selection.add(a);
        selection.setVisible(true);

    }

    /**
     * Method CreateDropDown
     * 
     * Creates the dropdown menu for the selection view
     * 
     * @return the dropdown menu used in the view
     */
    public JComboBox createDropdown() {
        String[] sport = { "Table Tennis", "BasketBall" };

        JComboBox cb = new JComboBox(sport);

        return cb;
    }
}

Solution

  • Make sure you are initialise (and updating) the UI from within the context of the Event Dispatching Thread.

    See Initial Threads for more details.

    EventQueue.invokeAndWait(new Runnable() {
        @Override
        public void run() {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                ex.printStackTrace();
            }
            // Create the UI here
        }
    });
    

    Also, if possible, call setVisible on the window last, after you have established the basic UI. If the UI is dynamic, you will need to call revalidate and repaint on the parent container that you are adding your components to or consider using a CardLayout

    Some video drivers can cause problems on some platforms, if problems persist, consider providing a runnable example which demonstrates your problem. This will result in less confusion and better responses