Example usage for javax.swing JTable setTransferHandler

List of usage examples for javax.swing JTable setTransferHandler

Introduction

In this page you can find the example usage for javax.swing JTable setTransferHandler.

Prototype

@BeanProperty(hidden = true, description = "Mechanism for transfer of data to and from the component")
public void setTransferHandler(TransferHandler newHandler) 

Source Link

Document

Sets the TransferHandler , which provides support for transfer of data into and out of this component via cut/copy/paste and drag and drop.

Usage

From source file:ExtendedDnDDemo.java

private JPanel createTable() {
    DefaultTableModel model = new DefaultTableModel();

    model.addColumn("Column 0");
    model.addColumn("Column 1");
    model.addColumn("Column 2");
    model.addColumn("Column 3");

    model.addRow(new String[] { "Table 00", "Table 01", "Table 02", "Table 03" });
    model.addRow(new String[] { "Table 10", "Table 11", "Table 12", "Table 13" });
    model.addRow(new String[] { "Table 20", "Table 21", "Table 22", "Table 23" });
    model.addRow(new String[] { "Table 30", "Table 31", "Table 32", "Table 33" });

    JTable table = new JTable(model);
    table.getTableHeader().setReorderingAllowed(false);
    table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);

    JScrollPane scrollPane = new JScrollPane(table);
    scrollPane.setPreferredSize(new Dimension(400, 100));

    table.setDragEnabled(true);/*from w ww  . ja  va  2  s .  co m*/
    table.setTransferHandler(new TableTransferHandler());

    JPanel panel = new JPanel(new BorderLayout());
    panel.add(scrollPane, BorderLayout.CENTER);
    panel.setBorder(BorderFactory.createTitledBorder("Table"));
    return panel;
}

From source file:com.diversityarrays.kdxplore.trials.SampleGroupViewer.java

private SampleGroupViewer(String title, KdxploreDatabase kdxdb, Trial trial, SampleGroup sampleGroup) {
    super(new BorderLayout());

    this.title = title;
    //        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    this.kdxdb = kdxdb;
    this.trial = trial;
    this.sampleGroup = sampleGroup;

    initialise();/* w ww  .j a  v  a  2  s .  co m*/
    if (plotInfoByPlotId.isEmpty()) {
        add(new JLabel("No Plots available"), BorderLayout.CENTER);
    } else {
        tiChoiceTableModel = new TraitInstanceChoiceTableModel();
        JTable tiTable = new JTable(tiChoiceTableModel);
        tiTable.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (SwingUtilities.isRightMouseButton(e)) {
                    List<Integer> modelRows = GuiUtil.getSelectedModelRows(tiTable);
                    if (modelRows.isEmpty()) {
                        Point pt = e.getPoint();
                        int vrow = tiTable.rowAtPoint(pt);
                        if (vrow >= 0) {
                            int mrow = tiTable.convertRowIndexToModel(vrow);
                            if (mrow >= 0) {
                                showPopupMenu(tiTable, pt, Arrays.asList(Integer.valueOf(mrow)));
                            }
                        }
                    } else {
                        showPopupMenu(tiTable, e.getPoint(), modelRows);
                    }
                }
            }
        });

        tableModel = new DataTableModel(plotInfoByPlotId);
        JTable table = new JTable(tableModel);
        DefaultTableCellRenderer r = new DefaultTableCellRenderer();
        r.setHorizontalAlignment(SwingConstants.CENTER);
        table.setDefaultRenderer(String.class, r);

        splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, new JScrollPane(tiTable),
                new JScrollPane(table));
        splitPane.setResizeWeight(0.2);
        add(splitPane, BorderLayout.CENTER);
        table.setTransferHandler(TableTransferHandler.initialiseForCopySelectAll(table, true));
    }
}

From source file:fi.hoski.remote.ui.Admin.java

