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):
And here is what happens after a while of clicking one of the JComboBox'es a few times:
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!