9

Basically, the problem is that when I either click or hover the mouse over a JComboBox my program's appearance gets all glitched.

Here is how my program look like right after I load it (how it should always look): non-glitched application

And here is what happens after a while of clicking one of the JComboBox'es a few times: glitched application

I was researching around for similar problems, and in this page someone was suggesting not to use absolute positioning; but I am not doing that in my program so I'm out of ideas.

Here is the the part of my code where I construct the layout and combo-boxes for my program, which goes in the constructor of my class, and which I launch from main by using: EventQueue.invokeLater(() -> new DownloadCalculator());.

<EDIT: Code replaced with a runnable example. See update #1 below.>

UPDATE 1: Here is a runnable demo to illustrate the problem. The exact same problem will occur with the combo boxes after a while of fiddling with them. Only the code necessary to reproduce the glitch is included, the functional part that does the main calculations has been removed because it is unrelated to the problem:

import java.awt.*;
import javax.swing.*;

public class RunnableExample
{
    private final JFrame mainframe;
    private JTextField downloadSize, downloadSpeed, answerBox;
    private JComboBox<String> byteTypeSize, byteTypeSpeed, answerTimeUnit;

    public RunnableExample()
    {
        mainframe = new JFrame("Download Calculator");
        mainframe.setLayout(new BoxLayout(mainframe.getContentPane(), BoxLayout.Y_AXIS));
        mainframe.setSize(new Dimension(600, 300));
        mainframe.setResizable(false);
        mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainframe.setLocationRelativeTo(null);

        initLook();
        mainframe.setVisible(true);
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(() -> new RunnableExample());
    }

    private void calculateDownloadTime(String size, int sizeUnitType, String speed,
                                       int speedUnitType)
    {
        return; // program's core logic goes here
    }

    private void initLook()
    {
        JPanel subLine1 = new JPanel(), subLine2 = new JPanel(), subLine3 = new JPanel();

        JLabel question1 = new JLabel("File size to be downloaded:");
        downloadSize = new JTextField(10);
        byteTypeSize = new JComboBox<>(new String[]{"Bytes", "Kilobytes", "Megabytes", "Gigabytes", "Terabytes"});
        byteTypeSize.setSelectedIndex(3);

        JLabel question2 = new JLabel("Average download speed:");
        downloadSpeed = new JTextField(10);
        byteTypeSpeed = new JComboBox<>(new String[]{"Bytes / s", "Kilobytes / s", "Megabytes / s", "Gigabytes / s", "Terabytes / s"});
        byteTypeSpeed.setSelectedIndex(1);

        JButton buttonCalc = new JButton("Calculate");
        answerTimeUnit = new JComboBox<>(new String[]{"Seconds", "Minutes", "Hours", "Days"});
        answerTimeUnit.setSelectedIndex(2);
        buttonCalc.addActionListener((e) -> new Thread(() -> calculateDownloadTime(downloadSize.getText(), byteTypeSize.getSelectedIndex(), downloadSpeed.getText(), byteTypeSpeed.getSelectedIndex())).start());
        answerBox = new JTextField("Hit \"Calculate\" button to see answer here.", 25);
        answerBox.setEnabled(true);

        subLine1.setLayout(new FlowLayout(FlowLayout.CENTER));
        subLine2.setLayout(new FlowLayout(FlowLayout.CENTER));
        subLine3.setLayout(new FlowLayout(FlowLayout.CENTER));

        subLine1.add(question1);
        subLine1.add(downloadSize);
        subLine1.add(byteTypeSize);
        subLine2.add(question2);
        subLine2.add(downloadSpeed);
        subLine2.add(byteTypeSpeed);
        subLine3.add(answerBox);
        subLine3.add(answerTimeUnit);
        subLine3.add(buttonCalc);

        mainframe.getContentPane().add(subLine1);
        mainframe.getContentPane().add(subLine2);
        mainframe.getContentPane().add(subLine3);

        mainframe.pack();
    }
}

