Jtable learning Summary

Source: Internet
Author: User

// Demonstrate the jtable Function

Package table; import Java. AWT. borderlayout; import Java. AWT. dimension; import Java. AWT. event. actionevent; import Java. AWT. event. actionlistener; import javax. swing. *; import javax. swing. event. changeevent; import javax. swing. event. changelistener; import javax. swing. table. abstracttablemodel; import javax. swing. table. tablemodel; import javax. swing. table. tablerowsorter;/** to change this template, choose tools | Templates and open the template in * the editor. * // ***** @ author hufeiyan */public class jtabledemo extends jpanel {jtable tableview; jscrollpane scrollpane; dimension origin = new dimension (0, 0); jbutton toup; string [] columnnames = {"C1", "C2", "C3", "C4", "C5"}; private final static int preferred_width = 680; private Final Static int preferred_height = 600; private object [] [] DATA = {"m Ary "," Campione "," snowboarding ", new INTEGER (5), new Boolean (false) },{" Alison "," huml "," Rowing ", new INTEGER (3), new Boolean (true)}, {"Kathy", "walrath", "knitting", new INTEGER (2), new Boolean (false )}, {"Sharon", "zakhour", "Speed reading", new INTEGER (20), new Boolean (true) },{ "Philip", "Milne", "pool ", new INTEGER (10), new Boolean (false) },}; private Boolean DEBUG = false; public static void Main (string [] ARGs) {jtabledemo T = new jtabledemo (); jframe frame = new jframe (jtabledemo. class. getname (); frame. setdefaclocloseoperation (jframe. exit_on_close); frame. getcontentpane (). setlayout (New borderlayout (); frame. getcontentpane (). add (T, borderlayout. center); T. setpreferredsize (new dimension (preferred_width, preferred_height); frame. pack (); frame. setlocationrelativeto (null); frame. setvi Sible (true);} public jtabledemo () {Init ();} private void Init () {This. setlayout (New borderlayout (); tablemodel datamodel = new abstracttablemodel () {@ override public int getcolumncount () {return columnnames. length ;}@ override public int getrowcount () {return data. length ;}@ override public object getvalueat (INT row, int col) {return data [row] [col] ;}@ override Public String getcolumnname (INT column) {return columnnames [column];} @ override public class getcolumnclass (INT c) {return getvalueat (0, c ). getclass () ;}@ override public Boolean iscelleditable (INT row, int col) {// if false is returned, the cell cannot be edited, the first two columns cannot be edited // The Position of the drag column on the interface does not affect the column number return Col> 2;} @ override public void setvalueat (object value, int row, int col) {If (Debug) {system. out. println ("setting value at" + row + "," + Col +" "+ Value +" (an instance of "+ value. getclass () + ")");} data [row] [col] = value; firetablecellupdated (row, col); If (Debug) {system. out. println ("new value of data:"); printdebugdata () ;}}; tableview = new jtable (datamodel); tablerowsorter sorter = new tablerowsorter (datamodel); tableview. setrowsorter (sorter); tableview. setrowheight (2,100); scrollpane = new jscrollpane (tableview); tablevie W. setfillsviewportheight (true); this. add (scrollpane, borderlayout. center); toup = new jbutton ("up"); // select a row and click the up button to move up. addactionlistener (New actionlistener () {@ override public void actionreceivmed (actionevent e) {int [] rowindexs = jtabledemo. this. tableview. getselectedrows (); If (rowindexs = NULL | rowindexs. length = 0) {return;} int Index = rowindexs [0]; If (index! = 0) {object [] temp = jtabledemo. this. data [Index]; jtabledemo. this. data [Index] = jtabledemo. this. data [index-1]; jtabledemo. this. data [index-1] = temp; jtabledemo. this. tableview. setrowselectioninterval (index-1, index-1); For (INT I = 1; I <rowindexs. length; I ++) {jtabledemo. this. tableview. addrowselectioninterval (rowindexs [I], rowindexs [I]) ;}} printdebugdata () ;}}); jpanel u = new jpanel (); final jcheckbox DEBUG = new jcheckbox ("debug", debug); debug. addchangelistener (New changelistener () {@ override public void statechanged (changeevent e) {DEBUG = debug. isselected () ;}}); U. add (toup); U. add (Debug); this. add (u, borderlayout. north);} public int getcolumncount () {return columnnames. length;} public int getrowcount () {return data. length;} private void printdebugdata () {int numrows = getrowcount (); int numcols = getcolumncount (); For (INT I = 0; I <numrows; I ++) {system. out. print ("row" + I + ":"); For (Int J = 0; j <numcols; j ++) {system. out. print ("" + data [I] [J]);} system. out. println ();} system. out. println ("--------------------------");}}

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.