/** * 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); }
/** * 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); }
public void selectionChanged() { int[] selRows = dataTable.getSelectedRows(); boolean hasSelection = (selRows != null && selRows.length != 0); removeAction.setEnabled(hasSelection); frame.setRemoveActionEnabled(this, hasSelection); }
public void valueChanged(ListSelectionEvent e) { int maxRows; int[] selRows; Object value; if (!e.getValueIsAdjusting()) { selRows = aTable.getSelectedRows(); if (selRows.length > 0) { for (int i = 0; i < 3; i++) { // get Table data TableModel tm = aTable.getModel(); value = tm.getValueAt(selRows[0], i); System.out.println("Selection : " + value); } System.out.println(); } } }
private String extractContent( DataGridPanel grid, String delimiter, boolean saveHeader, boolean ecloseWithDowbleQuotes) { JTable _table = grid.getTable(); int numcols = _table.getSelectedColumnCount(); int numrows = _table.getSelectedRowCount(); if (numcols > 0 && numrows > 0) { StringBuffer sbf = new StringBuffer(); int[] rowsselected = _table.getSelectedRows(); int[] colsselected = _table.getSelectedColumns(); if (saveHeader) { // put header name list for (int j = 0; j < numcols; j++) { String text = (String) _table .getTableHeader() .getColumnModel() .getColumn(colsselected[j]) .getHeaderValue(); sbf.append(text); if (j < numcols - 1) sbf.append(delimiter); } sbf.append("\n"); } // put content for (int i = 0; i < numrows; i++) { for (int j = 0; j < numcols; j++) { Object value = _table.getValueAt(rowsselected[i], colsselected[j]); String _value = ""; if (value != null) { _value = value.toString(); } if (ecloseWithDowbleQuotes) { sbf.append("\"").append(_value).append("\""); } else { sbf.append(_value); } if (j < numcols - 1) sbf.append(delimiter); } sbf.append("\n"); } // StringSelection stsel = new StringSelection(sbf.toString()); // Clipboard system = Toolkit.getDefaultToolkit().getSystemClipboard(); // system.setContents(stsel, stsel); return sbf.toString(); } return null; }
public void delete() { int[] selRows = dataTable.getSelectedRows(); Set<InputFile> dataToRemove = new HashSet<InputFile>(); for (int row : selRows) { dataToRemove.add(document.getInputFiles().get(row)); } // TODO: would probably be a good idea to check if the user wants to remove the last file document.getInputFiles().removeAll(dataToRemove); document.fireDataChanged(); if (document.getInputFiles().size() == 0) { // all data partitions removed so reset the taxa frame.setStatusMessage("No data loaded"); } }
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); } }
private void delete() { int[] selectedRows = setTable.getSelectedRows(); for (int i = selectedRows.length - 1; i >= 0; --i) { setTableModel.deleteAt(selectedRows[i]); } }