问题描述
先看代码:importjava.awt.BorderLayout;importjava.awt.Color;importjava.awt.Component;importjava.awt.EventQueue;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.WindowAdapter;importjava.awt.event.WindowEvent;importjava.util.EventObject;importjavax.swing.AbstractCellEditor;importjavax.swing.DefaultCellEditor;importjavax.swing.ImageIcon;importjavax.swing.JColorChooser;importjavax.swing.JComboBox;importjavax.swing.JDialog;importjavax.swing.JFrame;importjavax.swing.JPanel;importjavax.swing.JScrollPane;importjavax.swing.JTable;importjavax.swing.UIManager;importjavax.swing.table.AbstractTableModel;importjavax.swing.table.TableCellEditor;importjavax.swing.table.TableCellRenderer;importjavax.swing.table.TableColumn;importjavax.swing.table.TableColumnModel;importjavax.swing.table.TableModel;publicclassTableCellRenderTest{/***@paramargs*/publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubtry{UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");}catch(Throwablee){e.printStackTrace();}EventQueue.invokeLater(newRunnable(){@Overridepublicvoidrun(){//TODOAuto-generatedmethodstubJFrameframe=newTableCellRenderFrame();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setVisible(true);}});}}classTableCellRenderFrameextendsJFrame{privatestaticfinallongserialVersionUID=2017007572540121699L;publicTableCellRenderFrame(){setTitle("TableCellRenderTest");setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);TableModelmodel=newPlanetTableModel();JTabletable=newJTable(model);table.setRowSelectionAllowed(false);//setuprenderersandeditorstable.setDefaultRenderer(Color.class,newColorTableCellRenderer());table.setDefaultEditor(Color.class,newColorTableCellEditor());JComboBox<Integer>moonCombo=newJComboBox<Integer>();for(inti=0;i<=20;i++){moonCombo.addItem(i);}//获取列视图TableColumnModelcolumnModel=table.getColumnModel();TableColumnmoonColumn=columnModel.getColumn(PlanetTableModel.MOONS_COLUMN);moonColumn.setCellEditor(newDefaultCellEditor(moonCombo));moonColumn.setHeaderRenderer(table.getDefaultRenderer(ImageIcon.class));moonColumn.setHeaderValue(newImageIcon("images/6.jpg"));//showtabletable.setRowHeight(100);add(newJScrollPane(table),BorderLayout.CENTER);}privatestaticfinalintDEFAULT_WIDTH=600;privatestaticfinalintDEFAULT_HEIGHT=400;}/**Theplanettablemodelspecifiesthevalue,renderingandeditingproperties*fortheplanetdata.*/classPlanetTableModelextendsAbstractTableModel{privatestaticfinallongserialVersionUID=-6525995252603866044L;//返回列数@OverridepublicintgetColumnCount(){//TODOAuto-generatedmethodstubreturncells[0].length;}//返回行数@OverridepublicintgetRowCount(){//TODOAuto-generatedmethodstubreturncells.length;}@OverridepublicObjectgetValueAt(intr,intc){//TODOAuto-generatedmethodstubreturncells[r][c];}publicStringgetColumnName(intc){returncolumnsName[c];}publicClass<?>getColumnClass(intc){returncells[0][c].getClass();}publicvoidsetValueAt(Objectobj,intr,intc){cells[r][c]=obj;}publicbooleanisCellEditable(intr,intc){returnc==PLANET_COLUMN||c==MOONS_COLUMN||c==GASEOUS_COLUMN||c==COLOR_COLUMN;}publicstaticfinalintPLANET_COLUMN=0;publicstaticfinalintMOONS_COLUMN=2;publicstaticfinalintGASEOUS_COLUMN=3;publicstaticfinalintCOLOR_COLUMN=4;privateObject[][]cells={{"Mercury",2440.0,0,false,Color.yellow,newImageIcon("images/7.jgp")},{"Venus",6052.0,0,false,Color.YELLOW,newImageIcon("images/8.jpg")},{"Earth",6378.0,0,false,Color.BLUE,newImageIcon("images/9.jpg")},{"Mars",3397.0,0,false,Color.RED,newImageIcon("images/10.jpg")},{"Jupiter",71492.0,false,Color.ORANGE,newImageIcon("images/11.jpg")},{"Saturn",60268.0,0,false,Color.ORANGE,newImageIcon("images/12.jpg")},{"Uranus",25559.0,0,false,Color.BLUE,newImageIcon("images/13.jpg")},{"Neptune",24766.0,0,false,Color.BLUE,newImageIcon("images/14.jpg")},{"Pluto",1137.0,1,false,Color.BLACK,newImageIcon("images/15.jpg")}};privateString[]columnsName={"Planet","Radius","Moons","Gaseous","Color","Image"};}/**Thisrendererrendersacolorvalueasapanelwiththegivencolor.*/classColorTableCellRendererextendsJPanelimplementsTableCellRenderer{privatestaticfinallongserialVersionUID=851890999617120783L;@OverridepublicComponentgetTableCellRendererComponent(JTabletable,Objectvalue,booleanisSelected,booleanhasFocus,introw,intcolumn){//TODOAuto-generatedmethodstubsetBackground((Color)value);if(hasFocus)setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));elsesetBorder(null);returnthis;}}/**Thiseditorpopsupacolordialogtoeditacellvalue*/classColorTableCellEditorextendsAbstractCellEditorimplementsTableCellEditor{privatestaticfinallongserialVersionUID=-5021717417421372502L;//构造器publicColorTableCellEditor(){panel=newJPanel();//preparecolordialog/**publicstaticJDialogcreateDialog(Componentc,Stringtitle,boolean*modal,JColorChoosercolorChooser,ActionLisntener*okListener,ActionListenercancelListener);**@paramc对话框的父组件**@paramtitle对话框的标题**@parammodal为true时,在关闭对话框之前,程序的剩余部分将一直处于非激活状态**@paramcolorChooser要置于对话框中的颜色选择器**@paramokListener按下"OK"时调用的ActionListener**@paramcalcelListener按下"Cancel"时调用的ActionListener*/colorChooser=newJColorChooser();colorDialog=JColorChooser.createDialog(null,"PlanetColor",false,colorChooser,newActionListener(){@OverridepublicvoidactionPerformed(ActionEventevent){//TODOAuto-generatedmethodstubstopCellEditing();}},newActionListener(){@OverridepublicvoidactionPerformed(ActionEventevent){//TODOAuto-generatedmethodstubcancelCellEditing();}});colorDialog.addWindowListener(newWindowAdapter(){@OverridepublicvoidwindowClosing(WindowEventarg0){//TODOAuto-generatedmethodstubcancelCellEditing();}});}@OverridepublicObjectgetCellEditorValue(){//TODOAuto-generatedmethodstubreturncolorChooser.getColor();}/**ComponentgetTableCellEditorComponent(JTabletable,Objectvalue,boolean*isSelected,introw,intcolumn)*/@OverridepublicComponentgetTableCellEditorComponent(JTabletable,Objectvalue,booleanisSelected,introw,intcolumn){//TODOAuto-generatedmethodstub//thisiswherewegetthecurrentColorvalue,Westoreitinthe//dialogincase//theuserstartseditingcolorChooser.setColor((Color)value);returnpanel;}publicbooleanshouldSelectCell(EventObjectanEvent){//starteditingcolorChooser.setVisible(true);//tellcalleritisoktoselectthiscellreturntrue;}//告知编辑器取消编辑并且不接受任何已部分编辑的值。publicvoidcancelCellEditing(){//editingiscanceled--hidedialogcolorDialog.setVisible(false);super.cancelCellEditing();}//告知编辑器停止编辑并接受任何已部分编辑的值作为编辑器的值。//如果编辑没有停止,则编辑器返回false;这对已生效并且无法接受无效条目的编辑器很有用。publicbooleanstopCellEditing(){//editingiscomplete--hidedialogcolorDialog.setVisible(false);super.stopCellEditing();//tellcalleritisoktousecolorvaluereturntrue;}privateJColorChoosercolorChooser;privateJDialogcolorDialog;privateJPanelpanel;}
运行截图:问题:当下拉滚动条时出现了错误,看Console截图: