How to change the order of tabs in JTabbedPane in Netbeans?

This should hopefully be a very easy question.

Within Netbeans, using the GUI Swing editor I have four panels within a JTabbedPane.

Tab #4 I want to sit where Tab #1 is now, but I created it last so by default its at the end of the list.

How to I move it in the GUI in Netbeans?

Google has not been my friend on this one. I keep coming up with questions about tab order, (e.g. Tabbing through forms), rather than what I actually want.

Right Click on the JTabbedPane->Change Order->You will get option to Move Up and Move Down which will change the order of tabs.

you can't explicitly change the sizes of those module icons in netbeans, but it can be done implicitly by increasing the size of the font used by the netbeans ide.

that font size is unrelated to the font size used for displaying your source code; instead it is used for rendering ide text such as labels on tabs ("files", "services", etc.), menu entries and file names within panels such as the projects tab. when the font size of a file name is changed, netbeans also makes a corresponding increase or decrease to the size of the icon (e.g. the module icon) to its immediate left.

to modify the font size used by netbeans specify the fontsize parameter when starting netbeans. for example, to get netbeans to use 18 point font (not recommended!) on windows 10 i made this call:

c:netbeans82_151binnetbeans64.exe --fontsize 18

the second screen shot below shows that using 18 point font also increased the size of the icons:

i deliberately used a significantly larger font to clarify the effect of the change in the screen shots, and the overall result was ugly. but for your purposes perhaps a smaller increase in font size will help you with the size of the icons.

really quick and dirty (definitely needs improvements and changes), but i would imagine that something like that could work for you (but is not a jtabbepane):

import java.awt.borderlayout;
import java.awt.component;
import java.awt.container;
import java.awt.dimension;
import java.awt.flowlayout;
import java.awt.insets;
import java.awt.event.actionevent;
import java.awt.event.actionlistener;
import java.util.arraylist;
import java.util.list;

import javax.swing.jbutton;
import javax.swing.jcomponent;
import javax.swing.jframe;
import javax.swing.jlabel;
import javax.swing.jpanel;
import javax.swing.jscrollpane;
import javax.swing.swingutilities;

public class test {

    /**
     * flowlayout subclass that fully supports wrapping of components.
     */
    public static class wraplayout extends flowlayout {
        private dimension preferredlayoutsize;

        /**
         * constructs a new <code>wraplayout</code> with a left alignment and a
         * default 5-unit horizontal and vertical gap.
         */
        public wraplayout() {
            super();
        }

        /**
         * constructs a new <code>flowlayout</code> with the specified alignment
         * and a default 5-unit horizontal and vertical gap. the value of the
         * alignment argument must be one of <code>wraplayout</code>,
         * <code>wraplayout</code>, or <code>wraplayout</code>.
         * 
         * @param align
         *            the alignment value
         */
        public wraplayout(int align) {
            super(align);
        }

        /**
         * creates a new flow layout manager with the indicated alignment and
         * the indicated horizontal and vertical gaps.
         * <p>
         * the value of the alignment argument must be one of
         * <code>wraplayout</code>, <code>wraplayout</code>, or
         * <code>wraplayout</code>.
         * 
         * @param align
         *            the alignment value
         * @param hgap
         *            the horizontal gap between components
         * @param vgap
         *            the vertical gap between components
         */
        public wraplayout(int align, int hgap, int vgap) {
            super(align, hgap, vgap);
        }

        /**
         * returns the preferred dimensions for this layout given the
         * <i>visible</i> components in the specified target container.
         * 
         * @param target
         *            the component which needs to be laid out
         * @return the preferred dimensions to lay out the subcomponents of the
         *         specified container
         */
        @override
        public dimension preferredlayoutsize(container target) {
            return layoutsize(target, true);
        }

        /**
         * returns the minimum dimensions needed to layout the <i>visible</i>
         * components contained in the specified target container.
         * 
         * @param target
         *            the component which needs to be laid out
         * @return the minimum dimensions to lay out the subcomponents of the
         *         specified container
         */
        @override
        public dimension minimumlayoutsize(container target) {
            dimension minimum = layoutsize(target, false);
            minimum.width -= gethgap() + 1;
            return minimum;
        }