UPDATE 2: It seems the graphics work fine as long as I don't click/change any of the combo-boxes. As soon as I click on any of the combo-boxes and then move the mouse outside the area of the combo-box I clicked, then it starts painting those weird glitched graphics. From the feedback I've received, it seems this problem might be platform-specific.

UPDATE 3: I thought I would share what I found in the end so others with similar problems can try it too: After reinstalling Java and rebooting, the problem seems to have gone away. Thanks to everyone who suggested this and all the other suggestions too!

Community
  • 1
  • 1
programmar
  • 670
  • 2
  • 8
  • 18
  • I don't think you should be using threads... – Wyatt Lowery Dec 16 '15 at 03:51
  • 5
    There are two main causes of this, one, you trying to heavy weight components in a light weight container or you've overridden `paint` or `paintComponent` somewhere without calling there `super` methods. Consider providing a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem. This is not a code dump, but an example of what you are doing which highlights the problem you are having. This will result in less confusion and better responses – MadProgrammer Dec 16 '15 at 03:52
  • 2
    Works on Window 7/Java 8 using both the System and Metal look and feels – MadProgrammer Dec 16 '15 at 03:54
  • @MadProgrammer Thank you for your insight, I am not modifying paint in any way. I've updated my question with a runnable example that also produces this problem. – programmar Dec 16 '15 at 04:18
  • Okay, I've re-run your example without issue – MadProgrammer Dec 16 '15 at 04:25
  • That's odd, on my end the problem happens right after I click on any of the combo boxes. Maybe it is a platform-specific problem. – programmar Dec 16 '15 at 04:30
  • 1
    I also have the same problem before with widows 7, then when I upgraded to when 10, problem solved, I think it is not the problem with code, I just shared my experienced here... – Bahramdun Adil Dec 16 '15 at 05:11
  • 1
    Could not reproduce your problem with win7 and java 1.8_65. Nothing wrong with your code. – ArcticLord Dec 16 '15 at 06:07
  • 1
    Works fine on Win7 and jdk 1.8.0_25. – user1803551 Dec 16 '15 at 06:14
  • 1
    Does it happen with other L&AFs as well? Maybe reinstall Java. – user1803551 Dec 16 '15 at 06:17
  • 1
    After seeing update 2 I have a proposition. override the `paintComponent`, call its `super` and add some prints after. Do this for the topmost panel and for the comboboxes. See if one of them isn't printing. – user1803551 Dec 16 '15 at 06:24
  • @user1803551 I just tried with 5x different L&F's, unfortunately they all produce the same problem. Checking now on their `paintComponent`. – programmar Dec 16 '15 at 06:28
  • @user1803551 I overrode `paint` for `JFrame` and `paintComponent` for the combo-boxes and `JPanel`. These methods are constantly being called every time I interact with those components so they seem to be working properly but the visual glitches still appear strangely. – programmar Dec 16 '15 at 07:02
  • Everything works fine, except for that calculate button! – Sher Alam Dec 16 '15 at 07:04
  • 1
    If you minimize and un-minimize the frame, does it fix the glitch (redraw) after it happens? Do all overriden methods print when you resize? – user1803551 Dec 16 '15 at 07:04
  • It does fix it like 95% of the times if I minimize and restore, but just hovering the mouse over will cause the glitch to appear after I got it glitched the first time. As for the other 5% of the times, after I restore it it will seem fine, but then after about a second and without me interacting with any component, it paints the glitches anyway. And yes, all overriden `paint` methods print, some as soon as I even hover over them. – programmar Dec 16 '15 at 07:10
  • 1
    Then you're out of luck in my book. Reinstall Java or find something related to the OS. This has to be low-level (well, lower than we can check here). You have my +1 and my blessings. – user1803551 Dec 16 '15 at 07:22

0 Answers0