Java Code Examples for javax.swing.JTable#getCellRect()

The following examples show how to use javax.swing.JTable#getCellRect() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ComboBoxTableCellEditor.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**********************************************************************
    *  Is the cell editable? If the mouse was pressed at a margin
    *  we don't want the cell to be editable.
    *
    *  @param  evt  The event-object.
    *
    *  @interfaceMethod javax.swing.table.TableCellEditor
    *********************************************************************/

public boolean isCellEditable (EventObject evt)
{

       this.startEditingEvent = evt;
       if  (evt instanceof MouseEvent  &&  evt.getSource () instanceof JTable)
       {
           MouseEvent me = (MouseEvent) evt;
           JTable table = (JTable) me.getSource ();
           Point pt = new Point (me.getX (), me.getY ());
           int row = table.rowAtPoint (pt);
           int col = table.columnAtPoint (pt);
           Rectangle rec = table.getCellRect (row, col, false);
           if  (me.getY () >= rec.y + rec.height  ||  me.getX () >= rec.x + rec.width)
           {
               return false;
           }
       }

       return super.isCellEditable (evt);

}
 
Example 2
Source File: HistoryUtils.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public static String computeFitText(JTable table, int rowIdx, int columnIdx, String text) {
    if(text == null) text = "";                                             // NOI18N
    if (text.length() <= VISIBLE_START_CHARS + 3) return text;

    FontMetrics fm = table.getFontMetrics(table.getFont());
    int width = table.getCellRect(rowIdx, columnIdx, false).width;

    String sufix = "...";                                                   // NOI18N
    int sufixLength = fm.stringWidth(sufix + " ");                          // NOI18N
    int desired = width - sufixLength;
    if (desired <= 0) return text;

    for (int i = 0; i <= text.length() - 1; i++) {
        String prefix = text.substring(0, i);
        int swidth = fm.stringWidth(prefix);
        if (swidth >= desired) {
            return prefix.length() > 0 ? prefix + sufix: text;
        }
    }
    return text;
}
 
Example 3
Source File: TableRendererTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isCellEditable(EventObject anEvent) {
    if (anEvent.getSource() instanceof JTable) {
        JTable table = (JTable) anEvent.getSource();
        if (anEvent instanceof MouseEvent) {
            MouseEvent event = (MouseEvent) anEvent;
            Point p = event.getPoint();
            int row = table.rowAtPoint(p);
            int col = table.columnAtPoint(p);
            Rectangle rect = table.getCellRect(row, col, true);
            p.translate(-rect.x, -rect.y);
            System.out.println("isCellEditable("+anEvent+")");
            System.out.println("Point "+p+"in rectangle "+rect);
            if (p.x > rect.width - 24) {
                // last 24 points not editable
                return false;
            }
        }
    }
    return true;
}
 
Example 4
Source File: SwingUtilities2.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * Returns true if the given point is outside the preferredSize of the
 * item at the given row of the table.  (Column must be 0).
 * Does not check the "Table.isFileList" property. That should be checked
 * before calling this method.
 * This is used to make Windows {@literal L&F} JFileChooser act
 * like native dialogs.
 */
public static boolean pointOutsidePrefSize(JTable table, int row, int column, Point p) {
    if (table.convertColumnIndexToModel(column) != 0 || row == -1) {
        return true;
    }
    TableCellRenderer tcr = table.getCellRenderer(row, column);
    Object value = table.getValueAt(row, column);
    Component cell = tcr.getTableCellRendererComponent(table, value, false,
            false, row, column);
    Dimension itemSize = cell.getPreferredSize();
    Rectangle cellBounds = table.getCellRect(row, column, false);
    cellBounds.width = itemSize.width;
    cellBounds.height = itemSize.height;

    // See if coords are inside
    // ASSUME: mouse x,y will never be < cell's x,y
    assert (p.x >= cellBounds.x && p.y >= cellBounds.y);
    return p.x > cellBounds.x + cellBounds.width ||
            p.y > cellBounds.y + cellBounds.height;
}
 
