Java-swing: Improvement 2 for transparent jtable generation

Source: Internet
Author: User

This time, the split line is added to the header that cannot see the split line. In fact, it is very simple. The header is essentially a jtable, and it also needs to use the Renderer. Then add a custom Renderer to it and manually draw the split line in this Renderer. :

The Code is as follows:

Package UI. testworker; import Java. AWT. borderlayout; import Java. AWT. color; import Java. AWT. component; import Java. AWT. dimension; import Java. AWT. gradientpaint; import Java. AWT. graphics; import Java. AWT. graphics2d; import Java. AWT. rectangle; import Java. AWT. toolkit; import Java. AWT. event. windowadapter; import Java. AWT. event. using wevent; import javax. swing. jframe; import javax. swing. jlabel; import javax. swing. JPA Nel; import javax. swing. jscrollpane; import javax. swing. jtable; import javax. swing. listselectionmodel; import javax. swing. uimanager; import javax. swing. table. defaulttablecellrenderer; import javax. swing. table. jtableheader; import javax. swing. table. tablecellrenderer; public class simple extends jframe {Private Static final long serialversionuid = 1l; Private Static final int min_width = 1270; Private Static F Inal int min_height = 700; private color color1 = new color (225,237,233); Private color color2 = new color (76,124,206); Private jtable filetable; public simple () {// set to Windows style try {string windows = "com. sun. java. swing. plaf. windows. windowslookandfeel "; uimanager. setlookandfeel (Windows);} catch (exception e) {} addwindowlistener (New windowadapter () {public void windowclosing (windowevent e) {system. exit (0 ); });/** Generate a panel, then, draw the background of the panel into the specified color * and use the setcontentpane method to set the panel of the window to this Panel */jpanel JP = new jpanel () {Private Static final long serialversionuid = 1l; protected void paintcomponent (Graphics g) {// use graphics2d to draw a gradient color graphics2d g2d = (graphics2d) g; g2d. setpaint (New gradientpaint (0, 0, color1, this. getwidth (), this. getheight (), color2); g2d. fill (New rectangle (0, 0, this. getwidth (), this. getheight ()) ;}}; This. setcontentpane (JP); this. setlayout (New borderlayout (); inittable ();/** set jscrollpane to transparent here. * Jscrollpane consists of several parts: one is itself, the other is the viewport in the middle, and the headerview in the header, * rowview on the left, and scroll bars on the right and below. * For a jscrollpane with a default jtable added, it includes at least the edge of jscrollpane, the header title, And the jtable in the middle * If only jscrollpane is set to transparent, the edge is only transparent, the viewport in the middle (where the table is located) and the header are still not transparent * Therefore, you must set them to transparent. However, note that you must manually add the jtable header to the header first, then retrieve the header and set it to transparent * otherwise, a null pointer error */jscrollpane scrollpane = new jscrollpane (); scrollpane will be reported. getviewport (). setopaque (false); // set jscrollpane to transparent scrollpane. setopaque (false); // set the viewport in the middle to transparent scrollpane. setviewportview (filetable); // load table SCR Ollpane. setcolumnheaderview (filetable. gettableheader (); // sets the header (headerview) scrollpane. getcolumnheader (). setopaque (false); // retrieve the header and set it to transparent getcontentpane (). add (scrollpane, borderlayout. center); toolkit TK = This. gettoolkit (); dimension dm = TK. getscreensize (); // This. setbounds (DM. width-min_width)/2, (DM. height-min_height)/2, min_width, min_height); this. setvisible (true);} private void in Ittable () {// initialize tablestring [] columnname = new string [] {"file name", "size", "user", "Upload time "}; object [] [] columnvalues = new object [] [] {"Chu Liuxiang Legend", "232134 kb", "fykhlp", "19:36:21 "}, {"legend of Chu Liuxiang", "232134 kb", "fykhlp", "19:36:21" },{ "legend of Chu Liuxiang", "232134 kb", "fykhlp ", "19:36:21" },{ "Chu Liuxiang Legend", "232134 kb", "fykhlp", "19:36:21" },{ "Chu Liuxiang Legend", "232134 kb ", "fykhlp", "19:36:21" }}; filetable = ne W jtable (columnvalues, columnname); filetable. setrowheight (25); filetable. setautoresizemode (jtable. auto_resize_next_column); filetable. setselectionmode (listselectionmodel. single_selection); filetable. setintercellspacing (new dimension (0, 0);/** set the table to transparent, the table also contains the table itself and its content items * It is useless to set the table itself as transparent, you should also set the content items as transparent * content items to implement */filetable by setting the transparency of the Renderer. setopaque (false); defaulttablecellrenderer render = new def Aulttablecellrenderer (); render. setopaque (false); // set the Renderer to transparent. // set this Renderer to filetable. // This setting is effective without other special column settings. // If you specify a Renderer for a column, it will not call the render Renderer // so to ensure transparency, if you specify an extra Renderer for column, you should also set the transparent filetable in the additional Renderer. setdefaultrenderer (object. class, render); // sets the display range dimension viewsize = new dimension (); viewsize. width = filetable. getcolumnmodel (). gettotalcolumnwidth (); viewsize. height = 10 * filetable. getrowheight (); filetable. setpreferredscrollableviewportsize (viewsize); // you can specify whether the header is transparent. // The header is actually a jtable with only one row. Jtableheader header = filetable. gettableheader (); // get the header. setpreferredsize (new dimension (30, 26); header. setopaque (false); // set the header to a transparent header. gettable (). setopaque (false); // set table transparency in the header. the table in the header is also set as in the previous table, you also need to set the unit items to transparent * So you also need to set the transparency of the unit items in the header. The Renderer is used here. * The problem here is that if you set the header Renderer directly as above, there is no horizontal line below it * therefore, we need a dedicated head Renderer to manually draw a horizontal line */header. setdefaultrenderer (New headercellrenderer (); tablecellrenderer headerrenderer = header. getdefaultrenderer (); If (headerrenderer instanceof jlabel) {(jlabel) headerrenderer ). sethorizontalalignment (jlabel. center); (jlabel) headerrenderer ). setopaque (false) ;}}/*** header Renderer * @ author administrator **/class headercellrenderer Extends defaulttablecellrenderer {Private Static final long serialversionuid = 1l; public component gettablecellrenderercomponent (jtable table, object value, Boolean isselected, Boolean hasfocus, int row, int column) {jlabel label = new jlabel () {Private Static final long serialversionuid = 1l; protected void paintcomponent (Graphics g) {// reload the paintcomponent method of jlabel, manually draw graphics2d g2d = (G Raphics2d) g; g2d. setcolor (color. gray); g2d. drawline (0, 0, this. getwidth (), 0); g2d. drawline (0, this. getheight ()-1, this. getwidth (), this. getheight ()-1); g2d. drawline (this. getwidth ()-1, 0, this. getwidth ()-1, this. getheight ()-1); // you must remember to call the paintcomponent method of the parent class. Otherwise, it will only strip and will not display the text super. paintcomponent (g) ;}}; label. settext (value! = NULL? Value. tostring (): "unknown"); label. sethorizontalalignment (jlabel. center); return label ;}} public static void main (string [] ARGs) {new simple ();}}

The transparent table is written here. If you are interested, you can continue to add special effects to it. For example, if the current head mouse has no light and shade effect, you can implement this function. You only need to add the mouse to the label in gettablecellrenderercomponent of the header Renderer to enter the event.

Related Article

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.