swing-tab panel JTabbedPane Getting Started with

Source: Internet
Author: User

Note: Non-original, the content from theSwing tab panel , the author made a few changes.

The tab panel is a very common swing component, under Window, right-clicking on my Computer, viewing properties, is a typical elective card panel. And, of course, the most classic Google browser, which is also a typical tab. The tabs in swing are implemented using the JTabbedPane class, which describes the use of JTabbedPane:

1. Build a JTabbedPane object

JTabbedPane tab = new JTabbedPane ();

2. To add a tab, a tab is a compnent component, often a jpane panel to organize the required components, in fact, the idea of swing is like this, He divides components into two categories, one of which is a common component, a component that can hold components called a container, the outermost frame organizes the containers by layout, and each container organizes its own components by layout, so swing uses just the containers, components, Layout way to get it, hehe, here is a lot of, here is the method to add:

Tab.addtab (String title,component compnent);

Tab.addtab (String title,icon icon,component compnent);

Tab.addtab (String title,icon icon,component compnent,string tooltip);

Title is the caption of the tab, Compnent is of course the contents of thetab, icon is the ToolTip is a ToolTip . The AddTab method is added sequentially to the end of the tab set, and we know that the tab panel is actually a collection of tabs, with each tab counting from 0, which means that the first tab is numbered 0. So we can add a tab to any location in the tab set,

Tab.addtab (String title,icon icon,component compnent,string tooltip,int index);

Of course, you can also delete a tab based on the number,

Tab.removetabat (int index);

So many tabs, can only show one at a time, how to display the specified tab?

Tab.setselectedindex (int index);

If the tabs are too many, you can choose how they are displayed, hide or scroll

Tab.settablayoutpolicy (jtabbedpane.wrap_tab_layout);

Tab.settablayoutpolicy (jtabbedpane.scroll_tab_layout);

When you select a tab, how do you update the interface with the corresponding event? To add a changelistener to the panel, it has only one method called statechanged.

Here is the sample code:

/** Tabbedpanetest.java * @2015-06-02*/ Importjava.awt.BorderLayout;Importjava.awt.event.ActionEvent;ImportJava.awt.event.ActionListener;ImportJavax.swing.ButtonGroup;ImportJavax.swing.JFrame;ImportJavax.swing.JLabel;ImportJavax.swing.JPanel;ImportJavax.swing.JRadioButton;ImportJavax.swing.JTabbedPane;Importjavax.swing.event.ChangeEvent;ImportJavax.swing.event.ChangeListener;  Public classTabbedpanetest { Public Static voidMain (string[] args) {//TODO auto-generated Method StubTabbedpaneframe Frame=NewTabbedpaneframe (); Frame.settitle ("Tabbedpaneframe"); Frame.setsize (400, 300);       Frame.setdefaultcloseoperation (Jframe.exit_on_close); Frame.setvisible (true); } } classTabbedpaneframeextendsJFrame {PrivateJTabbedPane TabbedPane; Private intCount = 0;  PublicTabbedpaneframe () {//Add TabTabbedPane=NewJTabbedPane (); Tabbedpane.addtab ("Mercury",NULL); Tabbedpane.addtab ("Venus",NULL); Tabbedpane.addtab ("Earth",NULL); Tabbedpane.addtab ("Mars",NULL); Tabbedpane.addtab ("Jupiter",NULL); Tabbedpane.addtab ("Saturn",NULL); Tabbedpane.addtab ("Uranus",NULL); Tabbedpane.addtab ("Neptune",NULL); Tabbedpane.addtab ("Pluto",NULL); //Add a tab panelAdd (TabbedPane,"Center"); //Add ListenerTabbedpane.addchangelistener (NewChangeListener () {@Override Public voidstatechanged (ChangeEvent e) {//TODO auto-generated Method Stub              intn =Tabbedpane.getselectedindex ();           Loadtab (n);       }       }); Loadtab (0); //add radio buttons to adjust how tabs are laid outJPanel Buttonpanel=NewJPanel (); Buttongroup Buttongroup=NewButtongroup (); Jradiobutton Wrapbutton=NewJradiobutton ("Wrap tabs"); Wrapbutton.setselected (true); Wrapbutton.addactionlistener (NewActionListener () {@Override Public voidactionperformed (ActionEvent arg0) {//TODO auto-generated Method StubTabbedpane.settablayoutpolicy (jtabbedpane.wrap_tab_layout);       }       });       Buttongroup.add (Wrapbutton);        Buttonpanel.add (Wrapbutton); Jradiobutton Scrobutton=NewJradiobutton ("Scroll tabs"); Scrobutton.addactionlistener (NewActionListener () {@Override Public voidactionperformed (ActionEvent arg0) {//TODO auto-generated Method StubTabbedpane.settablayoutpolicy (jtabbedpane.scroll_tab_layout);       }       });       Buttongroup.add (Scrobutton);        Buttonpanel.add (Scrobutton);    Add (Buttonpanel, Borderlayout.south); }    Private voidLoadtab (intN) {String title=tabbedpane.gettitleat (n); String countstring= string.valueof (count + +); String msg= "This was" + title + ", load at" + Countstring + "Times"; Tabbedpane.setcomponentat (N,NewJLabel (msg)); }}

Run:

Figure 1 Wrap Mode effect

Figure 2 Scroll Mode effect

swing-tab panel JTabbedPane Getting Started with

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.