Example 5
Source File: MemoryMapPluginTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private void editNameCell(final JTable table, String name) {
	final int nameColumn = MemoryMapModel.NAME;
	final int namedRow = getNamedRow(table, nameColumn, name);

	runSwing(() -> {
		table.getSelectionModel().setSelectionInterval(namedRow, namedRow);
		table.scrollRectToVisible(table.getCellRect(namedRow, nameColumn, true));
	});

	Rectangle rect = table.getCellRect(namedRow, nameColumn, true);
	Point tablePoint = table.getLocationOnScreen();
	final int x = tablePoint.x + rect.x + (rect.width / 2);
	final int y = tablePoint.y + rect.y + (rect.height / 2);
	runSwing(() -> {
		MouseEvent editMouseEvent = new MouseEvent(table, MouseEvent.MOUSE_CLICKED,
			System.currentTimeMillis(), 0, x, y, 2, false);
		table.editCellAt(namedRow, nameColumn, editMouseEvent);
	});

	assertEquals(true, table.isEditing());

	Component editorComponent = table.getEditorComponent();
	assertNotNull(editorComponent);
	assertTrue(editorComponent instanceof JTextField);
	final JTextField editorField = (JTextField) editorComponent;
	editorField.selectAll();
	runSwing(() -> editorField.requestFocus());
	waitForPostedSwingRunnables();

	triggerText(editorField, ".myText\n");

	assertEquals(".myText", table.getModel().getValueAt(namedRow, nameColumn));
}
 
Example 6
Source File: RComponentFactory.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the location to a point the table.
 *
 * @param table
 * @param location
 */
private void setLocationForTable(JTable table, Point location) {
    if (location != null) {
        Rectangle cellRect = table.getCellRect(table.getEditingRow(), table.getEditingColumn(), false);
        location.setLocation(cellRect.getLocation());
    }
}
 
Example 7
Source File: ResultsOutlineCellRenderer.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private String computeFitText(JTable table, int rowIdx, int columnIdx,
        String text) {
    if (text == null) {
        text = ""; // NOI18N
    }
    if (text.length() <= 3) {
        return text;
    }

    FontMetrics fm = table.getFontMetrics(table.getFont());
    int width = table.getCellRect(rowIdx, columnIdx, false).width;

    String prefix = "...";                                          //NOI18N
    int sufixLength = fm.stringWidth(prefix + "  ");                 //NOI18
    int desired = width - sufixLength - 15;
    if (desired <= 0) {
        return text;
    }

    for (int i = 1; i <= text.length() - 1; i++) {
        String part = text.substring(text.length() - i, text.length());
        int swidth = fm.stringWidth(part);
        if (swidth >= desired) {
            return part.length() > 0 ? prefix + part + " " : text;  //NOI18N
        }
    }
    return text;
}
 
Example 8
Source File: ButtonCellRenderer.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public Component getTableCellRendererComponent (
    JTable table, Object value,
    boolean isSelected, boolean hasFocus, int row, int column
) {
    JLabel c = (JLabel)defaultRenderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    
    if (value instanceof String) {
        Rectangle cellRect = table.getCellRect(row, column, false);
        String scCell = (String) value;
        Dimension d = new Dimension((int) cellRect.getWidth(), (int) cellRect.getHeight());
        if (panel == null)
            panel = new ShortcutCellPanel(scCell);
        panel.setText(scCell);
        panel.setSize(d);

        if (isSelected) {
            panel.setBgColor(table.getSelectionBackground());
            if (UIManager.getLookAndFeel ().getID ().equals ("GTK"))
                panel.setFgCOlor(table.getForeground(), true);
            else
                panel.setFgCOlor(table.getSelectionForeground(), true);
        } else {
            panel.setBgColor(c.getBackground());
            panel.setFgCOlor(c.getForeground(), false);
        }
        if (hasFocus) {
            panel.setBorder(c.getBorder());
        } else {
            panel.setBorder(null);
        }

        return panel;
    }
    else {
        return c;
    }
}
 
