I'm writing my personal look and feel and now I want to set my personal height to the tabs inside the JTabbledPane. I found this post for using the UIDefauls for setting the Insets, and it works well and this is the results
But I have noted a bug for the vertical tab, so this is the problem
And this is a minimal example reproducible
import javax.swing.*;
import javax.swing.plaf.basic.BasicTabbedPaneUI;
import javax.swing.plaf.metal.MetalLookAndFeel;
import javax.swing.plaf.metal.MetalTabbedPaneUI;
import java.awt.*;
/**
* @author https://github.com/vincenzopalazzo
*/
public class MaterialMain extends JFrame {
public static MaterialMain SINGLETON = new MaterialMain();
static {
try {
UIManager.setLookAndFeel(new MaterialMain.LookAndFeelTest());
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
}
public void init() {
JMenuBar menuBar = new JMenuBar();
JMenu file = new JMenu("File");
menuBar.add(file);
this.setJMenuBar(menuBar);
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.LEFT);
//JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.setUI(new LookAndFeelTest.TestTabbledPaneUI());
JPanel panel = new JPanel();
JPanel panelTwo = new JPanel();
panel.add(new JTextField("Hello guys, this is MaterialLookAndFeel"));
tabbedPane.add("Test", panel);
tabbedPane.add("TestTwo", panelTwo);
setTitle("Look and feel");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(630, 360);
add(tabbedPane);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
SINGLETON.init();
}
});
}
public static class LookAndFeelTest extends MetalLookAndFeel {
@Override
protected void initClassDefaults(UIDefaults table) {
super.initClassDefaults(table);
}
@Override
protected void initComponentDefaults(UIDefaults table) {
super.initComponentDefaults(table);
table.put( "TabbedPane.tabInsets", new Insets(10,10,10,10) );
table.put( "TabbedPane.selectedTabPadInsets", new Insets(10,10,10,10) );
table.put( "TabbedPane.linePositionY", 45);
table.put( "TabbedPane.linePositionX", 0);
table.put( "TabbedPane.lineWith", 0);
}
static class TestTabbledPaneUI extends MetalTabbedPaneUI {
@Override
protected void paintTabBackground(Graphics g, int tabPlacement, int tabIndex, int x, int y, int w, int h, boolean isSelected) {
g.setColor(isSelected ? lightHighlight : tabPane.getBackground());
g.fillRect(x, y, w, h);
if (isSelected) {
paintLine(g, x, y, w, h);
}else{
}
}
protected void paintLine(Graphics graphics, int x, int y, int w, int h) {
if(graphics == null){
return;
}
graphics.setColor(Color.RED);
graphics.fillRoundRect(x + UIManager.getInt("TabbedPane.linePositionX"),
y + UIManager.getInt("TabbedPane.linePositionY"),
w - UIManager.getInt("TabbedPane.lineWith"), 1, 10, 10);
}
@Override
protected LayoutManager createLayoutManager() {
return new TestTabbedPaneLayout();
}
protected class TestTabbedPaneLayout extends BasicTabbedPaneUI.TabbedPaneLayout{
protected int spacer; // should be non-negative
protected int indent;
public TestTabbedPaneLayout() {
this.spacer = UIManager.getInt("TabbedPane.spacer");
this.indent = UIManager.getInt("TabbedPane.indent");
}
@Override
protected void calculateTabRects(int tabPlacement, int tabCount){
if(spacer < 0){
throw new IllegalArgumentException("The spacer inside the " +
this.getClass().getSimpleName() + " must be a negative value");
}
super.calculateTabRects(tabPlacement,tabCount);
for (int i = 0; i < rects.length; i++){
rects[i].x += i * spacer + indent;
}
}
}
}
}
}
The my personal method for paint the line
protected void paintLine(Graphics graphics, int x, int y, int w, int h) {
if(graphics == null){
return;
}
graphics.setColor(Color.RED);
graphics.fillRoundRect(x + UIManager.getInt("TabbedPane.linePositionX"),
y + UIManager.getInt("TabbedPane.linePositionY"),
w - UIManager.getInt("TabbedPane.lineWith"), 1, 10, 10);
}
My personal layout
protected class TestTabbedPaneLayout extends BasicTabbedPaneUI.TabbedPaneLayout{
protected int spacer; // should be non-negative
protected int indent;
public TestTabbedPaneLayout() {
this.spacer = UIManager.getInt("TabbedPane.spacer");
this.indent = UIManager.getInt("TabbedPane.indent");
}
@Override
protected void calculateTabRects(int tabPlacement, int tabCount){
if(spacer < 0){
throw new IllegalArgumentException("The spacer inside the " +
this.getClass().getSimpleName() + " must be a negative value");
}
super.calculateTabRects(tabPlacement,tabCount);
for (int i = 0; i < rects.length; i++){
rects[i].x += i * spacer + indent;
}
}
}
I believe the bug is in the Layaut code but I can't find an intelligent way to do it, and with this I ask opinions from those who know more than me.