下面列出了javax.swing.JTable#scrollRectToVisible ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void searchNext() {
if (getViewerTab().getComponentCount() > 0) {
if (_searchDlg != null) {
ViewPane pane = ((Main) PacketSamurai.getUserInterface()).getViewerTab().getCurrentViewPane();
if (pane != null) {
int index = _searchDlg.search(pane.getSelectedPacketindex() + 1);
if (index >= 0) {
JTable pt = ((Main) PacketSamurai.getUserInterface()).getViewerTab().getCurrentViewPane().getPacketTable();
pt.setAutoscrolls(true);
pt.getSelectionModel().setSelectionInterval(index, index);
pt.scrollRectToVisible(pt.getCellRect(index, 0, true));
_searchDlg.setCurrentSearchIndex(index + 1);
}
}
} else {
toggleSearchDialog();
}
}
}
private void setSelectedRow(JTable table, Object selectedObject, int objectColumn) {
if(table==null || selectedObject==null || objectColumn==-1)return;
//after move to java6 as minimum requirent for nb may consider to replace with JTable::convertRowIndexToModel and back
//current mplementation works with java5
TableModel model = table.getModel();
int rowCount=table.getRowCount();
for(int i=0; i<rowCount; i++) {
if(selectedObject.equals(model.getValueAt(i, objectColumn))) {
table.setRowSelectionInterval(i, i);
table.scrollRectToVisible(table.getCellRect(i, objectColumn, true));
break;
}
}
}
/**
* Inserts new row if any row is selected else adds new row at the end
*
* @param table
*/
public static void addrow(JTable table) {
int rowindex = table.getRowCount();
if (table.getSelectedRow() > -1) {
((DefaultTableModel) table.getModel()).addRow(nullRow);
((DefaultTableModel) table.getModel()).moveRow(table.getRowCount() - 1, table.getRowCount() - 1, table.getSelectedRow());
rowindex = table.getSelectedRow();
} else {
((DefaultTableModel) table.getModel()).addRow(nullRow);
}
table.scrollRectToVisible(new Rectangle(table.getCellRect(rowindex, 0, true)));
}
/**
* This method selects a certain row that contains the value {@code value}
* in the column {@code column} in the jTable {@code table}.
*
* @param table the table where the row should be selected
* @param value the value that should be selected
* @param column the table-column that contains the value {@code value}.
*/
public static void selectValueInTable(JTable table, String value, int column) {
for (int cnt = 0; cnt < table.getRowCount(); cnt++) {
String val = table.getValueAt(cnt, column).toString();
if (val.equals(value)) {
table.getSelectionModel().setSelectionInterval(cnt, cnt);
table.scrollRectToVisible(table.getCellRect(cnt, column, false));
// and leave method
return;
}
}
}
/**
* This method retrieves the key-code {@code keyCode} of a released key in
* the JTable {@code table} and checks whether this key was a navigation key
* (i.e. cursor up/down/left/right or home/end). If so, the method tries to
* select the next related entry of that JTable, according to the pressed
* key.<br><br>
* Furthermore, the related content is made visible (scroll rect to visible
* or ensure index is visible).
*
* @param table a reference to the JTable where the related key was released
* @param keyCode the keycode of the released key
*/
public static void navigateThroughList(JTable table, int keyCode) {
if (KeyEvent.VK_LEFT == keyCode || KeyEvent.VK_RIGHT == keyCode) {
return;
}
int selectedRow = table.getSelectedRow();
if (-1 == selectedRow) {
selectedRow = 0;
}
switch (keyCode) {
case KeyEvent.VK_HOME:
selectedRow = 0;
break;
case KeyEvent.VK_END:
selectedRow = table.getRowCount() - 1;
break;
case KeyEvent.VK_DOWN:
if (table.getRowCount() > (selectedRow + 1)) {
selectedRow++;
}
break;
case KeyEvent.VK_UP:
if (selectedRow > 0) {
selectedRow--;
}
break;
}
table.getSelectionModel().setSelectionInterval(selectedRow, selectedRow);
table.scrollRectToVisible(table.getCellRect(selectedRow, 0, false));
}
/**
* This method selects the first entry in the JTable {@code table} that
* start with the text that is entered in the filter-textfield
* {@code textfield}.
*
* @param table the jTable where the item should be selected
* @param textfield the related filtertextfield that contains the user-input
* @param column the column where the filtering-comparison should be applied
* to. in most cases, the relevant information (i.e. the string/text) is in
* column 0, but sometimes also in column 1
*/
public static void selectByTyping(JTable table, javax.swing.JTextField textfield, int column) {
String text = textfield.getText().toLowerCase();
for (int cnt = 0; cnt < table.getRowCount(); cnt++) {
String val = table.getValueAt(cnt, column).toString();
if (val.toLowerCase().startsWith(text)) {
table.getSelectionModel().setSelectionInterval(cnt, cnt);
table.scrollRectToVisible(table.getCellRect(cnt, column, false));
// and leave method
return;
}
}
}
private void notifyDataChanged(JTable table, int newIndex) {
fireTableDataChanged();
table.setRowSelectionInterval(newIndex, newIndex);
Rectangle rect = table.getCellRect(newIndex, 0, true);
table.scrollRectToVisible(rect);
}
public static void scrollToSelectedRow(JTable table) {
int selectedRow = table.getSelectedRow();
table.scrollRectToVisible(table.getCellRect(selectedRow, 0, true));
}