        /**
         * returns the minimum or preferred dimension needed to layout the
         * target container.
         * 
         * @param target
         *            target to get layout size for
         * @param preferred
         *            should preferred size be calculated
         * @return the dimension to layout the target container
         */
        private dimension layoutsize(container target, boolean preferred) {
            synchronized (target.gettreelock()) {
                // each row must fit with the width allocated to the containter.
                // when the container width = 0, the preferred width of the
                // container
                // has not yet been calculated so lets ask for the maximum.

                int targetwidth = target.getsize().width;

                if (targetwidth == 0) {
                    targetwidth = integer.max_value;
                }

                int hgap = gethgap();
                int vgap = getvgap();
                insets insets = target.getinsets();
                int horizontalinsetsandgap = insets.left + insets.right + hgap * 2;
                int maxwidth = targetwidth - horizontalinsetsandgap;

                // fit components into the allowed width

                dimension dim = new dimension(0, 0);
                int rowwidth = 0;
                int rowheight = 0;

                int nmembers = target.getcomponentcount();

                for (int i = 0; i < nmembers; i++) {
                    component m = target.getcomponent(i);

                    if (m.isvisible()) {
                        dimension d = preferred ? m.getpreferredsize() : m.getminimumsize();

                        // can't add the component to current row. start a new
                        // row.

                        if (rowwidth + d.width > maxwidth) {
                            addrow(dim, rowwidth, rowheight);
                            rowwidth = 0;
                            rowheight = 0;
                        }

                        // add a horizontal gap for all components after the
                        // first

                        if (rowwidth != 0) {
                            rowwidth += hgap;
                        }

                        rowwidth += d.width;
                        rowheight = math.max(rowheight, d.height);
                    }
                }

                addrow(dim, rowwidth, rowheight);

                dim.width += horizontalinsetsandgap;
                dim.height += insets.top + insets.bottom + vgap * 2;

                // when using a scroll pane or the decoratedlookandfeel we need
                // to
                // make sure the preferred size is less than the size of the
                // target containter so shrinking the container size works
                // correctly. removing the horizontal gap is an easy way to do
                // this.

                container scrollpane = swingutilities.getancestorofclass(jscrollpane.class, target);

                if (scrollpane != null) {
                    dim.width -= hgap + 1;
                }

                return dim;
            }
        }

        /*
         *  a new row has been completed. use the dimensions of this row
         *  to update the preferred size for the container.
         *
         *  @param dim update the width and height when appropriate
         *  @param rowwidth the width of the row to add
         *  @param rowheight the height of the row to add
         */
        private void addrow(dimension dim, int rowwidth, int rowheight) {
            dim.width = math.max(dim.width, rowwidth);

            if (dim.height > 0) {
                dim.height += getvgap();
            }

            dim.height += rowheight;
        }
    }

    public static class mytabbedpane extends jpanel {
        private jpanel buttonpanel;
        private jpanel currentview;

        private tab currenttab;

        private class tab {
            string name;
            jcomponent component;
        }

        private list<tab> tabs = new arraylist<tab>();

        public mytabbedpane() {
            super(new borderlayout());
            buttonpanel = new jpanel(new wraplayout());
            currentview = new jpanel();
            add(buttonpanel, borderlayout.north);
            add(currentview);
        }

        public void addtab(string name, jcomponent tabview, int index) {
            if (index < 0 || index > tabs.size()) {
                throw new illegalargumentexception("index out of bounds");
            }
            final tab tab = new tab();
            tab.component = tabview;
            tab.name = name;
            tabs.add(index, tab);
            jbutton b = new jbutton(name);
            b.addactionlistener(new actionlistener() {

                @override
                public void actionperformed(actionevent e) {
                    setcurrenttab(tab);
                }

            });
            buttonpanel.add(b, index);
            buttonpanel.validate();
        }

        public void removetab(int i) {
            tab tab = tabs.remove(i);

            if (tab == currenttab) {
                if (tabs.size() > 0) {
                    if (i < tabs.size()) {
                        setcurrenttab(tabs.get(i));
                    } else {
                        setcurrenttab(tabs.get(i - 1));
                    }
                } else {
                    setcurrenttab(null);
                }
            }
            buttonpanel.remove(index);
        }

        void setcurrenttab(final tab tab) {
            if (currenttab == tab) {
                return;
            }
            if (currenttab != null) {
                currentview.remove(currenttab.component);
            }
            if (tab != null) {
                currentview.add(tab.component);
            }
            currenttab = tab;
            currentview.validate();
        }
    }

