I wrote a previous article to generate a transparent jtable, but the title of the jtable header is not transparent. After continuous learning, I finally found a way to set the header to transparent. First effect:
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 simple2 extends jframe {Private Static final long serialversionuid = 1l; Private Static final int min_width = 1270; Private Static Final int min_height = 700; private color color1 = new color (225,237,233); Private color color2 = new color (76,124,206); Private jtable filetable; Public simple2 () {// 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. */Header. setdefaultrenderer (render); tablecellrenderer headerrenderer = header. getdefaultrenderer (); If (headerrenderer instanceof jlabel) {(jlabel) headerrenderer ). sethorizontalalignment (jlabel. center); (jlabel) headerrenderer ). setopaque (false) ;}} public static void main (string [] ARGs) {New simple2 ();}}
In fact, to achieve these transparency, the key is to have an understanding of jscrollpane. Here we provide a jscrollpane diagram.
The header here is not very nice, mainly because the split line is invisible, so we will continue to improve it in the next chapter.