Import javax. Swing. jframe;
Import javax. Swing. jpanel;
Import javax. Swing. jscrollpane;
Import javax. Swing. jtable;
Import javax. Swing. swingutilities;
Import javax. Swing. event. listselectionevent;
Import javax. Swing. event. listselectionlistener;
Import javax. Swing. Table. defaulttablecellrenderer;
Import javax. Swing. Table. tablecellrenderer;
Import java. AWT. component;
Import java. AWT. dimension;
Import java. AWT. gridlayout;
Import java. AWT. event. mouseadapter;
Import java. AWT. event. mouseevent;
Import java. Lang. runnable;
Import java. AWT. Font;
Public class simpletabledemo extends jpanel {
Private Boolean DEBUG = false;
Public simpletabledemo (){
Super (New gridlayout (1, 0 ));
String [] columnnames = {"first name ",
"Last Name ",
"Sport ",
"# Of years ",
"Vegetarian "};
Object [] [] DATA = {
{"Mary", "Campione ",
"Snowboarding", new INTEGER (5), new Boolean (true )},
{"Alison", "huml ",
"Rowing", new INTEGER (3), new Boolean (true )},
{"Kathy", "walrath ",
"Knitting", new INTEGER (2), new Boolean (true )},
{"Sharon", "zakhour ",
"Speed reading", new INTEGER (20), new Boolean (true )},
{"Philip", "Milne ",
"Pool", new INTEGER (10), new Boolean (true )}
};
Final jboldtable table = new jboldtable (data, columnnames );
Table. setpreferredscrollableviewportsize (new dimension (500, 70 ));
If (Debug ){
Table. addmouselistener (New mouseadapter (){
Public void mouseclicked (mouseevent e ){
Printdebugdata (table );
}
});
}
// Create the scroll pane and add the table to it.
Jscrollpane scrollpane = new jscrollpane (table );
// Add the scroll pane to this Panel.
Add (scrollpane );
}
Private void printdebugdata (jtable table ){
Int numrows = table. getrowcount ();
Int numcols = table. getcolumncount ();
Javax. Swing. Table. tablemodel model = table. GetModel ();
System. Out. println ("value of data :");
For (INT I = 0; I <numrows; I ++ ){
System. Out. Print ("row" + I + ":");
For (Int J = 0; j <numcols; j ++ ){
System. Out. Print ("" + model. getvalueat (I, j ));
}
System. Out. println ();
}
System. Out. println ("--------------------------");
}
/**
* Create the GUI and show it. For thread safety,
* This method shoshould be invoked from
* Event-dispatching thread.
*/
Private Static void createandshowgui (){
// Make sure we have nice window decorations.
// Jframe. setdefalooklookandfeeldecorated (true );
// Create and set up the window.
Jframe frame = new jframe ("simpletabledemo ");
Frame. setdefaclocloseoperation (jframe. exit_on_close );
// Create and set up the content pane.
Simpletabledemo newcontentpane = new simpletabledemo ();
Newcontentpane. setopaque (true); // content panes must be opaque
Frame. setcontentpane (newcontentpane );
// Display the window.
Frame. Pack ();
Frame. setvisible (true );
}
Public static void main (string [] ARGs ){
// Schedule a job for the event-dispatching thread:
// Creating and showing this application's Gui.
Javax. Swing. swingutilities. invokelater (New runnable (){
Public void run (){
Createandshowgui ();
}
});
}
}
Class jboldtable extends jtable implements listselectionlistener {
Mybold bold = new mybold (this );
Thread TD = new thread (BOLD );
Boolean checkrun = false;
Int ROW =-1;
Public jboldtable (object [] [] data, object [] column ){
Super (data, column );
Setdefaultrenderer (object. Class, new defaulttablecellrenderer (){
Public component gettablecellrenderercomponent (jtable table, object value, Boolean isselected, Boolean hasfocus, int row, int column ){
// Todo automatically generates method stubs
Super. gettablecellrenderercomponent (table, value, isselected,
Hasfocus, row, column );
Boolean bflag = (Boolean) getvalueat (row, 4 );
Int nflag = 0;
If (bflag ){
Nflag = font. Bold;
}
Font font = new font ("", nflag, 12 );
Setfont (font );
Return this;
}
});
Getselectionmodel (). addlistselectionlistener (this );
}
Public void valuechanged (listselectionevent e ){
// Todo automatically generates method stubs
Repaint ();
If (row! = Getselectedrow ()){
Row = getselectedrow ();
If (TD. isalive ()){
Bold. Stop ();
Try {
TD. Join ();
} Catch (interruptedexception E1 ){
// Todo automatically generates catch Blocks
E1.printstacktrace ();
}
}
Bold. Start ();
TD = new thread (BOLD );
TD. Start ();
}
}
Class mybold implements runnable {
Private jtable table;
Volatile Boolean isstop = false;
Public mybold (jtable table ){
This. Table = table;
}
Public void start (){
Isstop = false;
}
Public void stop (){
Isstop = true;
}
Public synchronized void run (){
// Todo automatically generates method stubs
Int row1 = table. getselectedrow ();
For (INT I = 0; I <1000; ++ I ){
Try {
Thread. Sleep (1 );
If (isstop)
Return;
} Catch (interruptedexception e ){
// Todo automatically generates catch Blocks
E. printstacktrace ();
}
}
Int row2 = table. getselectedrow ();
If (row1! = Row2)
Return;
Boolean bflag = (Boolean) Table. getvalueat (row1, 4 );
If (! Bflag)
Return;
Table. setvalueat (object) (new Boolean (! Bflag. booleanvalue (), row1, 4 );
Table. repaint ();
// Table. updateui ();
}
}
}