    public static void main(string[] args) {
        jframe frame = new jframe();
        mytabbedpane tabbedpane = new mytabbedpane();
        for (int i = 0; i < 100; i++) {
            tabbedpane.addtab("button " + (i + 1), new jlabel("dummy label " + (i + 1)), i);
        }
        frame.add(tabbedpane);
        frame.pack();
        frame.setsize(new dimension(1000, 800));
        frame.setdefaultcloseoperation(jframe.exit_on_close);
        frame.setvisible(true);
    }

}

the wraplayout was taken from another post on so.

alright...after hours of trying, i have realized that the tabitem selection behaviour is defined at the template level. so, if i wana change the backgrnd color, i do this:

<window.resources>
        <style targettype="{x:type tabitem}">
            <setter property="template">
                <setter.value>
                    <controltemplate targettype="{x:type tabitem}">
                        <border>
                            <grid>
                                <grid>
                                    <border x:name="border" 
                                            cornerradius="3,3,0,0"
                                            background="whitesmoke"/topic/>
                                </grid>
                                    <contentpresenter contentsource="header"
                                                      horizontalalignment="{templatebinding horizontalcontentalignment}"
                                                      verticalalignment="{templatebinding verticalcontentalignment}" />
                            </grid>
                        </border>
                        <controltemplate.triggers>
                            <trigger property="ismouseover"
                                     value="true">
                                <setter targetname="border"
                                        property="background"
                                        value="lightgray" />
                            </trigger>
                            <trigger property="isselected"
                                     value="true">
                                <setter targetname="border"
                                        property="background"
                                        value="lightgray" />
                            </trigger>
                        </controltemplate.triggers>
                    </controltemplate>
                </setter.value>
            </setter>
        </style>
    </window.resources>

i believe you need to call jtabbedpane.settabcomponentat (the javadoc for which can be found here)

this requires you be using java 6 (for java 5, you are going to have to look into writing a custom tabbedpaneui class, and overriding this -- or use a custom jtabbedpane class from some other source that allows this)

here's an example of it in action:

import groovy.swing.swingbuilder
import javax.swing.windowconstants as wc
import javax.swing.joptionpane
import javax.swing.jtabbedpane
import javax.swing.jscrollpane
import javax.swing.boxlayout as bxl
import java.awt.font

font font = new font("serif", font.bold, 13) 
int numpanels = 20

swing = new swingbuilder()

frame = swing.frame(title:'test', pack:true,   visible:true, defaultcloseoperation:wc.dispose_on_close) {
    vbox {
        tabbedpane(id: 'tabs', tablayoutpolicy:jtabbedpane.scroll_tab_layout) {
            panel( name:'tab 1', background:java.awt.color.white ) {
                vbox {
                    panel( background:java.awt.color.white ){
                        label ( 'label 1', preferredsize: [104, 24]).setfont(font) 
                        label ( 'label 2', preferredsize: [104, 24]).setfont(font) 
                        label ( 'label 3', preferredsize: [104, 24]).setfont(font) 
                    }   
                    scrollpane( verticalscrollbarpolicy:jscrollpane.vertical_scrollbar_always) {
                        vbox {
                            (1..numpanels).each { num ->
                                def panelid = "panel$num"
                                def pane = panel( alignmentx:0f, id:panelid, background:java.awt.color.green ) {
                                    label('description') 
                                    textfield( id: "description$num", text:panelid, columns: 70 )
                                    button( id: "buttonpanel$num", text:panelid, actionperformed:{
                                        swing."$panelid".background = java.awt.color.red
                                    } )
                                }
                            }
                        }
                    }
                }
            }       
            panel(name: 'tab 2', background:java.awt.color.white) {
                textfield(text: 'some text', columns: 15)
                scrollpane() {
                    textarea(text: 'some text', columns: 15, rows: 4)
                }
            }       
        }
        panel(id:'secondpanel', background:java.awt.color.white){                       
            button('quit', actionperformed:{
                dispose()
            })
        }   
    }
}

// define a list of labels for our tabs
def tabcomponents = [
  swing.label( text:'tab 1', font:font.derivefont( font.italic ) ),
  swing.label( text:'tab 2', font:font.derivefont( 20.0f ) )
]
// set the tab componets to our labels
tabcomponents.eachwithindex { lbl, idx ->
  swing.tabs.settabcomponentat idx, lbl
}

frame.size = [ frame.width, 600 ]

ps: you might want to remove your other question that i just found... generally editing a question to include extra informations is better than posting a new question asking the same thing but with more information


Tags: Netbeans Tabs Jtabbedpane