Tabs in JTabbedPane move by themselves on windows L&F

1 day ago 1
ARTICLE AD BOX

I have this simple java 8 program:

import java.awt.Dimension; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTabbedPane; import javax.swing.SwingUtilities; import javax.swing.UIManager; import javax.swing.WindowConstants; public class TestTabs { public static void main(String[] args) throws Exception { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); JFrame jfrey = new JFrame("Test"); JTabbedPane jt = new JTabbedPane(); jt.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT); for (int i = 0; i < 10; i++) { jt.addTab("Testing tabs" + i, new JPanel()); } jfrey.getContentPane().add(jt); jfrey.setMinimumSize(new Dimension(640, 480)); jfrey.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); jfrey.setLocationRelativeTo(null); SwingUtilities.invokeLater(() -> jfrey.setVisible(true)); } }

When you scroll all the way to the right tab, and then click between last and previous one, for some reason the tabs are getting "moved" to the right, bit by bit, which is really annoying:

How the tab movement looks

This seems to happen on windows L&F which is the system L&F for me, if i omit the UIManager call, they don't seem to move around. Is there a way around this behaviour in windows L&F?

EDIT:

Here's another version which makes sure each tab actually is added on the EDT:

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); SwingUtilities.invokeLater( () -> { JFrame jfrey = new JFrame("Test"); JTabbedPane jt = new JTabbedPane(); jt.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT); JPanel jeep = new JPanel(); JButton addTab = new JButton("Add new tab"); JButton removeTab = new JButton("Remove new tab"); addTab.addActionListener(al -> jt.addTab("Testing tabs" + jt.getTabCount(), new JPanel())); removeTab.addActionListener(al -> jt.removeTabAt(jt.getTabCount() - 1)); jeep.add(addTab); jeep.add(removeTab); jfrey.getContentPane().add(jeep, BorderLayout.NORTH); jfrey.getContentPane().add(jt); jfrey.setMinimumSize(new Dimension(640, 480)); jfrey.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); jfrey.setLocationRelativeTo(null); jfrey.setVisible(true); });

Here one can see that even after removing the last tab, the default L&F doesn't bug out the same way as the Windows one

Read Entire Article