Closed IanKrL closed 3 months ago
I am curious about your use case, are you trying to create a TabbedPane that supports 'clearing' of the selection? (no tabs selected and empty content).
According to the documentation there will always be a selected tab: https://docs.oracle.com/javase/8/docs/api/javax/swing/JTabbedPane.html
If the tab count is greater than 0, then there will always be a selected index, which by default will be initialized to the first tab.
You may be right, I didn't catch that line. I'll double check my real-life case and get back to you.
fixed in latest 3.5.1-SNAPSHOT
: https://github.com/JFormDesigner/FlatLaf#snapshots
I looked more closely at my real-life case and what we were doing was actually more like this:
package stuffs;
import java.net.UnknownHostException;
import javax.swing.DefaultSingleSelectionModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SingleSelectionModel;
import javax.swing.SwingUtilities;
import com.formdev.flatlaf.FlatClientProperties;
import com.formdev.flatlaf.FlatLightLaf;
public class SimpleLaf {
private static final String TEXT = "THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG";
public static void main(String[] args) throws UnknownHostException {
SwingUtilities.invokeLater(() -> {
JFrame.setDefaultLookAndFeelDecorated(true);
FlatLightLaf.setup();
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Some Title");
frame.setSize(350, 100);
final JPanel panel = new JPanel();
JTabbedPane pane = new JTabbedPane();
pane.putClientProperty(FlatClientProperties.TABBED_PANE_TAB_TYPE,
FlatClientProperties.TABBED_PANE_TAB_TYPE_CARD);
SingleSelectionModel model = new DefaultSingleSelectionModel();
pane.setModel(model);
pane.add(new JLabel(TEXT));
panel.add(pane);
frame.add(panel);
// ***** This call here *****
pane.setSelectedIndex(-1);
frame.setVisible(true);
});
}
}
That is, manually setting the selected index to -1. This value is set during a tab-collapse animation to indicate a changing state. Ideally I imagine I'd rewrite to not set the unexpected -1 while a tab is present, but it's pretty thorny to untangle the logic and probably not worth it at this point.
I did come up with a workaround to make flatlaf 3.3 happy (sorry I forgot to report the version in my original description) until I can upgrade to 3.5.1+. I overrode getSelectedIndex() in my subclass of JTabbedPane and just don't allow it to return a negative number when there are tabs. That allows my weird animation code to function, but looks kosher to the outside world.
Thanks for fixing, DevCharly.
When a TabbedPane returns a selected index of -1, it causes a ArrayIndexOutOfBoundsException.
Linux: AlmaLinux 8.10 Gnome: Version 3.32.2
Just launch this and the window displays, but also throws this exception several times:
In FlatTabbedPaneUI.paint:
Where getSelectedIndex() returns -1 this leads to the index exception, but from the SingleSelectionModel Javadoc:
So this is a valid value for this method to return, but it is not properly handled by FlatTabbedPaneUI or BasicTabbedPaneUI.