private void editReservations(final EventType eventType) {
    final Event event = chooseEvent(eventType, "CHOOSE");
    if (event != null) {
        final String eventTitle = TextUtil.getText(event.getEventType().name()) + " "
                + event.get(Event.EventDate);
        safeTitle = frame.getTitle();//from  w  w w  .ja v  a  2s . com
        frame.setTitle(eventTitle);
        reservationList = dss.getReservations(event);
        selectedReservations = new ArrayList<Reservation>();
        unSelectedReservations = new ArrayList<Reservation>();
        if (Event.isInspection(eventType)) {
            for (Reservation reservation : reservationList) {
                Boolean inspected = (Boolean) reservation.get(Reservation.INSPECTED);
                if (inspected != null && inspected) {
                    selectedReservations.add(reservation);
                } else {
                    unSelectedReservations.add(reservation);
                }
            }
        } else {
            for (Reservation reservation : reservationList) {
                Long order = (Long) reservation.get(Reservation.ORDER);
                if (order != null && order != 0) {
                    selectedReservations.add(reservation);
                } else {
                    unSelectedReservations.add(reservation);
                }
            }
        }
        DataObjectModel baseModel = Reservation.getModel(eventType);
        DataObjectModel unorderedModel = null;
        DataObjectModel orderedModel = null;
        switch (eventType) {
        case LAUNCH:
        case LIFT:
        case HULL_INSPECTION:
            unorderedModel = baseModel.hide(Reservation.BOAT, Reservation.INSPECTED, Reservation.CREATOR);
            orderedModel = baseModel.hide(Reservation.BOAT, Reservation.INSPECTED, Reservation.CREATOR);
            break;
        case INSPECTION:
            unorderedModel = baseModel.hide(Reservation.BOAT, Reservation.INSPECTED, Reservation.CREATOR,
                    Reservation.EMAIL, Reservation.MOBILEPHONE, Reservation.DOCK, Reservation.DOCKNUMBER,
                    Reservation.INSPECTION_GASS, Reservation.INSPECTOR);
            orderedModel = baseModel.hide(Reservation.BOAT, Reservation.INSPECTED, Reservation.CREATOR,
                    Reservation.EMAIL, Reservation.MOBILEPHONE, Reservation.DOCK, Reservation.DOCKNUMBER,
                    Reservation.NOTES);
            break;
        }

        final DataObjectListTableModel<Reservation> unorderedTableModel = new DataObjectListTableModel<Reservation>(
                unorderedModel, unSelectedReservations);
        final JTable unorderedtable = new FitTable(unorderedTableModel);
        TableSelectionHandler tsh1 = new TableSelectionHandler(unorderedtable);
        unorderedtable.setDefaultRenderer(String.class, new StringTableCellRenderer());
        unorderedtable.setDefaultRenderer(Text.class, new TextTableCellRenderer());

        unorderedtable.addKeyListener(unorderedTableModel.getRemover(dss));
        unorderedtable.setDragEnabled(true);
        unorderedtable.setDropMode(DropMode.INSERT_ROWS);
        TransferHandler unorderedTransferHandler = new DataObjectTransferHandler<Reservation>(
                unorderedTableModel);
        unorderedtable.setTransferHandler(unorderedTransferHandler);

        final DataObjectListTableModel<Reservation> orderedTableModel = new DataObjectListTableModel<Reservation>(
                orderedModel, selectedReservations);
        orderedTableModel.setEditable(Reservation.INSPECTION_CLASS, Reservation.INSPECTION_GASS,
                Reservation.BASICINSPECTION, Reservation.INSPECTOR);
        final JTable orderedtable = new FitTable(orderedTableModel);
        TableSelectionHandler tsh2 = new TableSelectionHandler(orderedtable);
        orderedtable.setDefaultRenderer(String.class, new StringTableCellRenderer());
        orderedtable.setDefaultRenderer(Text.class, new TextTableCellRenderer());

        orderedtable.addKeyListener(orderedTableModel.getRemover(dss));
        orderedtable.setDragEnabled(true);
        orderedtable.setDropMode(DropMode.INSERT_ROWS);
        TransferHandler orderedTransferHandler = new DataObjectTransferHandler<Reservation>(orderedTableModel);
        orderedtable.setTransferHandler(orderedTransferHandler);
        if (Event.isInspection(eventType)) {
            unorderedtable.setAutoCreateRowSorter(true);
            orderedtable.setAutoCreateRowSorter(true);
        }
        leftPane = new JScrollPane();
        leftPane.setViewport(new InfoViewport(TextUtil.getText(eventType.name() + "-leftPane")));
        leftPane.setViewportView(unorderedtable);
        leftPane.setTransferHandler(unorderedTransferHandler);

        rightPane = new JScrollPane();
        rightPane.setViewport(new InfoViewport(TextUtil.getText(eventType.name() + "-rightPane")));
        rightPane.setViewportView(orderedtable);
        rightPane.setTransferHandler(orderedTransferHandler);

        splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPane, rightPane);
        splitPane.setDividerLocation(0.5);
        menuReservation.setEnabled(false);
        safeContainer = frame.getContentPane();
        JPanel contentPane = new JPanel(new BorderLayout());
        contentPane.add(splitPane, BorderLayout.CENTER);

        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout());
        contentPane.add(buttonPanel, BorderLayout.SOUTH);

        JButton saveButton = new JButton();
        TextUtil.populate(saveButton, "SAVE");
        saveButton.setEnabled(!Event.isInspection(eventType));
        ActionListener saveAction = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                saveReservations();
            }
        };
        saveButton.addActionListener(saveAction);
        buttonPanel.add(saveButton);

        switch (eventType) {
        case INSPECTION: {
            if (privileged) {
                JButton inspectButton = new JButton();
                TextUtil.populate(inspectButton, "SET INSPECTED");
                ActionListener inspectAction = new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        TableCellEditor cellEditor = orderedtable.getCellEditor();
                        if (cellEditor != null) {
                            cellEditor.stopCellEditing();
                        }
                        try {
                            setAsInspected();
                        } catch (SQLException | ClassNotFoundException ex) {
                            ex.printStackTrace();
                            JOptionPane.showMessageDialog(null, ex.getMessage());
                        }
                    }
                };
                inspectButton.addActionListener(inspectAction);
                buttonPanel.add(inspectButton);
            }

            JButton addBoat = new JButton();
            TextUtil.populate(addBoat, "ADD BOAT");
            ActionListener addBoatAction = new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    Reservation reservation = reservate(eventType, event);
                    if (reservation != null) {
                        reservationList.add(reservation);
                        unSelectedReservations.add(reservation);
                        unorderedTableModel.fireTableDataChanged();
                    }
                }
            };
            addBoat.addActionListener(addBoatAction);
            buttonPanel.add(addBoat);

            JButton printTypeButton = new JButton();
            TextUtil.populate(printTypeButton, "PRINT");
            ActionListener printTypeAction = new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    try {
                        printNameBoatTypeOrder(eventTitle);
                    } catch (PrinterException ex) {
                        ex.printStackTrace();
                        JOptionPane.showMessageDialog(null, ex.getMessage());
                    }
                }
            };
            printTypeButton.addActionListener(printTypeAction);
            buttonPanel.add(printTypeButton);

            JButton printDockButton = new JButton();
            TextUtil.populate(printDockButton, "PRINT DOCK ORDER");
            ActionListener printDockAction = new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    try {
                        printDockOrder(eventTitle);
                    } catch (PrinterException ex) {
                        ex.printStackTrace();
                        JOptionPane.showMessageDialog(null, ex.getMessage());
                    }
                }
            };
            printDockButton.addActionListener(printDockAction);
            buttonPanel.add(printDockButton);

        }
            break;
        case HULL_INSPECTION: {
            JButton print = new JButton();
            TextUtil.populate(print, "PRINT");
            ActionListener printAction = new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    try {
                        printAlphaOrder(eventTitle);
                    } catch (PrinterException ex) {
                        ex.printStackTrace();
                        JOptionPane.showMessageDialog(null, ex.getMessage());
                    }
                }
            };
            print.addActionListener(printAction);
            buttonPanel.add(print);

        }
            break;
        case LAUNCH:
        case LIFT: {
            JButton addBoat = new JButton();
            TextUtil.populate(addBoat, "ADD BOAT");
            ActionListener addBoatAction = new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    Reservation reservation = reservate(eventType, event);
                    if (reservation != null) {
                        reservationList.add(reservation);
                        unSelectedReservations.add(reservation);
                        unorderedTableModel.fireTableDataChanged();
                    }
                }
            };
            addBoat.addActionListener(addBoatAction);
            buttonPanel.add(addBoat);

            JButton printBrief = new JButton();
            TextUtil.populate(printBrief, "BRIEF PRINT");
            ActionListener printBriefAction = new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    try {
                        printOrderBrief(eventTitle);
                    } catch (PrinterException ex) {
                        ex.printStackTrace();
                        JOptionPane.showMessageDialog(null, ex.getMessage());
                    }
                }
            };
            printBrief.addActionListener(printBriefAction);
            buttonPanel.add(printBrief);

            JButton print = new JButton();
            TextUtil.populate(print, "PRINT");
            ActionListener printAction = new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    try {
                        if (eventType == EventType.LAUNCH) {
                            printLaunchOrder(eventTitle);
                        } else {
                            printLiftOrder(eventTitle);
                        }
                    } catch (PrinterException ex) {
                        ex.printStackTrace();
                        JOptionPane.showMessageDialog(null, ex.getMessage());
                    }
                }
            };
            print.addActionListener(printAction);
            buttonPanel.add(print);
        }
            break;
        }

        JButton cancelButton = new JButton();
        TextUtil.populate(cancelButton, "CANCEL");
        ActionListener cancelAction = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                cancel();
            }
        };
        cancelButton.addActionListener(cancelAction);
        buttonPanel.add(cancelButton);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setVisible(true);
        KeyListener keyListener = new KeyAdapter() {

            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == 10) {
                    int selectedRow = unorderedtable.getSelectedRow();
                    if (selectedRow != -1) {
                        RowSorter<? extends TableModel> rowSorter = unorderedtable.getRowSorter();
                        if (rowSorter != null) {
                            selectedRow = rowSorter.convertRowIndexToModel(selectedRow);
                        }
                        Reservation reservation = unorderedTableModel.getObject(selectedRow);
                        orderedTableModel.add(reservation);
                        unorderedTableModel.remove(reservation);
                        e.consume();
                        unorderedtable.changeSelection(selectedRow, 0, false, false);
                    }
                }
            }
        };
        unorderedtable.addKeyListener(keyListener);
        unorderedtable.requestFocusInWindow();
    }
}

