Sei sulla pagina 1di 2

Chapter 40 JTable and JTree

1.You can initialize a table using the constructor of JTable. You cannot specify the
maximum number of visible rows in a table without scrolling. You can specify the
height of a table cell using the setRowHeight method. You can specify the horizontal
margin of table cells using the setIntercellSpacing method.
2. TomodifytablecontentsvisuallyfromtheUI,thetable
cellsmustbeeditablewithanassociatededitorforthe
cell.Youmustalsosavethechangethroughthedata
model.TheDefaultTableModelclassprovidesmethodsfor
addingandremovingrows.Toaddacolumn,youmayuse
theaddColumnmethodinDefaultTableModelorin
DefaultTableColumnModel.Toremoveacolumn,youmustuse
theremoveTableColumnmethodfromthe
DefaultTableColumnModelclass.
3. JTablehastheautoResizingModepropertythatcanbeused
toautoresizeatablecolumn.Possiblevaluesare:
JTable.AUTO_RESIZE_OFF
JTable.AUTO_RESIZE_LAST_COLUMN
JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS
JTable.AUTO_RESIZE_NEXT_COLUMN
JTable.AUTO_RESIZE_ALL_COLUMNS

4. Thepropertiestoshowgrids,horizontalgrids,and
verticalgridsareshowGrid,showHorizontalGrid,and
showVerticalGrid.Thepropertiestospecifythetablerow
height,andverticalmarginarerowHeightandrowMargin.
Therearenomethodstosethorizontalmarginsbetween
thecells,becausethistypeofmarginisflexible.
5. Bydefault,acellobject'sstringrepresentationis
displayedandthestringcanbeeditedasitwasina
textfield.JTablemaintainsasetofpredefined
renderersandeditors,listedinTable8.1,whichcanbe
specifiedtoreplacedefaultstringrenderersand
editors.Youcancreateacustomrendererbyextending
theDefaultTableCellRendererclass.
6. Tocreateatree,simplyusetheconstructorofJTtree.
Tospecifytherowheightofatreenode,usethe
rowHeightproperty.Toobtainthedefaulttreemodeland
treeselectionmodelfromaninstanceofJTree,use
getModelandgetSelectionModelmethods.

7. Youhavetodeclareacustomtablemodelandimplement
theisCellEditable(row,column)methodtoreturnfalse.
Bydefault,allcellsareeditable.
8. ToinitializedatainatreeusingTreeModel,youneedto
createnodesusingtheDefaultMutableTreeNodeclass,and
settherootwiththeTreeModel(orDefaultTreeModel).To
addachildtoaninstanceofDefaultMutableTreeNode,use
theaddmethod.
9. ToinitializedatainatreeusingTreeModel,youneedto
createnodesusingtheDefaultMutableTreeNodeclass,and
settherootwiththeTreeModel(orDefaultTreeModel).To
addachildtoaninstanceofDefaultMutableTreeNode,use
theaddmethod.
10. Toenabletreenodeediting,setjTreeseditable
propertytotrue.Bydefault,itisfalse.
11. Toaddanodefromatree,usetheaddmethodfroman
instanceofDefaultMutableTreeNodeinaparent.Toremove
anodefromatree,usetheremoveNodeFromParentmethod
fromTreeModel.
12. Toobtainaselectedtreenode,usethe
getleadSelectedPathmethodtogetthepath,thengetthe
nodefromthepathusingthegetLastPathComponentmethod.

Potrebbero piacerti anche