下面列出了javax.swing.JTable#repaint ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void mouseMoved(MouseEvent e) {
JTable aTable = (JTable) e.getSource();
int mCol = aTable.columnAtPoint(e.getPoint());
int mRow = aTable.rowAtPoint(e.getPoint());
if (mCol != lastMCol || mRow != lastMRow) {
lastMCol = mCol;
lastMRow = mRow;
if (mCol == 4) {
aTable.repaint();
MouseHelper.showHandCursor(aTable);
} else {
aTable.repaint();
MouseHelper.showDefaultCursor(aTable);
}
}
}
@Override
public void mousePressed(MouseEvent e) {
super.mousePressed(e);
if(!SwingUtilities.isLeftMouseButton(e)) {
return;
}
JTable t = getTable();
Point p = e.getPoint();
int row = t.rowAtPoint(p);
int column = t.columnAtPoint(p);
int rowCount = t.getRowCount();
int columnCount = t.getColumnCount();
if(column < 0 || row < 0 || column >= columnCount || row >= rowCount ) {
return;
}
TableCellEditor tce = t.getCellEditor();
if((tce==null) || (tce.shouldSelectCell(e))) {
t.requestFocus();
updateTableSelectionModel(row, column, e.isControlDown(), e.isShiftDown(), false);
t.repaint();
}
}
public void showPopupMenu(MouseEvent e)
{
JTable table = (JTable) e.getSource();
int row = table.rowAtPoint(e.getPoint());
//int col = table.columnAtPoint(e.getPoint());
boolean val = !((PacketTableModel) table.getModel()).getIsMarked(row);
((PacketTableModel) table.getModel()).setIsMarked(row, val);
table.repaint();
}
@Override
public void mouseDragged(MouseEvent e) {
super.mouseDragged(e);
if(!SwingUtilities.isLeftMouseButton(e)) {
return;
}
JTable t = getTable();
Point p = e.getPoint();
int row = t.rowAtPoint(p);
int column = t.columnAtPoint(p);
int rowCount = t.getRowCount();
int columnCount = t.getColumnCount();
if(column < 0 || row < 0 || column >= columnCount || row >= rowCount ) {
return;
}
TableCellEditor tce = t.getCellEditor();
if(tce==null) {
t.requestFocus();
updateTableSelectionModel(row, column, e.isControlDown(), !e.isShiftDown(), true);
t.repaint();
}
}
/** Resize all columns in the table to fit widest row including header. */
public static void resizeColumns( JTable table) {
if (table.getGraphics() == null) {
return;
}
DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
FontMetrics fm = table.getFontMetrics( renderer.getFont() );
TableColumnModel mod = table.getColumnModel();
for (int iCol = 0; iCol < mod.getColumnCount(); iCol++) {
TableColumn col = mod.getColumn( iCol);
int max = col.getPreferredWidth() - BUF;
String header = table.getModel().getColumnName( iCol);
if (header != null) {
max = Math.max( max, fm.stringWidth( header) );
}
for (int iRow = 0; iRow < table.getModel().getRowCount(); iRow++) {
Object obj = table.getModel().getValueAt(iRow, iCol);
String str = obj == null ? "" : obj.toString();
max = Math.max( max, fm.stringWidth( str) );
}
col.setPreferredWidth( max + BUF);
col.setMaxWidth( MAX);
}
table.revalidate();
table.repaint();
}
/**
* Adjusts the column width of the row headers table containg the number
* column. The font metrics are extracted from the label of the row at the
* bottom of the viewport and used to determing the appropriate width.
*
* The reason why this method is important, is that when the row number increases by an extra digit
* the column needs to get wider. It also needs to shrink when scrolling to smaller digit numbers.
*
* @param rowHeadersTable - single column table in the row header
* @param label - label used to get font metrics
* @param scrollBarValue - int value for determing point of lowest row
*/
private static void adjustColumnWidth(final JTable rowHeadersTable, final JLabel label, int scrollBarValue) {
label.setFont(rowHeadersTable.getFont());
label.setOpaque(true);
label.setHorizontalAlignment(JLabel.CENTER);
int v = rowHeadersTable.getVisibleRect().height;
int row = rowHeadersTable.rowAtPoint(new Point(0, v + scrollBarValue));
Integer modelValue = null;
if (row != -1) {
modelValue = (Integer) rowHeadersTable.getModel().getValueAt(row, 0);
} else {
RowHeadersTableModel tm = (RowHeadersTableModel) rowHeadersTable.getModel();
modelValue = new Integer(tm.getMaxIntValue());
}
label.setText("" + modelValue);
FontMetrics fontMetrics = label.getFontMetrics(label.getFont());
int widthFactor = 0;
if (fontMetrics != null && label.getText() != null) {
widthFactor = fontMetrics.stringWidth(label.getText());
rowHeadersTable.setPreferredScrollableViewportSize(new Dimension(widthFactor + 8, 100)); // height
// is
// ignored
rowHeadersTable.repaint();
}
}
@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);
}
}
@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);
}
}
private void setSizeAndDimensions(@NotNull JTable table, @NotNull JBPopup popup,
@NotNull RelativePoint popupPosition, @NotNull List<UsageNode> data) {
JComponent content = popup.getContent();
Window window = SwingUtilities.windowForComponent(content);
Dimension d = window.getSize();
int width = calcMaxWidth(table);
width = (int) Math.max(d.getWidth(), width);
Dimension headerSize = ((AbstractPopup) popup).getHeaderPreferredSize();
width = Math.max((int) headerSize.getWidth(), width);
width = Math.max(myWidth, width);
if (myWidth == -1) myWidth = width;
int newWidth = Math.max(width, d.width + width - myWidth);
myWidth = newWidth;
int rowsToShow = Math.min(30, data.size());
Dimension dimension = new Dimension(newWidth, table.getRowHeight() * rowsToShow);
Rectangle rectangle = fitToScreen(dimension, popupPosition, table);
dimension = rectangle.getSize();
Point location = window.getLocation();
if (!location.equals(rectangle.getLocation())) {
window.setLocation(rectangle.getLocation());
}
if (!data.isEmpty()) {
TableScrollingUtil.ensureSelectionExists(table);
}
table.setSize(dimension);
//table.setPreferredSize(dimension);
//table.setMaximumSize(dimension);
//table.setPreferredScrollableViewportSize(dimension);
Dimension footerSize = ((AbstractPopup) popup).getFooterPreferredSize();
int newHeight = (int) (dimension.height + headerSize.getHeight() + footerSize.getHeight()) + 4/* invisible borders, margins etc*/;
Dimension newDim = new Dimension(dimension.width, newHeight);
window.setSize(newDim);
window.setMinimumSize(newDim);
window.setMaximumSize(newDim);
window.validate();
window.repaint();
table.revalidate();
table.repaint();
}
private void setSizeAndDimensions(@NotNull JTable table,
@NotNull JBPopup popup,
@NotNull RelativePoint popupPosition,
@NotNull List<UsageNode> data) {
JComponent content = popup.getContent();
Window window = SwingUtilities.windowForComponent(content);
Dimension d = window.getSize();
int width = calcMaxWidth(table);
width = (int)Math.max(d.getWidth(), width);
Dimension headerSize = ((AbstractPopup)popup).getHeaderPreferredSize();
width = Math.max((int)headerSize.getWidth(), width);
width = Math.max(myWidth, width);
if (myWidth == -1) myWidth = width;
int newWidth = Math.max(width, d.width + width - myWidth);
myWidth = newWidth;
int rowsToShow = Math.min(30, data.size());
Dimension dimension = new Dimension(newWidth, table.getRowHeight() * rowsToShow);
Rectangle rectangle = fitToScreen(dimension, popupPosition, table);
dimension = rectangle.getSize();
Point location = window.getLocation();
if (!location.equals(rectangle.getLocation())) {
window.setLocation(rectangle.getLocation());
}
if (!data.isEmpty()) {
TableScrollingUtil.ensureSelectionExists(table);
}
table.setSize(dimension);
//table.setPreferredSize(dimension);
//table.setMaximumSize(dimension);
//table.setPreferredScrollableViewportSize(dimension);
Dimension footerSize = ((AbstractPopup)popup).getFooterPreferredSize();
int newHeight = (int)(dimension.height + headerSize.getHeight() + footerSize.getHeight()) + 4/* invisible borders, margins etc*/;
Dimension newDim = new Dimension(dimension.width, newHeight);
window.setSize(newDim);
window.setMinimumSize(newDim);
window.setMaximumSize(newDim);
window.validate();
window.repaint();
table.revalidate();
table.repaint();
}