From source file:org.nuclos.client.genericobject.GenericObjectCollectController.java

private void prepareTableForDragAndDrop(final JTable tbl) {
    tbl.setDragEnabled(true);//ww  w . j  ava  2 s.com
    tbl.setTransferHandler(new TransferHandler() {

        @Override
        public int getSourceActions(JComponent comp) {
            int result = NONE;
            if (comp == tbl)
                if (GenericObjectCollectController.this.getSelectedCollectable() != null)
                    result = COPY;
            return result;
        }

        @Override
        protected Transferable createTransferable(JComponent comp) {
            Transferable result = null;
            if (comp == tbl) {
                final int[] aiSelectedRows = tbl.getSelectedRows();

                final List<GenericObjectIdModuleProcess> lstgoimp = new ArrayList<GenericObjectIdModuleProcess>(
                        aiSelectedRows.length);
                for (int iSelectedRow : aiSelectedRows) {
                    final CollectableGenericObjectWithDependants clct = getResultTableModel()
                            .getCollectable(iSelectedRow);
                    GenericObjectIdModuleProcess transferableObject = getTransferableObject(tbl, iSelectedRow,
                            clct);
                    lstgoimp.add(transferableObject);
                }
                if (!lstgoimp.isEmpty())
                    result = new TransferableGenericObjects(lstgoimp);
            }
            return result;
        }

        private GenericObjectIdModuleProcess getTransferableObject(final JTable tbl, int iSelectedRow,
                final CollectableGenericObjectWithDependants clct) {
            return new GenericObjectIdModuleProcess(clct, getTreeViewIdentifier(clct),
                    getContents(tbl, iSelectedRow));
        }

        private String getContents(JTable tbl, int iRow) {
            final StringBuffer sb = new StringBuffer();
            final int iColumnCount = tbl.getColumnCount();

            for (int iColumn = 0; iColumn < iColumnCount; iColumn++) {
                final String sValue = tbl.getValueAt(iRow, iColumn).toString();
                if (sValue.indexOf("\n") >= 0 || sValue.indexOf("\t") >= 0) {
                    sb.append("\"");
                    sb.append(sValue.replaceAll("\"", "\"\""));
                    sb.append("\"");
                } else {
                    sb.append(sValue);
                }
                if (iColumn < iColumnCount - 1)
                    sb.append("\t");
            }
            return sb.toString();
        }
    });
}

