Select a row in jtable, enter a number, and move one digit forward automatically. Click the return key to delete the last number.

Source: Internet
Author: User
Tags rowcount

Each time you enter a number, the number moves one digit forward, and the entered number is displayed in the last cell. Click the delete key, and the number moves one digit backward to delete the number in the last cell. For example

 

 

Package table; </P> <p> Import Java. AWT. borderlayout; <br/> Import Java. AWT. container; <br/> Import Java. AWT. dimension; <br/> Import Java. AWT. event. keyadapter; <br/> Import Java. AWT. event. keyevent; <br/> Import Java. AWT. event. windowadapter; <br/> Import Java. AWT. event. using wevent; </P> <p> Import javax. swing. jframe; <br/> Import javax. swing. jscrollpane; <br/> Import javax. swing. jtable; <br/> Import javax. swing. ta Ble. defaulttablemodel; <br/> Import javax. swing. table. tablecolumn; </P> <p> public class test {<br/> jtable table = NULL; <br/> jinetablemodel defaultmodel = NULL; <br/> tablecolumn column = NULL; </P> <p> Public test () {<br/> jframe F = new jframe (); <br/> string [] n = {"thousands ", "Hundred", "Ten", "Yuan" };< br/> defaultmodel = new jinetablemodel (n, 10); <br/> table = new jtable (defaultmodel ); </P> <p> table. setpreferredscrol Lableviewportsize (new dimension (400, 80); <br/> jscrollpane S = new jscrollpane (table); <br/> container contentpane = f. getcontentpane (); <br/> contentpane. add (S, borderlayout. center); </P> <p> table. addkeylistener (New mylistener (); </P> <p> F. settitle ("test table actionlisener"); <br/> F. setlocation (500,300); <br/> F. pack (); <br/> F. setvisible (true); <br/> F. addwindowlistener (New windowadapter () {<br/> pub LIC void windowclosing (invalid wevent e) {<br/> system. exit (0); <br/>}< br/> }); <br/>}</P> <p> public class mylistener extends keyadapter {<br/> Public void keypressed (keyevent ke) {<br/> int ROW = table. getselectedrow (); <br/> int Cols = table. getcolumncount (); <br/> Boolean accept = false; <br/> If (Ke. getkeycode ()> = 48 & Ke. getkeycode () <= 57) {<br/> If (table. getvalueat (row, 0) = NULL) {<br/> If (K E. getkeycode () = 48) {<br/> If (table. getvalueat (row, cols-2 )! = NULL) {<br/> Accept = true; <br/>}< br/>} else {<br/> Accept = true; <br/>}< br/>} else if (Ke. getkeycode () = 8) {<br/> If (table. getvalueat (row, cols-1 )! = NULL) {<br/> for (INT I = Cols-1; I> 0; I --) {<br/> table. setvalueat (table. getvalueat (row, I-1), row, I); <br/>}< br/> table. setvalueat (null, row, 0); <br/>}</P> <p> If (accept) {<br/> for (INT I = 0; I <Cols-1; I ++) {<br/> table. setvalueat (table. getvalueat (row, I + 1), row, I); <br/>}< br/> table. setvalueat (null, row, cols-1); <br/> table. setvalueat (Ke. getkeychar (), row, cols-1 ); <br/>}</P> <p> Public static void main (string ARGs []) {<br/> new test (); <br/>}</P> <p> public class jinetablemodel extends defaulttablemodel {</P> <p> Private Static final long serialversionuid = 1l; </P> <p> Public Boolean iscelleditable (int r, int c) {<br/> return false; <br/>}</P> <p> jinetablemodel (object [] columnnames, int rowcount) {<br/> super (columnnames, rowcount ); <br/>}</P> <p>

 

Running result

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.