Example usage for javax.swing JTable addMouseListener

List of usage examples for javax.swing JTable addMouseListener

Introduction

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

Prototype

public synchronized void addMouseListener(MouseListener l) 

Source Link

Document

Adds the specified mouse listener to receive mouse events from this component.

Usage

From source file:components.SimpleTableDemo.java

public SimpleTableDemo() {
    super(new GridLayout(1, 0));

    String[] columnNames = { "First Name", "Last Name", "Sport", "# of Years", "Vegetarian" };

    Object[][] data = { { "Kathy", "Smith", "Snowboarding", new Integer(5), new Boolean(false) },
            { "John", "Doe", "Rowing", new Integer(3), new Boolean(true) },
            { "Sue", "Black", "Knitting", new Integer(2), new Boolean(false) },
            { "Jane", "White", "Speed reading", new Integer(20), new Boolean(true) },
            { "Joe", "Brown", "Pool", new Integer(10), new Boolean(false) } };

    final JTable table = new JTable(data, columnNames);
    table.setPreferredScrollableViewportSize(new Dimension(500, 70));
    table.setFillsViewportHeight(true);//from ww  w . j a  v  a2 s . com

    if (DEBUG) {
        table.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                printDebugData(table);
            }
        });
    }

    //Create the scroll pane and add the table to it.
    JScrollPane scrollPane = new JScrollPane(table);

    //Add the scroll pane to this panel.
    add(scrollPane);
}

From source file:net.sf.housekeeper.swing.item.ItemsView.java

private void configureTable(JTable table) {
    table.getSelectionModel().addListSelectionListener(new TableSelectionListener());
    table.addMouseListener(new PopupTriggerListener(createContextMenu()));
    table.addMouseListener(new DoubleClickListener());
}

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();/*from  w w  w.jav  a  2s .  c  o 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:org.drugis.addis.gui.builder.NetworkMetaAnalysisView.java

/**
 * Make table of results (Cipriani et al., Lancet(2009), fig. 3, pp752).
 * @param mtc Model for which to display results.
 * @return A TablePanel//from   w  w  w  . java 2 s . c o m
 */
private TablePanel createNetworkTablePanel(final MTCModelWrapper<TreatmentDefinition> mtc) {
    NetworkRelativeEffectTableModel tableModel = NetworkRelativeEffectTableModel.build(d_pm.getAlternatives(),
            mtc);
    final JTable table = new JTable(tableModel);
    final NetworkRelativeEffectTableCellRenderer renderer = new NetworkRelativeEffectTableCellRenderer(
            !d_pm.isContinuous());
    table.setDefaultRenderer(Object.class, renderer);
    table.setTableHeader(null);

    table.addMouseListener(treatmentCategorizationListener(renderer));

    setColumnWidths(table);
    TableCopyHandler.registerCopyAction(table);
    return new TablePanel(table);
}

From source file:uk.ac.lkl.cram.ui.ModuleFrame.java

private JXTaskPane createLineItemPane() {
    JXTaskPane lineItemPane = new JXTaskPane();
    lineItemPane.setTitle("Student Hours");
    lineItemPane.setScrollOnExpand(true);
    LineItemPanel lineItemPanel = new LineItemPanel(module, sharedSelectionModel);
    JTable table = lineItemPanel.getTable();
    table.addMouseListener(doubleClickListener);
    lineItemPane.add(lineItemPanel);//from   ww  w .  j  a v  a2s. co  m
    return lineItemPane;
}

From source file:uk.ac.lkl.cram.ui.ModuleFrame.java

private JXTaskPane createTutorCostPane() {
    JXTaskPane tutorCostPane = new JXTaskPane();
    tutorCostPane.setScrollOnExpand(true);
    tutorCostPane.setCollapsed(true);//  www .  ja  v a  2  s  . c o m
    tutorCostPane.setTitle("Tutor Costs");
    TutorCostPanel tutorCostPanel = new TutorCostPanel(module, sharedSelectionModel);
    JTable table = tutorCostPanel.getTable();
    table.addMouseListener(doubleClickListener);
    tutorCostPane.add(tutorCostPanel);
    return tutorCostPane;
}

From source file:uk.ac.lkl.cram.ui.ModuleFrame.java

