下面列出了javax.swing.JTable#columnAtPoint ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public RTable(Component source, JSONOMapConfig omapConfig, Point point, IJSONRecorder recorder) {
super(source, omapConfig, point, recorder);
JTable table = (JTable) source;
if (table.isEditing()) {
column = table.getEditingColumn();
row = table.getEditingRow();
} else {
if (point != null) {
row = table.rowAtPoint(point);
column = table.columnAtPoint(point);
} else {
row = table.getSelectedRow();
column = table.getSelectedColumn();
}
}
if (row == -1 || column == -1) {
row = column = -1;
}
}
/**********************************************************************
* 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);
}
private void maybeShowPopup(MouseEvent e) {
if (e.isPopupTrigger()) {
JTable source = (JTable)(e.getSource());
if ( ! source.isEnabled () ) return;
_inputTablePopupRow =
source.rowAtPoint(new Point (e.getX(), e.getY()));
_inputTablePopupColumn =
source.columnAtPoint(new Point (e.getX(), e.getY()));
// Make sure the row where click occurred is selected.
if (_inputTablePopupRow != -1) {
source.setRowSelectionInterval (_inputTablePopupRow,
_inputTablePopupRow);
}
// if ( _inputTablePopupColumn != Criteria_COLUMN )
// {
// // return without showing popup
// return;
// }
_inputTablePopup.show(e.getComponent(), e.getX(), e.getY());
}
}
@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;
}
/**
* Called when user clicks on the table.
*
* @param e Mouse event.
*/
public void mouseClicked(MouseEvent e) {
if (e == null) {
return;
}
// Extract information
int count = e.getClickCount();
JTable target = (JTable) e.getSource();
int row = target.rowAtPoint(e.getPoint());
int column = target.columnAtPoint(e.getPoint());
int modelRow = target.convertRowIndexToModel(row);
int modelColumn = target.convertColumnIndexToModel(column);
// Act depending on the button clicked
switch (e.getButton()) {
case MouseEvent.BUTTON1:
cellClickedLeft(count, modelRow, modelColumn);
break;
case MouseEvent.BUTTON3:
cellClickedRight(target, e.getPoint());
break;
}
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
String deckName = (String) value;
JLabel lbl = new JLabel(deckName);
lbl.setOpaque(true);
if (isSelected) {
lbl.setForeground(table.getSelectionForeground());
lbl.setBackground(table.getSelectionBackground());
} else {
lbl.setForeground(table.getForeground());
lbl.setBackground(table.getBackground());
}
Point mp = MouseInfo.getPointerInfo().getLocation();
SwingUtilities.convertPointFromScreen(mp, table);
int mRow = table.rowAtPoint(mp);
int mCol = table.columnAtPoint(mp);
if (row == mRow && column == mCol) {
lbl.setForeground(Color.blue);
lbl.setFont(withUnderline);
}
return lbl;
}
@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();
}
}
private void maybeShowPopup(MouseEvent e) {
if (e.isPopupTrigger()) {
JTable source = (JTable)(e.getSource());
int row = source.rowAtPoint(e.getPoint());
int column = source.columnAtPoint(e.getPoint());
// Make sure the row where click occurred is selected.
if (row != -1) {
source.setRowSelectionInterval (row, row);
}
resultTablePopup.show(e.getComponent(), e.getX(), e.getY());
}
}
@Override
public void mouseDragged(MouseEvent e) {
if (e.isAltDown() && startLocation != null) {
isInDragOperation = true;
JTable t = (JTable) e.getSource();
int selRow = t.rowAtPoint(e.getPoint());
int selColumn = t.columnAtPoint(e.getPoint());
if (selRow != -1 && selColumn != -1) {
rowsRColumns.add(new Integer[]{selRow, selColumn});
}
} else {
isInDragOperation = false;
}
}
@Override
public void mouseClicked(MouseEvent e) {
int fromRow = NO_ROW_SELECTED;
JTable table;
// RT MAX MIN SWITCH
if (healthTableToDisplay == DISPLAY_RT) {
table = rtTable;
} else if (healthTableToDisplay == DISPLAY_MAX) {
table = maxTable;
} else {
table = minTable;
}
// row is the one we clicked on
int row = table.rowAtPoint(e.getPoint());
int col = table.columnAtPoint(e.getPoint());
if (e.isShiftDown()) {
// from row is the first in the selection. It equals row if we clicked above the current selected row
fromRow = table.getSelectedRow();
int n = table.getSelectedRowCount();
if (row == fromRow)
fromRow = fromRow + n-1;
}
if (row >= 0 && col >= 0) {
//Log.println("CLICKED ROW: "+row+ " and COL: " + col);
displayRow(table, fromRow, row);
}
}
public void mouseClicked(MouseEvent e) {
int fromRow = NO_ROW_SELECTED;
JTable table = null;
if (showRawBytes.isSelected()) {
table = this.table;
} else {
table = packetTable;
}
int row = table.rowAtPoint(e.getPoint());
int col = table.columnAtPoint(e.getPoint());
if (e.isShiftDown()) {
// from row is the first in the selection. It equals row if we clicked above the current selected row
fromRow = table.getSelectedRow();
int n = table.getSelectedRowCount();
if (row == fromRow)
fromRow = fromRow + n-1;
}
if (row >= 0 && col >= 0) {
//Log.println("CLICKED ROW: "+row+ " and COL: " + col);
displayRow(table, fromRow, row);
}
}
/**
* @param e Event.
* @see java.awt.event.MouseAdapter#mouseClicked(java.awt.event.MouseEvent)
*/
@Override
public void mouseClicked(MouseEvent e) {
if ((!e.isConsumed()) &&
(e.getButton() == MouseEvent.BUTTON1) &&
(e.getClickCount() == 2)) {
if (e.getSource() instanceof JTable) {
JTable table = (JTable) e.getSource();
if (table.getModel() instanceof PageListTableModel) {
PageListTableModel model = (PageListTableModel) table.getModel();
int column = table.columnAtPoint(e.getPoint());
int row = table.rowAtPoint(e.getPoint());
if ((column >= 0) && (row >= 0)) {
row = Utilities.convertRowIndexToModel(table, row);
Page page = model.getPage(row);
if (Boolean.TRUE.equals(page.isDisambiguationPage())) {
Controller.runDisambiguationAnalysis(page.getTitle(), page.getWikipedia());
} else {
Controller.runFullAnalysis(page.getTitle(), null, page.getWikipedia());
}
e.consume();
}
}
}
}
super.mouseClicked(e);
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
String profileGUID = (String)value;
PlayerProfile player = PlayerProfiles.getPlayerProfiles().get(profileGUID);
boolean isTempPlayer = player == null;
final JLabel lbl = new JLabel(!isTempPlayer ? player.getPlayerName() : "");
lbl.setOpaque(true);
if (isSelected) {
lbl.setForeground(table.getSelectionForeground());
lbl.setBackground(table.getSelectionBackground());
} else {
lbl.setForeground(table.getForeground());
lbl.setBackground(table.getBackground());
}
if (isTempPlayer) {
lbl.setForeground(Color.GRAY);
} else {
Point mp = MouseInfo.getPointerInfo().getLocation();
SwingUtilities.convertPointFromScreen(mp, table);
int mRow = table.rowAtPoint(mp);
int mCol = table.columnAtPoint(mp);
if (row == mRow && column == mCol) {
lbl.setForeground(Color.blue);
lbl.setFont(withUnderline);
}
}
return lbl;
}
private boolean isMouseOverCell(JTable table, int row, int col) {
Point mp = MouseInfo.getPointerInfo().getLocation();
SwingUtilities.convertPointFromScreen(mp, table);
int mRow = table.rowAtPoint(mp);
int mCol = table.columnAtPoint(mp);
return row == mRow && col == mCol;
}
@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();
}
}
private void selectClickedCell(final MouseEvent e) {
Object source = e.getSource();
if (source instanceof JTable) {
JTable jSelectTable = (JTable) source;
Point point = e.getPoint();
if (!jSelectTable.getVisibleRect().contains(point)) { //Ignore clickes outside table
return;
}
int clickedRow = jSelectTable.rowAtPoint(point);
int clickedColumn = jSelectTable.columnAtPoint(point);
//Rows
boolean clickInRowsSelection;
if (jSelectTable.getRowSelectionAllowed()) { //clicked in selected rows?
clickInRowsSelection = false;
int[] selectedRows = jSelectTable.getSelectedRows();
for (int i = 0; i < selectedRows.length; i++) {
if (selectedRows[i] == clickedRow) {
clickInRowsSelection = true;
break;
}
}
} else { //Row selection not allowed - all rows selected
clickInRowsSelection = true;
}
//Column
boolean clickInColumnsSelection;
if (jSelectTable.getColumnSelectionAllowed()) { //clicked in selected columns?
clickInColumnsSelection = false;
int[] selectedColumns = jSelectTable.getSelectedColumns();
for (int i = 0; i < selectedColumns.length; i++) {
if (selectedColumns[i] == clickedColumn) {
clickInColumnsSelection = true;
break;
}
}
} else { //Column selection not allowed - all columns selected
clickInColumnsSelection = true;
}
//Clicked outside selection, select clicked cell
if ( (!clickInRowsSelection || !clickInColumnsSelection) && clickedRow >= 0 && clickedColumn >= 0) {
jSelectTable.setRowSelectionInterval(clickedRow, clickedRow);
jSelectTable.setColumnSelectionInterval(clickedColumn, clickedColumn);
}
}
}
/**
* Tells if the specified event happened on the home page link cell.
* @param detailsTable reference to the details table
* @param event event to examine
* @return true if the event happened on the home page link cell; false otherwise
*/
private static boolean isEventOnHomePageLinkCell( final JTable detailsTable, final MouseEvent event ) {
final Point point = event.getPoint();
return detailsTable.rowAtPoint( point ) == 6 && detailsTable.columnAtPoint( point ) == 1;
}