Example 9
Source File: KeymapPanel.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private boolean showPopupMenu(int row, int col, int x, int y) {
    JTable table = actionsTable;
    
    if (col != 1) {
        return false;
    }
    
    Object valueAt = table.getValueAt(row, col);
    ShortcutCellPanel scCell = (ShortcutCellPanel) table.getCellRenderer(row, col).getTableCellRendererComponent(table, valueAt, true, true, row, col);
    Rectangle cellRect = table.getCellRect(row, col, false);
    JButton button = scCell.getButton();
    if (x < 0  || x > (cellRect.x + cellRect.width - button.getWidth())) { //inside changeButton
        boolean isShortcutSet = scCell.getTextField().getText().length() != 0;
        final ShortcutPopupPanel panel = (ShortcutPopupPanel) popup.getComponents()[0];
        panel.setDisplayAddAlternative(isShortcutSet);
        panel.setRow(row);

        if (x == -1 || y == -1) {
            x = button.getX() + 1;
            y = button.getY() + 1;
        }
        panel.setCustomProfile(keymapModel.getMutableModel().isCustomProfile(keymapModel.getMutableModel().getCurrentProfile()));
        popup.show(table, x, y);
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                panel.requestFocus();
            }
        });
        popup.requestFocus();
        return true;
    }
    return false;
}
 
Example 10
Source File: PathnameTableModel.java    From ghidra with Apache License 2.0 4 votes vote down vote up
private void notifyDataChanged(JTable table, int newIndex) {
	fireTableDataChanged();
	table.setRowSelectionInterval(newIndex, newIndex);
	Rectangle rect = table.getCellRect(newIndex, 0, true);
	table.scrollRectToVisible(rect);
}
 
Example 11
Source File: JTableCellJavaElement.java    From marathonv5 with Apache License 2.0 4 votes vote down vote up
private boolean isVisible(JTable table, int row, int col) {
    Rectangle visibleRect = table.getVisibleRect();
    Rectangle cellRect = table.getCellRect(row, col, false);
    return SwingUtilities.isRectangleContainingRectangle(visibleRect, cellRect);
}
 
Example 12
Source File: ExtTestCase.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/** Send a mouse pressed to a particular cell of a JTable */
protected static void pressCell(JTable tbl, int x, int y) throws Exception {
    Rectangle r = tbl.getCellRect(x, y, false);
    System.err.println("Pressing table at " + (r.x+5) + "," + r.y+5);
    press(tbl, r.x+5, r.y+5);
}
 
Example 13
Source File: TabDataRenderer.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column ) {
    renderer.clear();
    Rectangle rect = table.getCellRect( row, column, true );
    renderer.setSize( rect.width, rect.height );
    if( value instanceof TabData ) {
        TabData tab = ( TabData ) value;
        String text = tab.getText();
        Icon icon = tab.getIcon();
        Color colBackground = isSelected ? table.getSelectionBackground() : table.getBackground();
        Color colForeground = isSelected ? table.getSelectionForeground() : table.getForeground();

        boolean isActive = (activeBackground != null || underlineColor != null)
            ? TabbedImpl.isActive(table) : false;
        if (!isSelected && isActive && activeBackground != null) {
            colBackground = activeBackground;
        }

        for( TabDecorator td : decorators ) {
            Color c = td.getBackground( tab, isSelected );
            if( null != c )
                colBackground = c;
            c = td.getForeground( tab, isSelected );
            if( null != c )
                colForeground = c;

            String s = td.getText( tab );
            if( null != s )
                text = s;

            Icon i = td.getIcon( tab );
            if( null != i ) {
                icon = i;
            }
        }
        boolean isHover = (hoverBackground != null && TabTableUI.isHover(table, row, column));
        if (isHover) {
            colBackground = hoverBackground;
        }
        renderer.label.setText( text );
        renderer.label.setIcon( icon );
        renderer.label.setFont( table.getFont() );
        renderer.setBackground( colBackground );
        renderer.label.setForeground( colForeground );
        renderer.tabData = tab;
        renderer.isSelected = isSelected;
        renderer.isActive = isActive;
        renderer.tabsLocation = (table instanceof TabTable) ? ((TabTable)table).getTabsLocation() : JTabbedPane.TOP;

        if( table instanceof TabTable ) {
            TabTable tabTable = ( TabTable ) table;
            if( isClosable(tab) ) {
                boolean inCloseButton = tabTable.isCloseButtonHighlighted( row, column );
                renderer.closeButton.setVisible( true );
                renderer.closeButton.getModel().setRollover( inCloseButton );
                renderer.closeButton.getModel().setArmed( inCloseButton );
            } else {
                renderer.closeButton.setVisible( false );
            }
        }
    }
    return renderer;
}
 