From source file:org.simmi.GeneSetHead.java

License:asdf

public Set<String> getSelspec(Component comp, final List<String> specs, final JCheckBox... contigs) {
    final JTable table = new JTable();
    table.setDragEnabled(true);//from   ww w . j  a  v  a2  s. c om
    JScrollPane scroll = new JScrollPane(table);
    table.setAutoCreateRowSorter(true);

    final List<Sequence> ctgs = new ArrayList<Sequence>(geneset.contigmap.values());
    final TableModel contigmodel = new TableModel() {
        @Override
        public int getRowCount() {
            return ctgs.size();
        }

        @Override
        public int getColumnCount() {
            return 1;
        }

        @Override
        public String getColumnName(int columnIndex) {
            return "Contigs/Scaffolds";
        }

        @Override
        public Class<?> getColumnClass(int columnIndex) {
            return String.class;
        }

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return false;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            return ctgs.get(rowIndex).toString();
        }

        @Override
        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        }

        @Override
        public void addTableModelListener(TableModelListener l) {
        }

        @Override
        public void removeTableModelListener(TableModelListener l) {
        }
    };
    final TableModel specmodel = new TableModel() {
        @Override
        public int getRowCount() {
            return specs.size();
        }

        @Override
        public int getColumnCount() {
            return 2;
        }

        @Override
        public String getColumnName(int columnIndex) {
            return columnIndex == 0 ? "Id" : "Spec";
        }

        @Override
        public Class<?> getColumnClass(int columnIndex) {
            return String.class;
        }

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return false;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            if (columnIndex == 1)
                return Sequence.nameFix(specs.get(rowIndex), true);
            else
                return specs.get(rowIndex);
        }

        @Override
        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        }

        @Override
        public void addTableModelListener(TableModelListener l) {
        }

        @Override
        public void removeTableModelListener(TableModelListener l) {
        }
    };
    table.setModel(specmodel);

    TransferHandler th = dragRows(table, specs);
    table.setTransferHandler(th);

    if (contigs != null && contigs.length > 0 && !contigs[0].getText().equals("Plasmid"))
        contigs[0].addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                if (contigs[0] != null && contigs[0].isSelected())
                    table.setModel(contigmodel);
                else
                    table.setModel(specmodel);
            }
        });

    Object[] ctls = new Object[] { scroll, contigs };
    //Object[] ctls2 = new Object[] { scroll };

    //if( contigs != null ) 
    JOptionPane.showMessageDialog(comp, ctls);
    //else JOptionPane.showMessageDialog( comp, ctls2 );
    Set<String> selspec = new LinkedHashSet<>();
    for (int i = 0; i < table.getRowCount(); i++) {
        if (table.isRowSelected(i)) {
            String spec = (String) table.getValueAt(i, 0);
            System.err.println("test " + spec);
            selspec.add(spec);
        }
    }
    return selspec;
}