Today, I wrote an article about jtable, including setting the font color for jtable cells, setting the background color for jtable cells, and making it impossible to edit a column in jtable. The code is very simple. For more information, see:
PackageJava;
ImportJavax. Swing. jframe;
ImportJavax. Swing. jtable;
ImportJavax. Swing. jpanel;
ImportJavax. Swing. jscrollpane;
ImportJava. AWT. borderlayout;
ImportJavax. Swing. jcombobox;
ImportJavax. Swing. Table. tablecolumn;
ImportJavax. Swing. defaultcelleditor;
ImportJavax. Swing. Table. defaulttablecellrenderer;
ImportJava. AWT. color;
ImportJavax. Swing. Table. defaulttablemodel;
/**
* Set the cell to uneditable.
*/
Public ClassJtableframe_4ExtendsJframe {
PrivateJpanel;
PrivateJtable table; // defines two-dimensional data tables.
PrivateJscrollpane scrollpane; // defines a panel with a scroll bar
PrivateDefaulttablemodel DTM; // defines a table model that can store data.
// Initialize data
Object [] [] rowdata = {
{"1001", "Li han", "software department ",NewDouble (3000 )},
{"1002", "Zhu ze", "software department ",NewDouble (3100 )},
{"1003", "Liu Yu", "manager department ",NewDouble (3000 )},
{"1004", "Zhang Bin", "software department ",NewDouble (4000 )},
{"1005", "Qin Xiao", "Personnel Department ",NewDouble (1, 4000 )}
};
// Initialize the column name
Object [] columnnames = {"no.", "name", "department", "monthly salary "};
PublicJtableframe_4 (){
Super("Cells with drop-down lists ");
Init ();
}
Public VoidInit (){
Setsize (500,300 );
Setdefaclocloseoperation (jframe. exit_on_close );
// Use tablemodel to re-overwrite iscelleditable and change the first column to uneditable.
DTM =NewDefaulttablemodel (rowdata, columnnames ){
Public BooleanIscelleditable (IntRowindex,IntColumnindex ){
BooleanF = (0 <= rowindex & rowindex <getrowcount () & columnindex = 0 )?False:True;
ReturnF;
}
};
Table =NewJtable (DTM );
Jcombobox ComboBox =NewJcombobox ();
ComboBox. additem ("software Department ");
ComboBox. additem ("manager ");
ComboBox. additem ("Personnel Department ");
ComboBox. additem ("Logistics Department ");
ComboBox. additem ("Finance Department ");
// Tablecolumn is used to edit attributes of a column in a table.
Tablecolumn = table. getcolumn ("Department ");
// Use the setcelleditor () method in the tablecolumn class to set the cell Editor
// The defaultcelleditor class can set cells in the table as drop-down boxes.
Tablecolumn. setcelleditor (NewDefaultcelleditor (ComboBox ));
// Defaulttablecellrenderer class can draw cell background, font color, and other functions
Defaulttablecellrenderer backgroundcolor =NewDefaulttablecellrenderer ();
// The background of the department column is yellow.
Backgroundcolor. setbackground (color. Yellow );
Tablecolumn. setcellrenderer (backgroundcolor );
Tablecolumn moneycolumn = table. getcolumn ("monthly salary ");
// Draw the font color of the monthly salary Column
Defaulttablecellrenderer fontcolor =NewDefaulttablecellrenderer (){
Public VoidSetvalue (object Value) {// rewrite the setvalue method to dynamically set the font color of the column unit.
DoubleA = (ValueInstanceofDouble )? (Double) value). doublevalue (): 0.0; // obtain the value in the monthly salary Column
Setforeground (A> 3099.0 )? Color. RED: color. Black); // if the monthly salary is greater than 3099 yuan, set the font to red.
Settext (value =Null)? "": Value. tostring ());
}
};
Moneycolumn. setcellrenderer (fontcolor );
Panel =NewJpanel ();
Panel. setlayout (NewBorderlayout ());
Scrollpane =NewJscrollpane ();
Scrollpane. getviewport (). setview (table); // place the two-dimensional data table in the scroll panel.
Panel. Add (scrollpane, borderlayout. center); // Add the scroll panel to the bottom panel.
This. Getcontentpane (). Add (panel, borderlayout. center );
Setvisible (True);
}
Public Static VoidMain (string [] ARGs ){
Jtableframe_4 F =NewJtableframe_4 ();
}
}