Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Visual artifacts with Java Swing #185

Open
NadChel opened this issue Apr 4, 2024 · 1 comment
Open

Visual artifacts with Java Swing #185

NadChel opened this issue Apr 4, 2024 · 1 comment

Comments

@NadChel
Copy link

NadChel commented Apr 4, 2024

I wrote a simple JSlider using javax.swing, but it's acting weirdly. Here's an MRE:

import javax.swing.SwingUtilities;

public class App {
	public static void main(String[] args) {
		SwingUtilities.invokeLater(() -> new MySlider().display());
	}
}
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;

public class MySlider {
	public static final int INITIAL_MEASUREMENT = 50;
	JFrame frame;

	MySlider() {
		this.frame = createFrame();
	}

	private JFrame createFrame() {
		JFrame newFrame = new JFrame("Slider");
		JPanel panel = createPanel();
		newFrame.add(panel);
		newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		newFrame.pack();
		newFrame.setLocationRelativeTo(null); // centers the frame
		return newFrame;
	}

	private JPanel createPanel() {
		JPanel panel = new JPanel();
		panel.setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 10));
		JLabel label = createLabel();
		JSlider slider = createSlider(label);
		panel.add(slider);
		panel.add(label);
		return panel;
	}

	private JLabel createLabel() {
		return new JLabel(getMeasurementText(INITIAL_MEASUREMENT));
	}

	private static String getMeasurementText(int measurement) {
		return "Measurement: %s".formatted(measurement);
	}

	private JSlider createSlider(JLabel label) {
		JSlider slider = new JSlider(0, 100, INITIAL_MEASUREMENT);
		slider.setPaintTicks(true);
		slider.setMinorTickSpacing(5);
		slider.setPaintTrack(true);
		slider.setMajorTickSpacing(25);
		slider.setPaintLabels(true);
		slider.setSnapToTicks(true);
		slider.addChangeListener(e -> {
			if (e.getSource() == slider) {
				int measurement = slider.getValue();
				label.setText(getMeasurementText(measurement));
			}
		});
		return slider;
	}

	public void display() {
		frame.setVisible(true);
	}
}

It works, but once I move the knob to the left, some unusual ticks appear on the bar itself and then disappear once I release the mouse button. I captured it on the screenshot below

2024-04-04_13-34-41

It's likely to be platform-dependent since it's not easily reproduced elsewhere so here is relevant info

  • Windows 10
  • Java Amazon Corretto 17
  • Intel(R) HD Graphics 620 25.20.100.6446

I post this issue to see whether it has to do with the driver or Corretto's compatibility issues with that driver

@mrserb
Copy link
Contributor

mrserb commented Apr 18, 2024

Thank you for your report. As a first step, I suggest updating corretto-17 to the latest version just released. If the error is still reproducible, you can check corretto-21 and corretto-22.

I cannot reproduce the problem on my system, so I need some additional information about your desktop: display resolution and scale factor.
Do any of these options solve the problem or change the behavior?

  • -Dsun.java2d.opengl=True
  • -Dsun.java2d.d3d=True
  • -Dsun.java2d.d3d=false
  • -Dsun.java2d.uiScale=1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants