在JTable中垂直滚动条 [英] vertical scroll bar in JTable
本文介绍了在JTable中垂直滚动条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个JTable中与它垂直滚动条,当我加入新行的滚动条将移动到新行。问题是滚动条是在框架中可见,但我不能把它的滚动。
这是我的方式创建的JTable
表=新javax.swing.JTable中的(){
公共布尔的isCellEditable(INT的rowIndex,诠释colIndex){
返回false; //不允许任何单元格的编辑
}
}; 模型=(的DefaultTableModel)table.getModel(); table.setRowHeight(20); selectionModel设置= table.getSelectionModel();
selectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); //创建一对夫妇列
model.addColumn(SERVERIP);
model.addColumn(端口);
model.addColumn(请求的数量);
JTableHeader的头= table.getTableHeader();
颜色C =新的色彩(163,250,250);
header.setBackground(C);
窗格=新JScrollPane的(表);
pane.setViewportView(表);
pane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); table.setFocusable(假);
//这里的时候添加新行我叫滚动条
scrollToNewRow(表,rowCount时,1);
rowCount等++;
私有静态无效scrollToNewRow(JTable的表,诠释行,诠释山口){
如果(!(table.getParent()的instanceof的JViewport)){
返回;
}
JViewport的视口=(JViewport的)table.getParent(); //这矩形是相对于表,其中
//单元格(0,0)的西北角始终是(0,0)。
矩形矩形= table.getCellRect(0,0,真); //相视口表中的位置
点PT = viewport.getViewPosition(); //翻译单元的位置,以便它相对于
//到视图,假设的西北角
//观点是(0,0)
rect.setLocation(rect.x-pt.x,rect.y-pt.y); //滚动区域进入视野
viewport.scrollRectToVisible(RECT);
}私有静态JScrollPane的getScrollPane(c成分){
而((C = c.getParent())!= NULL)
如果(C的instanceof JScrollPane的)
返回(JScrollPane的)C;
返回null;
}
解决方案
你总是叫 getCellRect
的单元格(0,0)。尝试更换:
矩形RECT = table.getCellRect(0,0,真正的);
与
矩形RECT = table.getCellRect(行,列,真正的);
I have one JTable with vertical scroll bar on it,when i added new row the scroll bar will move to the new row. The problem is scrollbar is visible in the frame but i can't scroll it.
this is way i created jtable
table = new javax.swing.JTable(){
public boolean isCellEditable(int rowIndex, int colIndex) {
return false; //Disallow the editing of any cell
}
};
model = (DefaultTableModel) table.getModel();
table.setRowHeight(20);
selectionModel = table.getSelectionModel();
selectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
// Create a couple of columns
model.addColumn("ServerIP");
model.addColumn("Port");
model.addColumn("Number of Request");
JTableHeader header = table.getTableHeader();
Color c = new Color(163, 250, 250);
header.setBackground(c);
pane = new JScrollPane(table);
pane.setViewportView(table);
pane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
table.setFocusable(false);
//when added new row here i call scrollbar
scrollToNewRow(table, rowCount, 1);
rowCount++;
private static void scrollToNewRow(JTable table, int row, int col) {
if (!(table.getParent() instanceof JViewport)) {
return;
}
JViewport viewport = (JViewport)table.getParent();
// This rectangle is relative to the table where the
// northwest corner of cell (0,0) is always (0,0).
Rectangle rect = table.getCellRect(0, 0, true);
// The location of the viewport relative to the table
Point pt = viewport.getViewPosition();
// Translate the cell location so that it is relative
// to the view, assuming the northwest corner of the
// view is (0,0)
rect.setLocation(rect.x-pt.x, rect.y-pt.y);
// Scroll the area into view
viewport.scrollRectToVisible(rect);
}
private static JScrollPane getScrollPane(Component c) {
while ((c = c.getParent()) != null)
if (c instanceof JScrollPane)
return (JScrollPane) c;
return null;
}
解决方案
You're always calling getCellRect
for the cell at (0, 0). Try replacing:
Rectangle rect = table.getCellRect(0, 0, true);
with :
Rectangle rect = table.getCellRect(row, col, true);
这篇关于在JTable中垂直滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文