get JTabbedPane Tab Area Bounds - Java Data Structure

Java examples for Data Structure:DNA

Description

get JTabbedPane Tab Area Bounds

Demo Code


//package com.java2s;
import javax.swing.*;
import java.awt.*;

public class Main {
    static Rectangle getTabAreaBounds(JTabbedPane tabbedPane) {
        Rectangle tabbedRect = tabbedPane.getBounds();
        //pointed out by daryl. NullPointerException: i.e. addTab("Tab",null)
        //Rectangle compRect   = getSelectedComponent().getBounds();
        Component comp = tabbedPane.getSelectedComponent();
        int idx = 0;
        while (comp == null && idx < tabbedPane.getTabCount()) {
            comp = tabbedPane.getComponentAt(idx++);
        }//from   ww  w. j  a  v a  2  s.com
        Rectangle compRect = (comp == null) ? new Rectangle() : comp
                .getBounds();
        int tabPlacement = tabbedPane.getTabPlacement();
        if (tabPlacement == SwingConstants.TOP) {
            tabbedRect.height = tabbedRect.height - compRect.height;
        } else if (tabPlacement == SwingConstants.BOTTOM) {
            tabbedRect.y = tabbedRect.y + compRect.y + compRect.height;
            tabbedRect.height = tabbedRect.height - compRect.height;
        } else if (tabPlacement == SwingConstants.LEFT) {
            tabbedRect.width = tabbedRect.width - compRect.width;
        } else if (tabPlacement == SwingConstants.RIGHT) {
            tabbedRect.x = tabbedRect.x + compRect.x + compRect.width;
            tabbedRect.width = tabbedRect.width - compRect.width;
        }
        tabbedRect.grow(2, 2);
        return tabbedRect;
    }
}

Related Tutorials