Let's take a look at this example. To save the changed status, pay attention to its setvalueat method.
Import javax. Swing. defaultcelleditor;
Import javax. Swing. jcombobox;
Import javax. Swing. jframe;
Import javax. Swing. jpanel;
Import javax. Swing. jscrollpane;
Import javax. Swing. jtable;
Import javax. Swing. Table. abstracttablemodel;
Import javax. Swing. Table. defaulttablecellrenderer;
Import javax. Swing. Table. tablecellrenderer;
Import javax. Swing. Table. tablecolumn;
Import java. AWT. component;
Import java. AWT. dimension;
Import java. AWT. gridlayout;
/**
* Tablerenderdemo is just like tabledemo, doesn't that it
* Explicitly initializes column sizes and it uses a combo box
* As an editor for the sport column.
*/
Public class tablerenderdemo extends jpanel {
Private Boolean DEBUG = false;
Public tablerenderdemo (){
Super (New gridlayout (1, 0 ));
Jtable table = new jtable (New mytablemodel ());
Table. setpreferredscrollableviewportsize (new dimension (500, 70 ));
Table. setfillsviewportheight (true );
// Create the scroll pane and add the table to it.
Jscrollpane scrollpane = new jscrollpane (table );
// Set up column sizes.
Initcolumnsizes (table );
// Fiddle with the sport column's cell Editors/renderers.
Setupsportcolumn (table, table. getcolumnmodel (). getcolumn (2 ));
// Add the scroll pane to this Panel.
Add (scrollpane );
}
/*
* This method picks good column sizes.
* If all column heads are wider than the column's cells'
* Contents, then you can just use column. sizewidthtofit ().
*/
Private void initcolumnsizes (jtable table ){
Mytablemodel model = (mytablemodel) Table. GetModel ();
Tablecolumn column = NULL;
Component comp = NULL;
Int headerwidth = 0;
Int cellwidth = 0;