SWING高手请进,怎样在JTABLE中添加ROW时,让ADD的ROW那一行始终处于屏幕可视状态 难道没有SWING高手吗??????????! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 use JTable.getCellRect() to get the rect of the row.use JScrollPane.scrollRectToVisible(); to make this row visible. 老兄,不行呀,jtable.getCellRect()返回Rectangle,可以打印,x=xxx,y=xxx,width=0,height=20,但call jscrollpane.scrollRectToVisible();就是没用,请指点 我try了一下,可以的:import java.awt.*;import javax.swing.*;import java.awt.event.*;import javax.swing.table.*;public class testFrame extends JFrame{ int rows=80; int cols=5; private Object[] rowData=new Object[cols]; private DefaultTableModel model=new DefaultTableModel(); JScrollPane jp = new JScrollPane(); private JTable table=new JTable(model); public testFrame() { for(int i=0;i<cols;i++) model.addColumn("Column"+i); for(int i=0;i<rows;i++) { for(int j=0;j<cols;j++) rowData[j]="("+i+","+j+")"; model.addRow(rowData); } Container contentPane=getContentPane(); jp.getViewport().add(table); contentPane.add(new ControlPanel(),BorderLayout.NORTH); contentPane.add(jp,BorderLayout.CENTER); } public static void main(String args[]) { testFrame bt=new testFrame(); bt.setTitle("test of AbstractTableModel"); bt.setSize(400,400); bt.setVisible(true); } class ControlPanel extends JPanel { private JButton scrollButton = new JButton("add&scroll"); public ControlPanel() { scrollButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String sdata[]=new String[5]; for(int i=0;i<5;i++) { sdata[i]="cell"+i; } model.addRow(sdata); int row=model.getRowCount(); Rectangle cellrec=table.getCellRect(row-1,0,true); jp.getViewport().scrollRectToVisible(cellrec); } }); } }} 谢谢您的回答,我有try了下,为何编译时老报告不能解析符号getViewport(),请指教! 我try了一下没有出问题。 急等!!通过FTPClient下载文件看不到文件。 找不到符号 JNI如何传递vc中带指针的参数和函数的问题 JAVAMAIL如何建立发件箱 学习J2SE 读完拉thinking in java 接下来要看什么书啊? 求助一个COOKIE的问题 JAVA执行时遇到的问题 关于线程问题,请指教,谢谢了 大力推荐《java 2 编程指南 jdk1.3版》,我买了! java中如何进行打印? 不知道广州有没有需要兼职java程序员的???? 初探application的布局
use JScrollPane.scrollRectToVisible(); to make this row visible.
jtable.getCellRect()返回Rectangle,
可以打印,x=xxx,y=xxx,width=0,height=20,
但call jscrollpane.scrollRectToVisible();就是没用,请指点
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;import javax.swing.table.*;
public class testFrame extends JFrame
{
int rows=80;
int cols=5;
private Object[] rowData=new Object[cols];
private DefaultTableModel model=new DefaultTableModel();
JScrollPane jp = new JScrollPane();
private JTable table=new JTable(model); public testFrame()
{
for(int i=0;i<cols;i++)
model.addColumn("Column"+i);
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
rowData[j]="("+i+","+j+")";
model.addRow(rowData);
} Container contentPane=getContentPane();
jp.getViewport().add(table); contentPane.add(new ControlPanel(),BorderLayout.NORTH);
contentPane.add(jp,BorderLayout.CENTER); }
public static void main(String args[])
{
testFrame bt=new testFrame();
bt.setTitle("test of AbstractTableModel");
bt.setSize(400,400);
bt.setVisible(true);
}
class ControlPanel extends JPanel
{ private JButton scrollButton = new JButton("add&scroll");
public ControlPanel()
{ scrollButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{ String sdata[]=new String[5];
for(int i=0;i<5;i++)
{
sdata[i]="cell"+i;
}
model.addRow(sdata);
int row=model.getRowCount();
Rectangle cellrec=table.getCellRect(row-1,0,true);
jp.getViewport().scrollRectToVisible(cellrec);
}
});
}
}
}
没有出问题。