Example usage for javax.swing.table JTableHeader columnAtPoint

List of usage examples for javax.swing.table JTableHeader columnAtPoint

Introduction

In this page you can find the example usage for javax.swing.table JTableHeader columnAtPoint.

Prototype

public int columnAtPoint(Point point) 

Source Link

Document

Returns the index of the column that point lies in, or -1 if it lies out of bounds.

Usage

From source file:SortedTableModel.java

public void addHeaderListener() {
    table.getTableHeader().addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent event) {
            JTableHeader header = (JTableHeader) (event.getSource());
            int index = header.columnAtPoint(event.getPoint());
            Class dataType = table.getModel().getColumnClass(index);
            Class[] interfaces = dataType.getInterfaces();
            for (int i = 0; i < interfaces.length; i++) {
                if (interfaces[i].equals(java.lang.Comparable.class)) {
                    renderer.columnSelected(index);
                    break;
                }// w w  w . j  a  v a2 s.  c o  m
            }
            table.setColumnSelectionInterval(index, index);
        }
    });
}

From source file:com.mirth.connect.client.ui.components.MirthTreeTable.java

@Override
public void setSortable(boolean enable) {
    super.setSortable(enable);

    JTableHeader header = getTableHeader();
    if (enable) {
        if (treeTableSortAdapter == null) {
            treeTableSortAdapter = new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    if (SwingUtilities.isLeftMouseButton(e)) {
                        JTableHeader h = (JTableHeader) e.getSource();
                        TableColumnModel columnModel = getColumnModel();

                        int viewColumn = h.columnAtPoint(e.getPoint());
                        int column = columnModel.getColumn(viewColumn).getModelIndex();

                        if (column != -1) {
                            beforeSort();

                            // Toggle sort order (ascending <-> descending)
                            SortableTreeTableModel model = (SortableTreeTableModel) getTreeTableModel();
                            model.setColumnAndToggleSortOrder(column);

                            // Set sorting icon and current column index
                            ((SortableHeaderCellRenderer) getTableHeader().getDefaultRenderer())
                                    .setSortingIcon(model.getSortOrder());
                            ((SortableHeaderCellRenderer) getTableHeader().getDefaultRenderer())
                                    .setColumnIndex(column);

                            saveSortPreferences(column);

                            afterSort();
                        }//from   ww  w  .j a v a 2s . co  m
                    }
                }
            };
            header.addMouseListener(treeTableSortAdapter);
        }
    } else {
        if (treeTableSortAdapter != null) {
            header.removeMouseListener(treeTableSortAdapter);
            treeTableSortAdapter = null;
        }
    }
}

From source file:SortableTable.java

/**
 * Handle a mouse press event - if the user is NOT resizing a column and NOT dragging a column
 * then give visual feedback that the column header has been pressed.
 *
 * @param e  the mouse event./*from   w w  w. j a  v a2  s .  co m*/
 */
public void mousePressed(final MouseEvent e) {

    final JTableHeader header = (JTableHeader) e.getComponent();

    if (header.getResizingColumn() == null) { // resizing takes precedence over sorting
        if (header.getDraggedDistance() < 1) { // dragging also takes precedence over sorting
            final int columnIndex = header.columnAtPoint(e.getPoint());
            final int modelColumnIndex = header.getTable().convertColumnIndexToModel(columnIndex);
            if (this.model.isSortable(modelColumnIndex)) {
                this.sortColumnIndex = header.getTable().convertColumnIndexToModel(columnIndex);
                this.renderer.setPressedColumn(this.sortColumnIndex);
                header.repaint();
                if (header.getTable().isEditing()) {
                    header.getTable().getCellEditor().stopCellEditing();
                }
            } else {
                this.sortColumnIndex = -1;
            }
        }
    }

}

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

