[Swing getting started tutorial] Step by Step netbeans (2): customizes the detachable jtabbedpane tag

Source: Internet
Author: User
Tags netbeans

We hope that the netbeansui. Java and netbeans. Java labels on the right can be closed last time. In this tutorial, we will discuss this issue. There are two ways to achieve this effect: one is to customize a set of jtabbedpane L & F; the other is to use the settabcomponentat (INT index, component componet) method of jtabbedpane through the custom component.

The first method may be a little troublesome for beginners. We mainly use the second method. Let's take a look at the effect:

The difference from tutorial 1 is that there are two small close buttons that can be closed. Now that we know that the settabcomponentat (INT index, component componet) method is used, let's customize the components and check the Code:

Package COM. monitor1394.netbeans. ui; <br/> Import Java. AWT. basicstroke; <br/> Import Java. AWT. color; <br/> Import Java. AWT. component; <br/> Import Java. AWT. dimension; <br/> Import Java. AWT. flowlayout; <br/> Import Java. AWT. graphics; <br/> Import Java. AWT. graphics2d; <br/> Import Java. AWT. event. actionevent; <br/> Import Java. AWT. event. actionlistener; <br/> Import Java. AWT. event. mouseadapter; <br/> Import Java. AWT. event. mouseevent; <br/> Import javax. swing. export actbutton; <br/> Import javax. swing. borderfactory; <br/> Import javax. swing. jbutton; <br/> Import javax. swing. jlabel; <br/> Import javax. swing. jpanel; <br/> Import javax. swing. jtabbedpane; <br/> Import javax. swing. plaf. basic. basicbuttonui; <br/>/** <br/> * The jtabbedpane tag that can be closed <br/> * @ author Monitor <br/> * created on, 23:52:41 <br/> */< Br/> public class tabbedpanel extends jpanel {<br/> private final jtabbedpane pane; <br/> Public tabbedpanel (final jtabbedpane pane) {<br/> super (New flowlayout (flowlayout. left, 0, 0); <br/> If (PANE = NULL) throw new nullpointerexception ("tabbedpane is null"); <br/> This. pane = pane; <br/> setopaque (false); <br/> // tab title <br/> jlabel label = new jlabel () {<br/> @ override <br/> Public String gettex T () {<br/> int I = pane. indexoftabcomponent (tabbedpanel. This); <br/> if (I! =-1) return pane. gettitleat (I); <br/> return NULL; <br/>}< br/>}; <br/> Add (Label); <br/> label. setborder (borderfactory. createemptyborder (0, 0, 0, 2); <br/> Add (New tabbutton (); <br/> setborder (borderfactory. createemptyborder (1, 0, 0, 0 )); <br/>}< br/>/** close button on the tab */<br/> private class tabbutton extends jbutton {<br/> Public tabbutton () {<br/> int size = 17; <br/> setpreferredsize (new dim Ension (size, size); <br/> settooltiptext ("close"); <br/> setui (New basicbuttonui (); <br/> setcontentareafilled (false ); <br/> setfocusable (false); <br/> setborder (borderfactory. createetchedborder (); <br/> setborderpainted (false); <br/> // flip effect <br/> setroloverenabled (true); <br/> // mouse event, draw a border when entering and cancel the border when removing the border <br/> addmouselistener (New mouseadapter () {<br/> @ override <br/> Public void mouseentered (mouseev Ent e) {<br/> component Component = E. getcomponent (); <br/> If (Component instanceof parameter actbutton) {<br/> specify actbutton button = (parameter actbutton) component; <br/> button. setborderpainted (true); <br/>}< br/> @ override <br/> Public void mouseexited (mouseevent E) {<br/> component Component = E. getcomponent (); <br/> If (Component instanceof parameter actbutton) {<br/> specify actbutton button = (Abstractbutton) component; <br/> button. setborderpainted (false); <br/>}< br/> }); <br/> // click the close button event <br/> addactionlistener (New actionlistener () {<br/> Public void actionreceivmed (actionevent EVT) {<br/> int I = pane. indexoftabcomponent (tabbedpanel. this); <br/> if (I! =-1) pane. remove (I); <br/>}< br/>}); <br/>}< br/> @ override <br/> Public void updateui () {<br/>}< br/> @ override <br/> protected void paintcomponent (Graphics g) {<br/> super. paintcomponent (g); <br/> graphics2d g2 = (graphics2d) g. create (); <br/> // offset of a coordinate point when the mouse is pressed <br/> If (GetModel (). ispressed () {<br/> g2.translate (1, 1); <br/>}< br/> g2.setstroke (New basicstroke (2 )); <br/> g2.setcolor (color. black); <br/> // red when the mouse is on the button <br/> If (GetModel (). isrolover () {<br/> g2.setcolor (color. red); <br/>}< br/> int Delta = 6; <br/> g2.drawline (delta, Delta, getwidth ()-Delta-1, getheight () -Delta-1); <br/> g2.drawline (getwidth ()-Delta-1, Delta, Delta, getheight ()-Delta-1 ); <br/> g2.dispose (); <br/>}< br/>

 

In the initmainpanel () method, you only need:

Worktabbedpane. Add ("netbeansui. Java", new jpanel (); <br/> worktabbedpane. Add ("netbeans. Java", new jpanel ());

Changed:

Worktabbedpane. add ("netbeansui. java ", new jpanel (); <br/> worktabbedpane. settabcomponentat (0, new tabbedpanel (worktabbedpane); <br/> worktabbedpane. add ("netbeans. java ", new jpanel (); <br/> worktabbedpane. settabcomponentat (1, new tabbedpanel (worktabbedpane ));

You can.

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.