Swing jtable add text input boxes to table cells

Source: Internet
Author: User

Adding text input boxes to the jtable unit is common. This method is usually used in development to control the input in the table.
Implementation Method:
Rewrite: tablecellrenderer Interface
Component gettablecellrenderercomponent (jtable table,
Object value,
Boolean isselected,
Boolean hasfocus,
Int row,
Int column) returns the component used to draw cells. This method is used to configure the Renderer properly before painting.

Parameters:
Table-The jtable to be drawn by the Renderer; it can be null.
Value-the value of the cell to be rendered. This value is interpreted and drawn by the specific Renderer. For example, if the value is a string "true", it can be rendered as a string or as a selected check box. Null is a valid value.

Isselected-if you use the highlighted display of the selected style to present the cell, the value is true; otherwise, the value is false.
Hasfocus-if it is true, the cell is properly rendered. For example, place a special border on a cell. If you can edit the cell, it is displayed in color to indicate that the cell is being edited.
Row-row index of the cell to be drawn. When the header is drawn, the row value is-1
Column-column index of the cell to be drawn

Import COM. jtextfield. test. uppercasefield; import Java. AWT. borderlayout; import Java. AWT. color; import Java. AWT. component; import javax. swing. jframe; import javax. swing. jscrollpane; import javax. swing. jtable; import javax. swing. jtextfield; import javax. swing. table. defaulttablemodel; import javax. swing. table. tablecellrenderer; public class evenoddrowcellrenderer extends jframe {defaulttablemodel TM Odel = new defaulttablemodel (new object [] [] {"some", "text" },{ "any", "text" },{ "even ", "More" },{ "text", "strings" },{ "and", "other" },{ "text", "values "}}, new object [] {"column 1", "column 2"}); Public evenoddrowcellrenderer () {setdefaclocloseoperation (jframe. exit_on_close); jtable table = new jtable (tModel); table. setdefaultrenderer (object. class, new myrenderer (); getcontentp Ane (). add (New jscrollpane (table), borderlayout. center); Pack ();} public static void main (string Arg []) {New evenoddrowcellrenderer (). setvisible (true) ;}} class myrenderer implements tablecellrenderer {public component gettablecellrenderercomponent (jtable table, object value, Boolean isselected, Boolean hasfocus, int row, int column) {uppercasefield editor = new uppercasefield (10); If (value! = NULL) Editor. settext (value. tostring (); editor. setbackground (row % 2 = 0 )? Color. white: color. cyan); Return editor;} import javax. swing. jtextfield; import javax. swing. text. attributeset; import javax. swing. text. badlocationexception; import javax. swing. text. document; import javax. swing. text. plaindocument; public class uppercasefield extends jtextfield {public uppercasefield (INT Cols) {super (Cols);} protected document createdefaultmodel () {return New uppercasedocument ();} static class uppercasedocument extends plaindocument {public void insertstring (INT offs, string STR, attributeset A) throws badlocationexception {If (STR = NULL) {return ;} // control the input super. insertstring (offs, STR, );}}}

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.