JTabbedPane: Avoid automatic reordering tabs if they are folded / Nimbus

JTabbedPane is what I need for my purpose. I have very limited horizontal space, so my tabs stack up, which is great.

But the default behavior is that if the user clicks on the tab, * the tabs get re-sorted, so that the active Tab becomes the bottom-mos * t. What looks very intuitive and logical in theory is a nightmare in practical use because users lose track of β€œwhat was”. They simply confuse him, they tell me again and again.

I think it should be possible to redefine any user interface method to avoid this behavior (and it doesn't matter to me whether this is physically possible with paper cards :-).

Does anyone know where I need to do this? I am using Nimbus LAF, which does not seem to make it easier.

(I thought about using / cardLayout radio cleaners, but I need to put the user panel in the tab title, and the radio buttons can only contain a line or icon. Same thing for JToggleButton ...)

Any hints are greatly appreciated!

Thanks and Regards Philipp

+2
source share
3 answers

Ok, I found a problem. IN

package javax.swing.plaf.basic.BasicTabbedPaneUI; 

he says something like this

 // Rotate run array so that selected run is first if (shouldRotateTabRuns(tabPlacement)) { rotateTabRuns(tabPlacement, selectedRun); } 

Too bad there's apparently no simple set-a-flag-and-there-you-go-way method to change this.

Although you don't care if you omitted the call to rotateTabRuns(tabPlacement, selectedRun); or change shouldRotateTabRuns(tabPlacement) , for that matter ... however, for this you will have to redefine a whole bunch of classes ... depending on which cry you use.

Inherits this as

 Basic > Synth > Nimbus 

And at each level of L&F, there are several classes to configure ... I did not count.

Hope this helps !: D

Edit Oh yes ... @mkorbel already provided his solution with this affair, why not use it?

+2
source

sscce for potential respondents (s) for Nimbus L & f (using another L & f cannot reproduce this ridiculous question), if containers # Size of packed tabs in two or more Lines , enter image description hereenter image description here

as I know, there is only one possible solution (without overriding NimbusTabbedPaneUI) aephyr

from sscce

 import java.awt.BorderLayout; import javax.swing.*; public class TabbedPane { private static final long serialVersionUID = 1L; public TabbedPane() { JPanel jp = new JPanel(); jp.setLayout(new BorderLayout()); JTabbedPane tb = new JTabbedPane(); //tb.setUI(new CustomTabbedPaneUI()); tb.add("Tab1", new JTextArea(10, 20)); tb.add("Tab2", new JTextArea(10, 20)); tb.add("Tab3", new JTextArea(10, 20)); tb.add("Tab4", new JTextArea(10, 20)); tb.add("Tab5", new JTextArea(10, 20)); jp.add(tb, BorderLayout.CENTER); //add(jp, BorderLayout.CENTER); //tb.setEnabledAt(1, false); //tb.setEnabledAt(3, false); JFrame frame = new JFrame(); frame.setLayout(new BorderLayout()); frame.add(jp, BorderLayout.CENTER); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { try { for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (Exception system) { system.printStackTrace(); } SwingUtilities.invokeLater(new Runnable() { @Override public void run() { TabbedPane tP = new TabbedPane(); } }); } } 
+3
source

It's a bit of a hack, but you can override the default Nimbus values ​​to have a simple old Java style for tabbed panels, while preserving everything else. The simple Java style for a tabbed panel does not cause annoying reordering. Just save the default settings before setting the appearance and then set them.

 // get the defaults to keep HashMap<Object, Object> defaultsToKeep = new HashMap<>(); for (Map.Entry<Object, Object> entry : UIManager.getDefaults().entrySet()) { boolean isStringKey = entry.getKey().getClass() == String.class ; String key = isStringKey ? ((String) entry.getKey()):""; if (key.startsWith("TabbedPane")) { defaultsToKeep.put(entry.getKey(), entry.getValue()); } } // set nimbus look and feel for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { UIManager.setLookAndFeel(info.getClassName()); break; } } // set back your originals for (Map.Entry<Object, Object> entry : defaultsToKeep.entrySet()) { UIManager.getDefaults().put(entry.getKey(), entry.getValue()); } JFrame.setDefaultLookAndFeelDecorated(true); 
+1
source

Source: https://habr.com/ru/post/1381681/


All Articles