/** * This method is activated on the Keystrokes we are listening to in this implementation. Here it * listens for Copy and Paste ActionCommands. Selections comprising non-adjacent cells result in * invalid selection and then copy action cannot be performed. Paste is done by aligning the upper * left corner of the selection with the 1st element in the current selection of the JTable. */ public void actionPerformed(ActionEvent e) { if (e.getActionCommand().compareTo("Copy") == 0) { StringBuffer sbf = new StringBuffer(); // Check to ensure we have selected only a contiguous block of // cells int numcols = jTable1.getSelectedColumnCount(); int numrows = jTable1.getSelectedRowCount(); int[] rowsselected = jTable1.getSelectedRows(); int[] colsselected = jTable1.getSelectedColumns(); if (!((numrows - 1 == rowsselected[rowsselected.length - 1] - rowsselected[0] && numrows == rowsselected.length) && (numcols - 1 == colsselected[colsselected.length - 1] - colsselected[0] && numcols == colsselected.length))) { JOptionPane.showMessageDialog( null, "Invalid Copy Selection", "Invalid Copy Selection", JOptionPane.ERROR_MESSAGE); return; } for (int i = 0; i < numrows; i++) { for (int j = 0; j < numcols; j++) { sbf.append(jTable1.getValueAt(rowsselected[i], colsselected[j])); if (j < numcols - 1) sbf.append("\t"); } sbf.append("\n"); } stsel = new StringSelection(sbf.toString()); system = Toolkit.getDefaultToolkit().getSystemClipboard(); system.setContents(stsel, stsel); } if (e.getActionCommand().compareTo("Paste") == 0) { System.out.println("Trying to Paste"); int startRow = (jTable1.getSelectedRows())[0]; int startCol = (jTable1.getSelectedColumns())[0]; try { String trstring = (String) (system.getContents(this).getTransferData(DataFlavor.stringFlavor)); System.out.println("String is:" + trstring); StringTokenizer st1 = new StringTokenizer(trstring, "\n"); for (int i = 0; st1.hasMoreTokens(); i++) { rowstring = st1.nextToken(); StringTokenizer st2 = new StringTokenizer(rowstring, "\t"); for (int j = 0; st2.hasMoreTokens(); j++) { value = (String) st2.nextToken(); if (startRow + i < jTable1.getRowCount() && startCol + j < jTable1.getColumnCount()) jTable1.setValueAt(value, startRow + i, startCol + j); System.out.println( "Putting " + value + "at row = " + startRow + i + "column = " + startCol + j); } } } catch (Exception ex) { ex.printStackTrace(); } } }
/** Invoked when removing selected reading list is required. */ private void onRemoveReadingList() { int[] rows = tblReadingLists.getSelectedRows(); boolean haveFeeds = false; for (int i = 0; !haveFeeds && i < rows.length; i++) { int row = rows[i]; haveFeeds = readingListsModel.getLists()[row].getFeeds().length > 0; } boolean delete = true; if (haveFeeds) { String msg = rows.length == 1 ? Strings.message("guide.dialog.readinglists.has.feeds") : Strings.message("guide.dialog.readinglists.have.feeds"); delete = JOptionPane.showConfirmDialog( this, msg, Strings.message("guide.dialog.delete.readinglist"), JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION; } if (delete) readingListsModel.removeRows(rows); }
private static void drawSelection(JTable table, int column, Graphics g, final int width) { int y = 0; final int[] rows = table.getSelectedRows(); final int height = table.getRowHeight(); for (int row : rows) { final TableCellRenderer renderer = table.getCellRenderer(row, column); final Component component = renderer.getTableCellRendererComponent( table, table.getValueAt(row, column), false, false, row, column); g.translate(0, y); component.setBounds(0, 0, width, height); boolean wasOpaque = false; if (component instanceof JComponent) { final JComponent j = (JComponent) component; if (j.isOpaque()) wasOpaque = true; j.setOpaque(false); } component.paint(g); if (wasOpaque) { ((JComponent) component).setOpaque(true); } y += height; g.translate(0, -y); } }
/** * Create a Transferable to use as the source for a data transfer. * * @param c The component holding the data to be transfered. This argument is provided to enable * sharing of TransferHandlers by multiple components. * @return The representation of the data to be transfered. */ protected Transferable createTransferable(JComponent c) { Object[] values = null; if (c instanceof JList) { values = ((JList) c).getSelectedValues(); } else if (c instanceof JTable) { JTable table = (JTable) c; int[] rows = table.getSelectedRows(); if (rows != null) { values = new Object[rows.length]; for (int i = 0; i < rows.length; i++) { values[i] = table.getValueAt(rows[i], 0); } } } if (values == null || values.length == 0) { return null; } StringBuffer plainBuf = new StringBuffer(); StringBuffer htmlBuf = new StringBuffer(); htmlBuf.append("<html>\n<body>\n<ul>\n"); for (Object obj : values) { String val = ((obj == null) ? "" : obj.toString()); plainBuf.append(val + "\n"); htmlBuf.append(" <li>" + val + "\n"); } // remove the last newline plainBuf.deleteCharAt(plainBuf.length() - 1); htmlBuf.append("</ul>\n</body>\n</html>"); return new FileTransferable(plainBuf.toString(), htmlBuf.toString(), values); }
private void removeSelectedParameters() { int[] selRows = parametersTable.getSelectedRows(); List<Parameter> parametersToRemove = new ArrayList<Parameter>(); for (int row : selRows) { parametersToRemove.add((Parameter) parametersTable.getValueAt(row, 0)); } removeParameters(parametersToRemove); }
protected void moveSelectedRow(JTable from, JTable to) { SimpleColorTableModel fromModel = (SimpleColorTableModel) from.getModel(); SimpleColorTableModel toModel = (SimpleColorTableModel) to.getModel(); for (int index : from.getSelectedRows()) { Vector rowValue = (Vector) fromModel.getDataVector().get(index); toModel.addRow(rowValue); } int selectedRow = -1; while ((selectedRow = from.getSelectedRow()) != -1) { fromModel.removeRow(selectedRow); } from.clearSelection(); }
/** Reaction to Add/Delete buttons. */ public void actionPerformed(ActionEvent e) { String cmd = e.getActionCommand(); if (cmdAdd.equals(cmd)) { Zone z = ((ControllerSWARM) controller).new Zone(); Vector<AbstractNetworkElement> ml = ((AbstractControllerComplex) controller).getMyMonitor().getPredecessors(); Vector<AbstractNetworkElement> cl = ((AbstractControllerComplex) controller).getMyMonitor().getSuccessors(); if (ml.size() > 0) z.bottleneck = (AbstractLinkHWC) ml.firstElement(); if (cl.size() > 0) { z.setFromOnramp((AbstractLinkHWC) cl.firstElement()); z.setToOnramp((AbstractLinkHWC) cl.firstElement()); } z.initialize(); zones.add(z); zoneTM.fireTableStructureChanged(); setUpBottleneckColumn(); setUpFromOnrampColumn(); setUpToOnrampColumn(); } if (cmdDelete.equals(cmd)) { try { int[] selected = zonetab.getSelectedRows(); if ((selected != null) && (selected.length > 0)) for (int i = 0; i < selected.length; i++) { int idx = selected[i] - i; if ((idx >= 0) && (idx < zones.size())) { zones.remove(idx); zoneTM.fireTableStructureChanged(); setUpBottleneckColumn(); setUpFromOnrampColumn(); setUpToOnrampColumn(); } } } catch (Exception ex) { } } return; }
public void end() { final int[] selected = table.getSelectedRows(); model.clear(rows.size()); Iterator i = rows.iterator(); while (i.hasNext()) { model.addEntry((Map) i.next()); } rows.clear(); if (SwingUtilities.isEventDispatchThread()) { SwingUtilities.invokeLater( new Runnable() { public void run() { model.fireListeners(); fixSelection(selected); } }); } else { model.fireListeners(); fixSelection(selected); } }