JTabbedPane Tab Widths Screwy on Wrap

I have a JTabbedPane that add tabs dynamically from queried database records. Everything works great until the tabs wrap. Then, I get two or three tabs on the top row which are VERY wide and the bottom row will have the remaining tabs (about 8 at this point) that are normal size. The labels for the tabs aren't that much different in width. The tabs on the top row just seem to pad out unproportionally.
I tried calling a custom TabbedPaneUI with a calculated width, and it still behaves the same. Has anyone run across this before? I'm not sure if it's something that can be managed with the UI, or if it's a layout issue. I'm loading JPanels with FlowLayout into the tabs and adding the tab pane to the top-level JFrame container. Thanks!

I have the tab placement user settable now. It can be switched to JTabbedPane.RIGHT within the app. Also have a setting to use the SCROLL_TAB_LAYOUT policy instead of wrap. Maybe that will suffice. Just really weird the way the tabs are expanding on the first row. Anyway, thanks for your reply, bbritta. Five duke ducats deposited to your account. The remaining five available for a solution.
To take a look at a jpeg of what I'm getting:
http://www.catpoll.com/tabpane.jpg
Cheers!!

Similar Messages

  • JtabbedPane equally size tab width..

    Hello,
    I have a JTabbedPane with about 7 tabs, but the tab names are mostly short and the sum width of the tabs takes up about 80% of the TabbedPane.
    I'd like for the tab widths to take up the entire 100% of the tabbedPane (just a pref.) It would be nice if I could set the min tab width to 1/7 tabbedPane width. How can I set the width of each tab??

    this might start you off - needs lots of testing
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    class Testing extends JFrame
      JTabbedPane tp = new JTabbedPane();
      int tabPaneWidth;
      int tabCounter;
      public Testing()
        setLocation(300,100);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        for(int x = 0; x < 3; x++)
          JPanel p = new JPanel();
          p.setPreferredSize(new Dimension(600,400));
          tp.addTab(""+(++tabCounter),p);
        JButton btn = new JButton("Add Tab");
        JPanel p = new JPanel();
        p.add(btn);
        getContentPane().add(tp,BorderLayout.CENTER);
        getContentPane().add(p,BorderLayout.SOUTH);
        tabPaneWidth = tp.getPreferredSize().width;
        tp.setUI(new MyUI());
        setResizable(false);//otherwise needs a componentListener
        pack();
        btn.addActionListener(new ActionListener(){
          public void actionPerformed(ActionEvent ae){
            tp.addTab(""+(++tabCounter),new JPanel());}});
      class MyUI extends javax.swing.plaf.basic.BasicTabbedPaneUI
        protected int calculateTabWidth(int tabPlacement,int tabIndex, FontMetrics metrics)
          Insets margin = getInsets();
          return (int)((tabPaneWidth - margin.left - margin.right)/tp.getTabCount());
      public static void main(String[] args){new Testing().setVisible(true);}
    }

  • JTabbedPane: Tab Text, Tab Width and Tab Height

    Whenever i put text on JTabbedPane tab it displays it in one line, so when i have a text e.g. "XXXXXXXXX XXXXXXXXX"
    it displays it as it is not like XXXXXXXXX
    XXXXXXXXX
    How can I display it in two line or more?
    Does Tab width and height automatically adjust itself relative to the text inside it? If not how can I set the tab width and height?
    Thanks in advance.

    leemax quote:
    Simply add the escaped new line character "\n" to display the text of the tab on the next line.
    --Yeah i tried that already but anyway i solved the problem by making used of htnl tags "<br>"                                                                                                                                                                                                                                                                                                                                                                                                                               

  • Setting tab width in JTabbedPane....

    hi all,
    I want to set the tab width to a constant value for all tabs in tabbed pane. How can i do this?
    Actually i have a tabbed pane where it has different length of strring to display in each tab. To make it uniform i want to set a constant width to the tab.
    please help...
    -Soni

    hi all
    i got it by overridding BasicTabbedPaneUI .
    class TabbedPaneUI extends BasicTabbedPaneUI {
    protected int calculateTabWidth(int tabPlacement, int tabIndex,
                                  FontMetrics metrics) {
              return TAB_TEXT_WIDTH;
    Now my problem is,
    all text in tab are center aligned.
    how to make it left aligned?
    please help....
    -Soni

  • Title length in JTabbedPane tabs

    Has anyone ever written code to make JTabbedPane tab titles shorten automatically when there isn't room for all of them in one row?
    Our users like neither of the many-tabs solutions that Swing provides (scroll and multiple rows of tabs), and would prefer a style similar to Eclipse where the tab titles shorten if there isn't room for the entire text.
    All I really want to know is - did anyone do it? And if so a few hints as to how you achieved it.
    Thanks,
    Tim

    Well here is the code I developed in case it is useful to anyone else. Didn't have to do anything clever like work out the length of the text in the end - as you will see in the code:     import java.awt.BorderLayout;
         import java.awt.event.ActionEvent;
         import java.awt.event.ActionListener;
         import java.awt.event.ContainerEvent;
         import java.awt.event.ContainerListener;
         import java.util.Random;
         import javax.swing.JButton;
         import javax.swing.JFrame;
         import javax.swing.JPanel;
         import javax.swing.JTabbedPane;
         import nz.co.timryan.components.CentredButtonPanel;
         import nz.co.timryan.components.FixedWidthButton;
          * Automatically shorten the tab selector titles if necessary to fit them
          * into the width of the space available without scrolling.
          * <br><br>Created on 10/07/2005 by Tim Ryan
         public class BTTTabbedPane extends JTabbedPane implements ContainerListener {
              static BTTTabbedPane instance;
              BTTTabbedPane() {
                   setTabPlacement(JTabbedPane.BOTTOM);
                   setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
                   addContainerListener(this);
                   instance = this;
               * @param args
              public static void main(String[] args) {
                   JFrame frame = new JFrame("TabbedPane test");
                   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                   JPanel main = new JPanel(new BorderLayout());
                   main.add(new BTTTabbedPane(), BorderLayout.CENTER);
                   JPanel buttonPanel = new CentredButtonPanel();
                   JButton addButton = new FixedWidthButton("Add");
                   addButton.addActionListener(new ActionListener() {
                        Random random = new Random();
                        public void actionPerformed(ActionEvent e) {
                             String text = "MARY AND JANE SMITH AND MICHAEL".substring(0, random.nextInt(30) + 1);
                             instance.addTab(text, null, new JPanel(), text);
                   JButton removeButton = new FixedWidthButton("Remove");
                   removeButton.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                             instance.remove(instance.getTabCount() - 1);
                   buttonPanel.add(addButton);
                   buttonPanel.add(removeButton);
                   main.add(buttonPanel, BorderLayout.SOUTH);
                   frame.add(main);
                   frame.setBounds(100, 100, 800, 500);
                   frame.setVisible(true);
              public void componentAdded(ContainerEvent e) {
                   recalculateTitleWidths();
              public void componentRemoved(ContainerEvent e) {
                   recalculateTitleWidths();
               * Shorten the titles of the tabs as necessary to fit the selectors across
               * the width of the available space without scrolling (if possible).
               * Whenever a title is shortened, "..." is appended to the text to show that
               * this has occurred.
               * The full length titles are held in the tooltip text so that the text can
               * be restored if other tabs are deleted, and also so that the user can see
               * the full text by mousing over the title.
              private void recalculateTitleWidths() {
                   int tabCount = getTabCount();
                   int[] tabWidth = new int[tabCount];
                   int availableWidth = getBounds().width - 4;
                   //  Reset the titles to their full text
                   for (int j = 0; j < tabCount; j++) {
                        setTitleAt(j, getToolTipTextAt(j));
                        tabWidth[j] = getBoundsAt(j).width;
                   int totalWidth = getTotalWidth(tabWidth, tabCount);
                   boolean wasShortened = true;
                   // Loop around, shortening titles until all the selectors fit across
                   // the screen, or the titles are too short to be shortened further.
                   while ((totalWidth > availableWidth) && wasShortened) {
                        int longestTab = -1;
                        int longestTabWidth = 0;
                        int longestText = 0;
                        wasShortened = false;
                        // Find the longest tab
                        for (int j = 0; j < tabCount; j++) {
                             if (tabWidth[j] >= longestTabWidth) {
                                  longestTab = j;
                                  longestTabWidth = tabWidth[j];
                                  longestText = getTitleAt(j).length();
                        // Shorten its text (adding "..." if not already done)
                        // and then determine the new tab width and total tab width
                        String title = getTitleAt(longestTab);
                        if (title.length() > 4) {
                             if (title.endsWith("...")) {
                                  title = title.substring(0, title.length() - 4);
                             } else {
                                  title = title.substring(0, title.length() - 2);
                             setTitleAt(longestTab, title + "...");
                             tabWidth[longestTab] = getBoundsAt(longestTab).width;
                             wasShortened = true;
                             totalWidth = getTotalWidth(tabWidth, tabCount);
               * Calculate the total width of the tab selectors.
               * @param  tabWidth the individual widths of the tab selectors
               *         (held in an array for performance reasons)
               * @param  tabCount the count of tabs
               * @return the total width of all tab selectors
              private int getTotalWidth(int[] tabWidth, int tabCount) {
                   int totalWidth = 0;
                   for (int j = 0; j < tabCount; j++) {
                        totalWidth += tabWidth[j];
                   return totalWidth;
    }Regards,
    Tim

  • The Custom Tab Width add-on doesn't work in Firefox 25

    I use the add-on 'Custom Tab Width' (https://addons.mozilla.org/en-US/firefox/addon/custom-tab-width) so that my tabs can shrink infinitely, but after updating to Firefox 25, it no longer seems to work. Is this add-on incompatible with Firefox 25? In that case, is there any other way to make the tabs shrink infinitely?

    Hi GAPa,
    Thanks for visiting Mozilla Support!
    This addon is indeed compatible with Fx25 as I have tested it. Try this. Press '''CTRL + SHIFT +A''' and click on '''Extensions''', then go into Options on '''Custom Tab Width''' then input '''1''' as MIN. Does that help?
    If not, feel free to reply and we will try other things to get this resolved.

  • Setting tab width for Text editor

    Simple ? I was unable to see this in the tool help. How do we set the tab width in the Text Editor for solaris.

    Hi,
    Please see the man page of xview.
    The parameter: text.tabWidth in the .Xdefaults file controls the tab width size.

  • Make firefox tabs auto-size similar to chrome rather than having the tab navigator arrows when the tab width is full???

    I want the tabs to auto-size/shrink when the tab bar width is full as opposed to adding the annoying extra step of navigation arrows...
    any suggestions?

    Suggestion:<br /> https://addons.mozilla.org/en-US/firefox/addon/custom-tab-width/

  • Is there a way to set min and max tab widths in firefox 17 or 18?

    The browser tab width maximum and minimum options are no longer in about:config and the "tab width clip" has no effect on tab width in firefox 17 or 18.

    hello, the browser.tabs.tabClipWidth setting does only specify beyond which width a close button is shown on the tab. in order to set a custom tab size you can use this extension: https://addons.mozilla.org/firefox/addon/custom-tab-width/

  • Change code tab width preference

    I am using CF Builder 3, and for the life of me I can't find how to change the tab width in the code formatter. It is defaulted to 4 spaces wide, but I prefer 2 spaces. And I use tabs, not spaces, for indentation. In the preferences I found 2 or 3 different places where I could change the tab display width, but none of those changes fixed the displayed tab width in .cfms and .cfcs. Am I missing something, or is this a bug?

    Yup, I'm seeing the same (mis)behavior.  Anit Kumar Panda can you reopen that bug please?  I added a comment summarizing the findings of Jake and myself, and provided a link to this forum thread.
    Thanks,
    -Carl V.

  • Variable tab width

    The tabs on tabbed forms currently are spaced according to the longest text on any of the tabs of the form. For example, if there are two tabs with labels "tab1" and "taaaaaaaaaaaaaaaaaaaaaaaab2", both tabs will be wide.
    Can anyone tell me how to control the tab widths of each tab so that they are not wider than necessary?
    Thank you very much for your suggestion.

    Sorry, you have no chance to control the tab width.
    The only way would be to create a tab yourself using stacked canvases and buttons instead of a tab canvas.

  • Document Tabs: Set Value and Tab Width

    1. Would like to be able to assign a tab name (value) to any document, at least for the current session and preferably as a permanent document property.
    2. Would like to be able to set min/max tab widths and L/R margins (white space). Setting could be in pixels or characters, either would be fine. Probably a product-level preference, though could be implemented differently.
    Thank you kindly,
    Elchanan

    Hi Shaji,
    You can do this in IRPT in the following way.
    <!DOCTYPE HTML>
    <HTML>
    <HEAD>
        <TITLE>Your Title Here</TITLE>
        <META http-equiv="X-UA-Compatible" content="IE=edge">
        <META http-equiv='cache-control' content='no-cache'>
        <META http-equiv='expires' content='0'>
        <META http-equiv='pragma' content='no-cache'>
        <SCRIPT type="text/javascript" src="/XMII/JavaScript/bootstrap.js" data-libs="i5Chart,i5Grid,i5SPCChart"></SCRIPT>
        <SCRIPT>
            var Chart = new com.sap.xmii.chart.hchart.i5Chart("Regression_15.0/i5Chart_Employees", "Regression_15.0/Employees_Hiked");
            Chart.setChartWidth("640px");
            Chart.setChartHeight("400px");
            Chart.draw("div1");
        function setValueColumns()
       Chart.getChartObject().setValueColumns("ESAL,ENEWSAL");
        Chart.refresh();
        function setLabelColumns()
        Chart.getChartObject().setLabelColumns("EID");
        Chart.refresh();
        </SCRIPT>
    </HEAD>
    <BODY>
        <DIV id="div1"></DIV>
    <INPUT type="button" value="setValueColumns" onClick="setValueColumns()"/>
    <INPUT type="button" value="setLabelColumns" onClick="setLabelColumns()"/>
    </BODY>
    </HTML>
    Output :
    After clicking on setValueColumns
    After clicking on setLabelColumns.
    hope this helps.
    Regards,
    Sriram

  • Dynamic Tab Width ?

    Does anyone know how to make the tabwidth automatically adjust depending on the size of the browser?  (Just like this site http://www.picnik.com/app)
    thanks for your help

    I observed picnik.com ,th website is having flex/flash component which  uses 100% of width of browser .so in order to dynamically changing the width of Tab you need to capture resize (mx.events.ResizeEvent)  event of application component  (mx.core.Application)  and inside that method change the tab width accordingly .
    If this post help you to resolve your question .Please mark as "correct" or "helpful"
    Thanks
    Rushit Patel

  • How to make JTabbedPane Title component take up entire tab width?

    Starting with JDK 1.6, JTabbedPane has been enhanced to allow you to specify an arbitrary Component for the title of a tab (I think before, you could only specify a JLabel and/or icon). We make use of this in our application by specifying a Panel with a GridBagLayout and three children: an Icon (on the left), a label (middle), and a close button (right). the middle component, the label, gets all the weightx and has fill set to HORIZONTAL. Everything looks good - as long as the tabs stay on a single row. When another tab is added and the TabbedPane extends to a second row, things don't look: the tabs are allocated alot more white space now - but the Component isn't being given that extra space! Consequently, the icons on the left and right of my label stay next to the label, instead of staying next to the edges of the tab!
    Is this a bug in the JDK?
    If it's not a bug, how do I make use of all available tab space?

    Hi Darryl,
    Thanks for the suggestion. Below is a primitive standalone example. It brings up an empty frame and you can click on the "Add Tab" button to add a tab. When you do, you'll see that the tab's "title" area consists of an "i" button, a label, and a "x" button. The tab itself isn't much bigger than those three components, so things look ok. Now, add two more tabs....the 2nd tab still fits on the first row of the TabbedPane, so things still look ok. When you add the third tab, the first tab gets placed on a new 2nd row of the TabbedPane and the tab takes up the whole width of the TabbedPane....but the "i" and the "x" buttons are still scrunched up next to the label - rather than near the edges of the tab, where they would be expected.
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    public class TabExample extends javax.swing.JFrame {
        public TabExample() {
            initComponents();
         class TabRenderer extends JPanel {
              private JButton infoButton;
              private JLabel label;
              private JButton closeButton;
              public TabRenderer(String title) {
                   infoButton = new JButton("i");
                   label = new JLabel(title);
                   closeButton = new JButton("x");
                   setLayout(new GridBagLayout());
                   GridBagConstraints gc = new GridBagConstraints();
                   gc.gridy = 0;
                   gc.gridx = 0;
                   gc.anchor = GridBagConstraints.WEST;
                   gc.fill = GridBagConstraints.NONE;
                   gc.weightx = 0;
                   this.add(infoButton, gc);
                   gc.gridx = 1;
                   gc.anchor = GridBagConstraints.WEST;
                   gc.fill = GridBagConstraints.HORIZONTAL;
                   gc.weightx = 1.0;
                   this.add(label, gc);
                   gc.gridx = 2;
                   gc.anchor = GridBagConstraints.EAST;
                   gc.fill = GridBagConstraints.NONE;
                   gc.weightx = 0;
                   this.add(closeButton, gc);
        private void initComponents() {
            tabbedPane = new javax.swing.JTabbedPane();
            jButton1 = new javax.swing.JButton();
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            getContentPane().add(tabbedPane, java.awt.BorderLayout.CENTER);
            jButton1.setText("Add Tab");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    addTabHandler(evt);
            getContentPane().add(jButton1, java.awt.BorderLayout.SOUTH);
            pack();
         private void addTabHandler(java.awt.event.ActionEvent evt) {
              int index = tabbedPane.getTabCount();
              tabbedPane.add(new JPanel(), index);
              tabbedPane.setTabComponentAt(index, new TabRenderer("Tab " + index));
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    JFrame f = new TabExample();
                        f.setSize(400, 300);
                        f.setVisible(true);
        private javax.swing.JButton jButton1;
        private javax.swing.JTabbedPane tabbedPane;
    }

  • Prob with fixed column widths and word wrapping in viewer

    i have a report where each record has a very long paragraph of text in it.
    In desktop and plus, the text column is at a fixed width and the text just wraps nicely according to the column width.
    But in Viewer, the column widths do not stay fixed, the text does not wrap, and the long text is displayed in a single line that completely stretches the column width off the page and requires a horizontal scroll bar.
    is there anyway to fix the column width in viewer, have the text naturally wrap, and prevent the column stretching. All word wrapping settings are on, but seem to have no effect. I can't seem to find any solution to this.
    Thanks...

    Hi Pritam,
    Per my understanding that you can't see the vertical scrollbar of the ReportViewer controls 2012 to scroll for the grid rows, but can see the vertical scrollbar of the web application, you also can't fix the headers while scrolling, right?
    I have tested on my local environment and can't reproduce your issue, but you have an alternative way to add some css  to the web form's source code to display the vertical scrollbar.
    Details information below for your reference:
    Please check below properties setting of the reportviewer which control the visibility of the scrollbar:
    AsyncRendering="true"
    SizeToReportContent="false"
    Please check if this problem also occur on other version of IE and other type of browser.
    Please check if you have done correct setting of the Fix data to freeze the table header as the step of below:
    http://technet.microsoft.com/en-us/library/bb934257(v=sql.100).aspx
    If step1 doesn't work, please click the source of webform.aspx and add below CSS to add the vertical scrollbar manually:
    #ReportViewer1 {
              overflow-y: scroll;
    Run the application you will see it display as below:
    Similar thread for your reference:
    https://social.msdn.microsoft.com/forums/sqlserver/en-US/f96b3b56-e920-411b-82ea-40467c922e66/reportviewer-control-vertical-scroll-bars
    If you still have any problem, please feel free to ask.
    Regards
    Vicky Liu

Maybe you are looking for