Example 14
Source File: HyperlinkCellRenderer.java    From littleluck with Apache License 2.0 4 votes vote down vote up
@Override
public void mouseMoved(MouseEvent event) {
    // This should only be called if underlineOnRollover is true
    JTable table = (JTable) event.getSource();

    // Locate the table cell under the event location
    int oldHitColumnIndex = hitColumnIndex;
    int oldHitRowIndex = hitRowIndex;

    checkIfPointInsideHyperlink(event.getPoint());

    if (hitRowIndex != oldHitRowIndex ||
            hitColumnIndex != oldHitColumnIndex) {
        if (hitRowIndex != -1) {
            if (tableCursor == null) {
                tableCursor = table.getCursor();
            }
            table.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        } else {
            table.setCursor(tableCursor);
        }

        // repaint the cells affected by rollover
        Rectangle repaintRect;
        if (hitRowIndex != -1 && hitColumnIndex != -1) {
            // we need to repaint new cell with rollover underline
            // cellRect already contains rect of hit cell
            if (oldHitRowIndex != -1 && oldHitColumnIndex != -1) {
                // we also need to repaint previously underlined hyperlink cell
                // to remove the underline
                repaintRect = cellRect.union(
                        table.getCellRect(oldHitRowIndex, oldHitColumnIndex, false));
            } else {
                // we don't have a previously underlined hyperlink, so just repaint new one'
                repaintRect = table.getCellRect(hitRowIndex, hitColumnIndex, false);
            }
        } else {
            // we just need to repaint previously underlined hyperlink cell
            //to remove the underline
            repaintRect = table.getCellRect(oldHitRowIndex, oldHitColumnIndex, false);
        }
        table.repaint(repaintRect);
    }

}
 
Example 15
Source File: HyperlinkCellRenderer.java    From beautyeye with Apache License 2.0 4 votes vote down vote up
@Override
public void mouseMoved(MouseEvent event) {
    // This should only be called if underlineOnRollover is true
    JTable table = (JTable) event.getSource();

    // Locate the table cell under the event location
    int oldHitColumnIndex = hitColumnIndex;
    int oldHitRowIndex = hitRowIndex;

    checkIfPointInsideHyperlink(event.getPoint());

    if (hitRowIndex != oldHitRowIndex ||
            hitColumnIndex != oldHitColumnIndex) {
        if (hitRowIndex != -1) {
            if (tableCursor == null) {
                tableCursor = table.getCursor();
            }
            table.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        } else {
            table.setCursor(tableCursor);
        }

        // repaint the cells affected by rollover
        Rectangle repaintRect;
        if (hitRowIndex != -1 && hitColumnIndex != -1) {
            // we need to repaint new cell with rollover underline
            // cellRect already contains rect of hit cell
            if (oldHitRowIndex != -1 && oldHitColumnIndex != -1) {
                // we also need to repaint previously underlined hyperlink cell
                // to remove the underline
                repaintRect = cellRect.union(
                        table.getCellRect(oldHitRowIndex, oldHitColumnIndex, false));
            } else {
                // we don't have a previously underlined hyperlink, so just repaint new one'
                repaintRect = table.getCellRect(hitRowIndex, hitColumnIndex, false);
            }
        } else {
            // we just need to repaint previously underlined hyperlink cell
            //to remove the underline
            repaintRect = table.getCellRect(oldHitRowIndex, oldHitColumnIndex, false);
        }
        table.repaint(repaintRect);
    }

}