| | |
| | | } |
| | | }; |
| | | table.setModel(dataModel); |
| | | setColumnType(dtoList,table); |
| | | setColumnType(dtoList, table); |
| | | |
| | | return table; |
| | | } |
| | |
| | | table.getColumnModel().getColumn(j).setCellRenderer(new CellComboBoxRenderer(itemMap)); |
| | | table.getColumnModel().getColumn(j).setCellEditor(new CellComboBoxEditor(comboBox, itemMap)); |
| | | } |
| | | if ("dicts".equals(dtoList.get(j).getColumnType())) { |
| | | Map<Long, JComboBoxItem> itemMap = new HashMap<>(); |
| | | JComboBoxItem[] dictList = dtoList.get(j).getDictList(); |
| | | for (JComboBoxItem item : dictList) { |
| | | itemMap.put(item.getId(), item); |
| | | } |
| | | |
| | | // 创建 JComboBox 并添加 Item 对象 |
| | | JComboBox<JComboBoxItem> comboBox = new JComboBox<>(dictList); |
| | | |
| | | // 设置自定义渲染器 |
| | | comboBox.setRenderer(new BasicComboBoxRenderer() { |
| | | @Override |
| | | public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { |
| | | super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); |
| | | if (value instanceof JComboBoxItem) { |
| | | JComboBoxItem item = (JComboBoxItem) value; |
| | | setText(item.getName()); // 显示 name |
| | | } |
| | | return this; |
| | | } |
| | | }); |
| | | table.getColumnModel().getColumn(j).setCellRenderer(new CellComboBoxRenderer(itemMap)); |
| | | table.getColumnModel().getColumn(j).setCellEditor(new CellComboBoxEditor(comboBox, itemMap)); |
| | | } |
| | | } |
| | | } |
| | | |
| | | public static void refreshTable(List<?> list, List<ColumnDto> dtoList, JTable table) { |
| | | TableModel dataModel = createCommonTableModel(list, dtoList); |
| | | table.setModel(dataModel); |
| | | setColumnType(dtoList,table); |
| | | setColumnType(dtoList, table); |
| | | } |
| | | |
| | | public static TableModel createCommonTableModel(List<?> list, List<ColumnDto> dtoList) { |
| | | public static String[][] getRowData(List<?> list, List<ColumnDto> dtoList) { |
| | | String[][] rowData = new String[list.size()][dtoList.size()]; |
| | | |
| | | for (int i = 0; i < list.size(); i++) { |
| | |
| | | rowData[i][j] = valueStr; |
| | | } else if (dtoList.get(j).getColumnType().equals("dict")) { |
| | | rowData[i][j] = valueStr; |
| | | } else if (dtoList.get(j).getColumnType().equals("dicts")) { |
| | | rowData[i][j] = valueStr; |
| | | } else if (dtoList.get(j).getColumnType().equals("autoCreate")) { |
| | | rowData[i][j] = "" + (i + 1); |
| | | } |
| | |
| | | } |
| | | } |
| | | } |
| | | return rowData; |
| | | } |
| | | |
| | | public static TableModel createCommonTableModel(List<?> list, List<ColumnDto> dtoList) { |
| | | String[][] rowData = getRowData(list, dtoList); |
| | | String[] columnNames = new String[dtoList.size()]; |
| | | for (int j = 0; j < dtoList.size(); j++) { |
| | | columnNames[j] = dtoList.get(j).getColumnDesc(); |