1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
| import java.io.File; import java.awt.*; import javax.swing.*; import javax.swing.table.*; import javax.swing.filechooser.*;
public class TableCellEditorTest { JFrame jf = new JFrame("使用单元格编辑器"); JTable table; Object[][] tableData = { new Object[]{"李清照" , 29 , "女" , new ImageIcon("icon/3.gif") , new ImageIcon("icon/3.gif") , true}, new Object[]{"苏格拉底", 56 , "男" , new ImageIcon("icon/1.gif") , new ImageIcon("icon/1.gif") , false}, new Object[]{"李白", 35 , "男" , new ImageIcon("icon/4.gif") , new ImageIcon("icon/4.gif") , true}, new Object[]{"弄玉", 18 , "女" , new ImageIcon("icon/2.gif") , new ImageIcon("icon/2.gif") , true}, new Object[]{"虎头" , 2 , "男" , new ImageIcon("icon/5.gif") , new ImageIcon("icon/5.gif") , false} }; String[] columnTitle = {"姓名" , "年龄" , "性别" , "主头像" , "次头像" , "是否中国人"}; public void init() { ExtendedTableModel model = new ExtendedTableModel( columnTitle , tableData); table = new JTable(model); table.setRowSelectionAllowed(false); table.setRowHeight(40); table.setDefaultEditor(ImageIcon.class, new ImageCellEditor()); TableColumn lastColumn = table.getColumnModel().getColumn(4); JComboBox<ImageIcon> editCombo = new JComboBox<>(); for (int i = 1; i <= 10; i++) { editCombo.addItem(new ImageIcon("icon/" + i + ".gif")); } lastColumn.setCellEditor(new DefaultCellEditor(editCombo)); jf.add(new JScrollPane(table)); jf.pack(); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setVisible(true); } public static void main(String[] args) { new TableCellEditorTest().init(); } } class ExtendedTableModel extends DefaultTableModel { public ExtendedTableModel(String[] columnNames , Object[][] cells) { super(cells , columnNames); } public Class getColumnClass(int c) { return getValueAt(0 , c).getClass(); } }
class ImageCellEditor extends DefaultCellEditor { private JFileChooser fDialog = new JFileChooser(); ; private JTextField field = new JTextField(15); private JButton button = new JButton("..."); public ImageCellEditor() { super(new JTextField()); initEditor(); } private void initEditor() { field.setEditable(false); button.addActionListener(e -> browse()); fDialog.addChoosableFileFilter(new FileFilter() { public boolean accept(File f) { if (f.isDirectory()) { return true; } String extension = Utils.getExtension(f); if (extension != null) { if (extension.equals(Utils.tiff) || extension.equals(Utils.tif) || extension.equals(Utils.gif) || extension.equals(Utils.jpeg) || extension.equals(Utils.jpg) || extension.equals(Utils.png)) { return true; } else { return false; } } return false; } public String getDescription() { return "有效的图片文件"; } }); fDialog.setAcceptAllFileFilterUsed(false); } public Component getTableCellEditorComponent(JTable table , Object value , boolean isSelected , int row , int column) { this.button.setPreferredSize(new Dimension(20, 20)); JPanel panel = new JPanel(); panel.setLayout(new BorderLayout()); field.setText(value.toString()); panel.add(this.field, BorderLayout.CENTER); panel.add(this.button, BorderLayout.EAST); return panel; } public Object getCellEditorValue() { return new ImageIcon(field.getText()); } private void browse() { fDialog.setCurrentDirectory(new File("icon")); int result = fDialog.showOpenDialog(null); if (result == JFileChooser.CANCEL_OPTION) { super.cancelCellEditing(); return; } else { field.setText("icon/" + fDialog.getSelectedFile().getName()); } } } class Utils { public final static String jpeg = "jpeg"; public final static String jpg = "jpg"; public final static String gif = "gif"; public final static String tiff = "tiff"; public final static String tif = "tif"; public final static String png = "png"; public static String getExtension(File f) { String ext = null; String s = f.getName(); int i = s.lastIndexOf('.'); if (i > 0 && i < s.length() - 1) { ext = s.substring(i + 1).toLowerCase(); } return ext; } }
|