public TrialViewPanel(WindowOpener<JFrame> windowOpener, OfflineData od,
        Transformer<Trial, Boolean> checkIfEditorActive, Consumer<Trial> onTraitInstancesRemoved,
        MessagePrinter mp) {/*  w ww . j  a v  a2s .  c  om*/
    super(new BorderLayout());

    this.windowOpener = windowOpener;
    this.checkIfEditorActive = checkIfEditorActive;
    this.onTraitInstancesRemoved = onTraitInstancesRemoved;
    this.messagePrinter = mp;

    this.offlineData = od;
    this.offlineData.addOfflineDataChangeListener(offlineDataChangeListener);
    KdxploreDatabase db = offlineData.getKdxploreDatabase();
    if (db != null) {
        db.addEntityChangeListener(trialChangeListener);
    }

    trialDataTable.setTransferHandler(TableTransferHandler.initialiseForCopySelectAll(trialDataTable, true));
    trialPropertiesTable
            .setTransferHandler(TableTransferHandler.initialiseForCopySelectAll(trialPropertiesTable, true));

    // Note: Can't use renderers because the TM always returns String.class
    // for getColumnClass()
    // trialPropertiesTable.setDefaultRenderer(TrialLayout.class, new
    // TrialLayoutRenderer(trialPropertiesTableModel));
    // trialPropertiesTable.setDefaultRenderer(PlotIdentOption.class, new
    // PlotIdentOptionRenderer(trialPropertiesTableModel));

    trialPropertiesTableModel.addTableModelListener(new TableModelListener() {
        @Override
        public void tableChanged(TableModelEvent e) {
            if (trialPropertiesTableModel.getRowCount() > 0) {
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        GuiUtil.initialiseTableColumnWidths(trialPropertiesTable);
                    }
                });
                trialPropertiesTableModel.removeTableModelListener(this);
            }
        }
    });

    //      int tnsColumnIndex = -1;
    //      for (int col = trialPropertiesTableModel.getColumnCount(); --col >= 0; ) {
    //         if (TraitNameStyle.class == trialPropertiesTableModel.getColumnClass(col)) {
    //            tnsColumnIndex = col;
    //            break;
    //         }
    //      }

    editAction.setEnabled(false);
    trialPropertiesTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
        @Override
        public void valueChanged(ListSelectionEvent e) {
            if (!e.getValueIsAdjusting()) {
                int vrow = trialPropertiesTable.getSelectedRow();
                editAction.setEnabled(vrow >= 0 && trialPropertiesTableModel.isCellEditable(vrow, 1));
            }
        }
    });

    errorMessage.setForeground(Color.RED);
    Box top = Box.createHorizontalBox();
    top.add(errorMessage);
    top.add(Box.createHorizontalGlue());
    top.add(new JButton(editAction));

    JPanel main = new JPanel(new BorderLayout());
    main.add(new JScrollPane(trialPropertiesTable), BorderLayout.CENTER);
    main.add(legendPanel, BorderLayout.SOUTH);

    JScrollPane trialDataTableScrollPane = new JScrollPane(trialDataTable);

    // The preferred height of the viewport is determined
    // by whether or not we need to use hh:mm:ss in the name of any of
    // the scoring data sets.
    JViewport viewPort = new JViewport() {
        @Override
        public Dimension getPreferredSize() {
            Dimension d = super.getPreferredSize();
            d.height = 32;
            TableModel model = trialDataTable.getModel();
            if (model instanceof TrialData) {
                if (((TrialData) model).isUsingHMSformat()) {
                    d.height = 48;
                }
            }
            return d;
        }
    };
    trialDataTableScrollPane.setColumnHeader(viewPort);

    JTableHeader th = trialDataTable.getTableHeader();
    th.setDefaultRenderer(trialDataTableHeaderRenderer);
    th.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            int column = th.columnAtPoint(e.getPoint());
            trialDataTableHeaderRenderer.columnSelected = column;
            boolean shifted = 0 != (MouseEvent.SHIFT_MASK & e.getModifiers());
            boolean right = SwingUtilities.isRightMouseButton(e);
            updateDeleteSamplesAction(shifted, right);
            e.consume();
        }
    });

    trialDataTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
        @Override
        public void valueChanged(ListSelectionEvent e) {
            if (!e.getValueIsAdjusting()) {
                removeTraitInstancesAction.setEnabled(trialDataTable.getSelectedRowCount() > 0);
            }
        }
    });
    removeTraitInstancesAction.setEnabled(false);

    KDClientUtils.initAction(ImageId.PLUS_BLUE_24, addSampleGroupAction, Msg.TOOLTIP_ADD_SAMPLES_FOR_SCORING());
    KDClientUtils.initAction(ImageId.TRASH_24, deleteSamplesAction, Msg.TOOLTIP_DELETE_COLLECTED_SAMPLES());
    KDClientUtils.initAction(ImageId.EXPORT_24, exportSamplesAction, Msg.TOOLTIP_EXPORT_SAMPLES_OR_TRAITS());
    KDClientUtils.initAction(ImageId.MINUS_GOLD_24, removeTraitInstancesAction,
            Msg.TOOLTIP_REMOVE_TRAIT_INSTANCES_WITH_NO_DATA());

    JPanel trialDataPanel = new JPanel(new BorderLayout());
    Box buttons = Box.createHorizontalBox();

    buttons.add(new JButton(removeTraitInstancesAction));
    buttons.add(Box.createHorizontalGlue());
    buttons.add(new JButton(exportSamplesAction));
    buttons.add(Box.createHorizontalGlue());
    buttons.add(new JButton(addSampleGroupAction));
    buttons.add(Box.createHorizontalStrut(8));
    buttons.add(new JButton(deleteSamplesAction));
    trialDataPanel.add(GuiUtil.createLabelSeparator("Measurements by Source", buttons), BorderLayout.NORTH);
    trialDataPanel.add(trialDataTableScrollPane, BorderLayout.CENTER);

    JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, main, trialDataPanel);
    splitPane.setResizeWeight(0.5);

    add(top, BorderLayout.NORTH);
    add(splitPane, BorderLayout.CENTER);

    trialDataTable.setDefaultRenderer(Object.class, new TrialDataCellRenderer());

    trialDataTable.addPropertyChangeListener("model", new PropertyChangeListener() {
        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            trialDataTableHeaderRenderer.columnSelected = -1;
            updateDeleteSamplesAction(false, false);
        }
    });
}

From source file:net.sourceforge.pmd.cpd.GUI.java

private JComponent makeMatchList() {

    resultsTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
        @Override//  ww  w . ja v  a 2s . co  m
        public void valueChanged(ListSelectionEvent e) {
            populateResultArea();
        }
    });

    resultsTable.registerKeyboardAction(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            copyMatchListSelectionsToClipboard();
        }
    }, "Copy", COPY_KEY_STROKE, JComponent.WHEN_FOCUSED);

    resultsTable.registerKeyboardAction(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            deleteMatchlistSelections();
        }
    }, "Del", DELETE_KEY_STROKE, JComponent.WHEN_FOCUSED);

    int[] alignments = new int[matchColumns.length];
    for (int i = 0; i < alignments.length; i++) {
        alignments[i] = matchColumns[i].alignment();
    }

    resultsTable.setDefaultRenderer(Object.class, new AlignmentRenderer(alignments));

    final JTableHeader header = resultsTable.getTableHeader();
    header.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            sortOnColumn(header.columnAtPoint(new Point(e.getX(), e.getY())));
        }
    });

    return new JScrollPane(resultsTable);
}