ERLEDIGT
NEIN
NEIN
ANTWORTEN
22
22
ZUGRIFFE
10293
10293
EMPFEHLEN
-
07.12.05 23:52 #16
- Registriert seit
- Jun 2002
- Ort
- Saarbrücken (Saarland)
- Beiträge
- 9.886
- Blog-Einträge
- 29
Hallo!
hier mal noch mal schnell eine refactorierte Version...
Code :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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
/** * */ package de.tutorials; import java.awt.BorderLayout; import java.awt.Point; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.lang.reflect.Method; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPopupMenu; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JTextField; import javax.swing.SwingUtilities; import javax.swing.table.AbstractTableModel; import javax.swing.table.DefaultTableModel; import javax.swing.table.TableModel; /** * @author Tom * */ public class FilteredJTableExample extends JFrame { FilterableJTable table; JTextField txtFilter; JComboBox cboSelectFilterColumn; final static String DEFAULT_FILTER = "^#.*"; JPopupMenu popupMenu; public FilteredJTableExample() { super("FilteredJTableExample"); setDefaultCloseOperation(EXIT_ON_CLOSE); popupMenu = createPopupMenu(); table = new FilterableJTable(createFilterableTableModel()); table.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { if (SwingUtilities.isRightMouseButton(e)) { Point point = e.getPoint(); popupMenu.show(table, point.x, point.y); } } }); txtFilter = new JTextField(20); txtFilter.addKeyListener(new KeyAdapter() { public void keyReleased(KeyEvent e) { table.setFilter(DEFAULT_FILTER .replace("#", txtFilter.getText())); table.updateUI(); } }); cboSelectFilterColumn = new JComboBox(createHeaderNames()); cboSelectFilterColumn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { table.setFilterColumn(cboSelectFilterColumn.getSelectedIndex()); } }); JScrollPane scrollPane = new JScrollPane(table); add(cboSelectFilterColumn, BorderLayout.WEST); add(txtFilter, BorderLayout.EAST); add(scrollPane, BorderLayout.SOUTH); pack(); setVisible(true); } JPopupMenu createPopupMenu() { JPopupMenu menu = new JPopupMenu(); JMenuItem deleteItem = new JMenuItem("delete"); deleteItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int selectedRow = table.getSelectedRow(); if (selectedRow == -1) { JOptionPane.showMessageDialog(FilteredJTableExample.this, "No row selected!", "Error", JOptionPane.ERROR_MESSAGE); return; } System.out.println("removeRow @idx: " + selectedRow); table.deleteRow(selectedRow); table.updateUI(); } }); menu.add(deleteItem); return menu; } private TableModel createFilterableTableModel() { return new DefaultTableModel(createRowData(), createHeaderNames()); } private Object[][] createRowData() { return new Object[][]{{"aaa", "121"}, {"aab", "111"}, {"abc", "123"}, {"xabc", "4123"}}; } private Object[] createHeaderNames() { return new Object[]{"A", "B"}; } /** * @param args */ public static void main(String[] args) { new FilteredJTableExample(); } static class FilterableJTable extends JTable { public FilterableJTable(TableModel model) { super(new FilterableTableModel(model)); } public void deleteRow(int selectedRow) { ((FilterableTableModel) this.getModel()).deleteRow(selectedRow); } public void setFilterColumn(int filterColumn) { ((FilterableTableModel) this.getModel()) .setFilterColumn(filterColumn); } public int getFilterColumn() { return ((FilterableTableModel) this.getModel()).getFilterColumn(); } public void setFilter(String filter) { ((FilterableTableModel) this.getModel()).setFilter(filter); } public String getFilter() { return ((FilterableTableModel) this.getModel()).getFilter(); } } static class FilterableTableModel extends AbstractTableModel { TableModel delegateTableModel; int[] filterMapping; String filter; int filterColumn; int filterMatchCount; public FilterableTableModel(TableModel delegateTableModel) { this.delegateTableModel = delegateTableModel; } public int getRowCount() { if (isFilterEnabled()) { doFilter(); } return this.filterMapping != null ? this.filterMapping.length : this.delegateTableModel.getRowCount(); } public int getColumnCount() { return delegateTableModel.getColumnCount(); } public Object getValueAt(int rowIndex, int columnIndex) { int actualRowIndex = rowIndex; if (isFilterEnabled()) { actualRowIndex = this.filterMapping[rowIndex]; } return delegateTableModel.getValueAt(actualRowIndex, columnIndex); } public int[] getFilterMapping() { return filterMapping; } public void setFilterMapping(int[] filterMapping) { this.filterMapping = filterMapping; } public TableModel getDelegateTableModel() { return delegateTableModel; } public void setDelegateTableModel(TableModel delegate) { this.delegateTableModel = delegate; } public void deleteRow(int selectedRow) { try { Method deleteRowMethod = delegateTableModel.getClass() .getDeclaredMethod("removeRow", new Class[]{int.class}); if (deleteRowMethod != null) { deleteRowMethod.invoke(delegateTableModel, new Object[]{Integer.valueOf(selectedRow)}); } } catch (Exception e) { throw new UnsupportedOperationException(e); } } private void doFilter() { filterMatchCount = 0; int rowCount = delegateTableModel.getRowCount(); int[] tmpMapping = new int[rowCount]; for (int i = 0; i < rowCount; i++) { if (delegateTableModel.getValueAt(i, filterColumn).toString() .matches(this.filter)) { tmpMapping[filterMatchCount++] = i; } } int[] currentFilterMapping = new int[filterMatchCount]; System.arraycopy(tmpMapping, 0, currentFilterMapping, 0, filterMatchCount); setFilterMapping(currentFilterMapping); } public int getFilterMatchCount() { return filterMatchCount; } public void setFilterMatchCount(int filterMatches) { this.filterMatchCount = filterMatches; } public String getFilter() { return filter; } public void setFilter(String filter) { this.filter = filter; } boolean isFilterEnabled() { return getFilter() != null && !"".equals(getFilter()); } public int getFilterColumn() { return filterColumn; } public void setFilterColumn(int filterColumn) { this.filterColumn = filterColumn; } } }
Gruss TomJava rocks!
How to become a good Java Programmer?
Does IT in Java and .Net
The only valid measurement of code quality: WTFs / minute
Blog
Xing
Twitter
-
Hi Tom,
vielen, vielen Dank für den Code. Ganz großes Tennis.
Er bringt bei mir zwar momentan noch ein paar Fehlermeldungen. Aber ich hoffe, dass ich das behoben bring.
Und dann kuck ich mal, ob es das ist was ich gesucht hab
Gruß Diable
-
Irgendwie krieg ich da ein paar Sachen net so hin, wie ich will.
Also erstmal danke Tom für den Code. Genau so, will ich das auch hinbekommen.
Nur leider, bekomm ich deine Klasse FilterableTableModel und meine Klasse TableRowsTableModel nicht vereint, bzw. nicht so vereint, dass mein Prog läuft.
Code :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
public class TableRowsTableModel extends DefaultTableModel { private String[] columnNames = {"Nr", "Anfrageart", "Teilnehmernr", "Name", "Vorname", "Strasse", "Ort", "kASpalte"}; private Class[] classTypes = {Integer.class, //Spalte 0: Nr String.class, //Spalte 1: Anfrageart String.class, //Spalte 2: Teilnehmernummer String.class, //Spalte 3: Name String.class, //Spalte 4: Vorname String.class, //Spalte 5: Strasse String.class, //Spalte 6: Ort String.class, //Spalte 7: Spalte }; TableRowsTableModel() { super(); } public int getColumnCount() { return columnNames.length; } public String getColumnName(int col) { return columnNames[col]; } public Class getColumnClass (int columnIndex) { return classTypes[columnIndex]; } public int getRowCount() { return getRows().size(); } public Object getValueAt(int rowIndex, int columnIndex) { Object objRet = null; try { objRet = ((TableRows) getRows().get(rowIndex)).getValueAt(columnIndex); } catch (Exception e){ } return objRet; } private TableRows getRow(int row) { return (TableRows) getRows().get(row); } private List rows; public List getRows() { if (rows == null) { rows = new ArrayList(); } return rows; } public void addRow(TableRows newRow) { getRows().add(newRow); fireTableDataChanged(); } public void deleteRow(int delRow) { getRows().remove(delRow); fireTableDataChanged(); } }
-
Also falls mir irgendjemand von euch nochmal helfen könnte, wär ich euch dankbar.
Wie gesagt, muss ich die Klasse FilterableTableModel und "meine" Klasse TableRowsTableModel zusammenfügen? und wenn ja wie?
Wenn ich das richtig sehe, kann ich mit der Klasse FilterableTableModel alleine ja keine Daten einlesen, oder
Also, falls mir irgendjemand nochmal Tipps geben könnte, wär das echt klasse.
Bin grad total am Verzweifeln.
-
08.12.05 20:40 #20
- Registriert seit
- Jun 2002
- Ort
- Saarbrücken (Saarland)
- Beiträge
- 9.886
- Blog-Einträge
- 29
Hallo!
Es ist doch kein Problem die Einfüg-Fähigkeit hinzuzufügen... das zugrundeliegende TableModel muss das nur unterstützen... sortieren ist auch nicht viel schwerer...
Code :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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
/** * */ package de.tutorials; import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.Point; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.lang.reflect.Method; import java.util.regex.PatternSyntaxException; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPopupMenu; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JTextField; import javax.swing.SwingUtilities; import javax.swing.table.AbstractTableModel; import javax.swing.table.DefaultTableModel; import javax.swing.table.TableModel; /** * @author Tom * */ public class FilteredJTableExample extends JFrame { FilterableJTable table; JTextField txtFilter, txtA, txtB; JButton btnAddRow; JComboBox cboSelectFilterColumn; final static String DEFAULT_FILTER = "^#.*"; JPopupMenu popupMenu; public FilteredJTableExample() { super("FilteredJTableExample"); setDefaultCloseOperation(EXIT_ON_CLOSE); popupMenu = createPopupMenu(); table = new FilterableJTable(createFilterableTableModel()); table.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { if (SwingUtilities.isRightMouseButton(e)) { Point point = e.getPoint(); popupMenu.show(table, point.x, point.y); } } }); txtFilter = new JTextField(20); txtFilter.addKeyListener(new KeyAdapter() { public void keyReleased(KeyEvent e) { table.setFilter(DEFAULT_FILTER .replace("#", txtFilter.getText())); table.updateUI(); } }); cboSelectFilterColumn = new JComboBox(createHeaderNames()); cboSelectFilterColumn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { table.setFilterColumn(cboSelectFilterColumn.getSelectedIndex()); } }); JScrollPane scrollPane = new JScrollPane(table); JPanel addRowPanel = new JPanel(new GridLayout(1, 5)); txtA = new JTextField(5); txtB = new JTextField(5); btnAddRow = new JButton("add"); btnAddRow.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String strA = txtA.getText(); String strB = txtB.getText(); if (!"".equals(strA) && !"".equals(strB)) { table.addRow(new Object[] { strA, strB }); table.updateUI(); txtA.setText(""); txtB.setText(""); } } }); addRowPanel.add(new JLabel("A:")); addRowPanel.add(txtA); addRowPanel.add(new JLabel("B:")); addRowPanel.add(txtB); addRowPanel.add(btnAddRow); add(addRowPanel, BorderLayout.NORTH); add(cboSelectFilterColumn, BorderLayout.WEST); add(txtFilter, BorderLayout.EAST); add(scrollPane, BorderLayout.SOUTH); pack(); setVisible(true); } JPopupMenu createPopupMenu() { JPopupMenu menu = new JPopupMenu(); JMenuItem deleteItem = new JMenuItem("delete"); deleteItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int selectedRow = table.getSelectedRow(); if (selectedRow == -1) { JOptionPane.showMessageDialog(FilteredJTableExample.this, "No row selected!", "Error", JOptionPane.ERROR_MESSAGE); return; } System.out.println("removeRow @idx: " + selectedRow); table.deleteRow(selectedRow); table.updateUI(); } }); menu.add(deleteItem); return menu; } private TableModel createFilterableTableModel() { return new DefaultTableModel(createRowData(), createHeaderNames()); } private Object[][] createRowData() { return new Object[][] { { "aaa", "121" }, { "aab", "111" }, { "abc", "123" }, { "xabc", "4123" } }; } private Object[] createHeaderNames() { return new Object[] { "A", "B" }; } /** * @param args */ public static void main(String[] args) { new FilteredJTableExample(); } static class FilterableJTable extends JTable { public FilterableJTable(TableModel model) { super(new FilterableTableModel(model)); } public void deleteRow(int selectedRow) { ((FilterableTableModel) this.getModel()).deleteRow(selectedRow); } public void setFilterColumn(int filterColumn) { ((FilterableTableModel) this.getModel()) .setFilterColumn(filterColumn); } public int getFilterColumn() { return ((FilterableTableModel) this.getModel()).getFilterColumn(); } public void setFilter(String filter) { ((FilterableTableModel) this.getModel()).setFilter(filter); } public String getFilter() { return ((FilterableTableModel) this.getModel()).getFilter(); } public void addRow(Object[] rowColumns) { ((FilterableTableModel) this.getModel()).addRow(rowColumns); } } static class FilterableTableModel extends AbstractTableModel { TableModel delegateTableModel; int[] filterMapping; String filter; int filterColumn; int filterMatchCount; public FilterableTableModel(TableModel delegateTableModel) { this.delegateTableModel = delegateTableModel; } public void addRow(Object[] rowColumns) { try { Method addRowMethod = delegateTableModel.getClass() .getDeclaredMethod("addRow", new Class[] { Object[].class }); if (addRowMethod != null) { addRowMethod.invoke(delegateTableModel, new Object[] { rowColumns }); } else { throw new UnsupportedOperationException( "Adding Rows is not supported by: " + delegateTableModel.getClass()); } } catch (Exception e) { e.printStackTrace(); } } public void deleteRow(int selectedRow) { try { Method deleteRowMethod = delegateTableModel.getClass() .getDeclaredMethod("removeRow", new Class[] { int.class }); if (deleteRowMethod != null) { deleteRowMethod.invoke(delegateTableModel, new Object[] { Integer.valueOf(selectedRow) }); } else { throw new UnsupportedOperationException( "Deleting Rows is not supported by: " + delegateTableModel.getClass()); } } catch (Exception e) { e.printStackTrace(); } } public int getRowCount() { if (isFilterEnabled()) { doFilter(); } return this.filterMapping != null ? this.filterMapping.length : this.delegateTableModel.getRowCount(); } public int getColumnCount() { return delegateTableModel.getColumnCount(); } public Object getValueAt(int rowIndex, int columnIndex) { int actualRowIndex = rowIndex; if (isFilterEnabled()) { actualRowIndex = this.filterMapping[rowIndex]; } return delegateTableModel.getValueAt(actualRowIndex, columnIndex); } public int[] getFilterMapping() { return filterMapping; } public void setFilterMapping(int[] filterMapping) { this.filterMapping = filterMapping; } public TableModel getDelegateTableModel() { return delegateTableModel; } public void setDelegateTableModel(TableModel delegate) { this.delegateTableModel = delegate; } private void doFilter() { filterMatchCount = 0; int rowCount = delegateTableModel.getRowCount(); int[] tmpMapping = new int[rowCount]; for (int i = 0; i < rowCount; i++) { try { if (delegateTableModel.getValueAt(i, filterColumn) .toString().matches(this.filter)) { tmpMapping[filterMatchCount++] = i; } } catch (PatternSyntaxException patternSyntaxException) { filterMatchCount = 0; return; } } int[] currentFilterMapping = new int[filterMatchCount]; System.arraycopy(tmpMapping, 0, currentFilterMapping, 0, filterMatchCount); setFilterMapping(currentFilterMapping); } public int getFilterMatchCount() { return filterMatchCount; } public void setFilterMatchCount(int filterMatches) { this.filterMatchCount = filterMatches; } public String getFilter() { return filter; } public void setFilter(String filter) { this.filter = filter; } boolean isFilterEnabled() { return getFilter() != null && !"".equals(getFilter()); } public int getFilterColumn() { return filterColumn; } public void setFilterColumn(int filterColumn) { this.filterColumn = filterColumn; } } }
Gruss TomJava rocks!
How to become a good Java Programmer?
Does IT in Java and .Net
The only valid measurement of code quality: WTFs / minute
Blog
Xing
Twitter
-
Danke Dir Tom.
Jetzt funktioniert's so einigermaßen.
Vielen Dank!
Hab nur noch ein Problem:
bei der Methode:
Code :1 2 3 4 5 6 7 8 9 10 11
public void deleteRow(int selectedRow) { try { Method deleteRowMethod = delegateTableModel.getClass() .getDeclaredMethod("removeRow", new Class[]{int.class}); if (deleteRowMethod != null) { deleteRowMethod.invoke(delegateTableModel, new Object[]{Integer.valueOf(selectedRow)}); } } catch (Exception e) { throw new UnsupportedOperationException(); } }
kommt folgende Fehlermeldung:
The method valueOf(String, int) int the type Integer is not applicable for the arguments (int)
Was kann ich dagegen tun
-
09.12.05 09:51 #22
- Registriert seit
- Jun 2002
- Ort
- Saarbrücken (Saarland)
- Beiträge
- 9.886
- Blog-Einträge
- 29
Hallo!
Entweder Java 5 verwenden, oder den normalen Integer Konstruktor nehmen... new Integer(selectedRow); ...
Gruss TomJava rocks!
How to become a good Java Programmer?
Does IT in Java and .Net
The only valid measurement of code quality: WTFs / minute
Blog
Xing
Twitter
-
Achso!!
Danke für den Tip!
Da hätte man auch selbst draufkommen können
Ähnliche Themen
-
JTable Löschen [Alle Zeilen aufeinmal Löschen]
Von Fastkiller im Forum JavaAntworten: 6Letzter Beitrag: 14.02.08, 16:18 -
Zeilen-löschen!!
Von yidaki im Forum Visual Basic 6.0Antworten: 2Letzter Beitrag: 22.06.07, 15:11 -
Tabelle mit 1000 Zeilen in Tabelle mit x-Spalten umwandenl
Von carstenroll im Forum Relationale DatenbanksystemeAntworten: 2Letzter Beitrag: 02.05.06, 17:55 -
Alle Zeilen aus einer Tabelle löschen - unbekannte Zeilenzahl
Von Biergamasda im Forum Javascript & AjaxAntworten: 2Letzter Beitrag: 21.03.06, 18:31 -
mehrere zeilen von tabelle 1 in tabelle 2 verschieben
Von muhkuh im Forum Relationale DatenbanksystemeAntworten: 7Letzter Beitrag: 12.08.03, 01:15






Zitieren
Login