private JXTaskPane createTutorHoursPane() {
    JXTaskPane tutorHoursPane = new JXTaskPane();
    tutorHoursPane.setScrollOnExpand(true);
    tutorHoursPane.setCollapsed(true);//ww  w .  java  2  s . c  om
    tutorHoursPane.setTitle("Tutor Hours");
    TutorHoursPanel tutorHoursPanel = new TutorHoursPanel(module, sharedSelectionModel);
    JTable table = tutorHoursPanel.getTable();
    table.addMouseListener(doubleClickListener);
    tutorHoursPane.add(tutorHoursPanel);
    return tutorHoursPane;
}

From source file:com.univocity.app.swing.DataAnalysisWindow.java

private void addPopupMenuToTable(DaoTable daoTable, boolean isSourceTable) {
    String engineName = isSourceTable ? config.getSourceEngineName() : config.getDestinationEngineName();

    if (engineName == null) {
        return;//from   w  w  w.j  a  va2  s  .  c  o  m
    }

    final JTable table = daoTable.getDataTable();
    final JPopupMenu menu = new JPopupMenu();
    menu.add(newJMenuItem("Disable updates", daoTable, engineName, false, false));
    menu.add(newJMenuItem("Enable updates", daoTable, engineName, true, false));
    menu.add(new JSeparator());
    menu.add(newJMenuItem("Disable updates on all rows", daoTable, engineName, false, true));
    menu.add(newJMenuItem("Enable updates on all rows", daoTable, engineName, true, true));

    table.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            if (e.getButton() == MouseEvent.BUTTON3) {
                int rowNumber = table.rowAtPoint(e.getPoint());
                if (rowNumber != -1) {
                    table.getSelectionModel().setSelectionInterval(rowNumber, rowNumber);
                    menu.show(table, e.getX(), e.getY());
                }
            }
        };
    });
}

From source file:fll.subjective.SubjectiveFrame.java

/**
 * Show differences./*from  w  w  w. j a v  a2 s.  c o m*/
 */
private void showDifferencesDialog(final Collection<SubjectiveScoreDifference> diffs) {
    final SubjectiveDiffTableModel model = new SubjectiveDiffTableModel(diffs);
    final JTable table = new JTable(model);
    table.setGridColor(Color.BLACK);

    table.addMouseListener(new MouseAdapter() {
        public void mouseClicked(final MouseEvent e) {
            if (e.getClickCount() == 2) {
                final JTable target = (JTable) e.getSource();
                final int row = target.getSelectedRow();

                final SubjectiveScoreDifference diff = model.getDiffForRow(row);

                // find correct table
                final String category = diff.getCategory();
                final JTable scoreTable = getTableForTitle(category);
                final int tabIndex = getTabIndexForCategory(category);
                getTabbedPane().setSelectedIndex(tabIndex);

                // get correct row and column
                final SubjectiveTableModel model = (SubjectiveTableModel) scoreTable.getModel();
                final int scoreRow = model.getRowForTeamAndJudge(diff.getTeamNumber(), diff.getJudge());
                final int scoreCol = model.getColForSubcategory(diff.getSubcategory());
                if (scoreRow == -1 || scoreCol == -1) {
                    throw new FLLRuntimeException(
                            "Internal error: Cannot find correct row and column for score difference: " + diff);
                }

                scoreTable.changeSelection(scoreRow, scoreCol, false, false);
            }
        }
    });

    final JDialog dialog = new JDialog(this, false);
    final Container cpane = dialog.getContentPane();
    cpane.setLayout(new BorderLayout());
    final JScrollPane tableScroller = new JScrollPane(table);
    cpane.add(tableScroller, BorderLayout.CENTER);

    dialog.pack();
    dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    dialog.setVisible(true);
}

From source file:com.emr.schemas.ButtonColumn.java

/**
 *  Create the ButtonColumn to be used as a renderer and editor. The
 *  renderer and editor will automatically be installed on the TableColumn
 *  of the specified column.// w  w  w .j a v a  2  s.c o  m
 *
 *  @param table the table containing the button renderer/editor
 *  @param action the Action to be invoked when the button is invoked
 *  @param column the column to which the button renderer/editor is added
 */
public ButtonColumn(JTable table, Action action, int column, String btnText) {
    this.table = table;
    this.action = action;
    this.btnText = btnText;

    renderButton = new JButton();
    editButton = new JButton();
    editButton.setFocusPainted(false);
    editButton.addActionListener(this);
    originalBorder = editButton.getBorder();
    setFocusBorder(new LineBorder(Color.BLUE));

    TableColumnModel columnModel = table.getColumnModel();
    columnModel.getColumn(column).setCellRenderer(this);
    columnModel.getColumn(column).setCellEditor(this);
    table.addMouseListener(this);
}