Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 一个JTable当前显示多少行?_Java_Swing_Jtable - Fatal编程技术网

Java 一个JTable当前显示多少行?

Java 一个JTable当前显示多少行?,java,swing,jtable,Java,Swing,Jtable,JTable有一个方法getVisibleRowCount(),它显示要显示的首选行数 我想确定JTable中当前可见的实际行数。我该怎么做 我目前的尝试是: int rowsVisible = table.getSize().getHeight()/table.getRowHeight(); 但它给我的价值远远高于我所能看到的 例如,当有10行或11行可见时,结果为18。请尝试: table.getModel().getRowCount(); 我还没有对此进行测试,但我希望当JScroll

JTable有一个方法getVisibleRowCount(),它显示要显示的首选行数

我想确定JTable中当前可见的实际行数。我该怎么做

我目前的尝试是:

int rowsVisible = table.getSize().getHeight()/table.getRowHeight();
但它给我的价值远远高于我所能看到的

例如,当有10行或11行可见时,结果为18。

请尝试:

table.getModel().getRowCount();

我还没有对此进行测试,但我希望当
JScrollPane
中包含
JTable
时,您可以使用
getViewPort
向滚动窗格请求其视口,并从视口中检索大小

如果将该高度与表的行高相除,可能会得到更好的估计

final int pageSize = 
    (int) (table.getParent().getSize().getHeight() / table.getRowHeight());

非常接近

Robin提供的方式并不完全正确-当桌子有不同的行高时,它将不起作用。它也不会检查行间距和其他一些细微差别

以下是适用于任何L&F和表格设置的示例:

public static void main ( String args[] )
{
    final JLabel rows = new JLabel ( "Visible rows: ?", JLabel.CENTER );

    final JTable table = new JTable ( new DefaultTableModel ( 30, 3 )
    {
        public String getColumnName ( int column )
        {
            return "title";
        }

        public Object getValueAt ( int row, int column )
        {
            return "cell";
        }
    } );

    JScrollPane scroll = new JScrollPane ( table )
    {
        public Dimension getPreferredSize ()
        {
            Dimension ps = super.getPreferredSize ();
            ps.height = 150;
            return ps;
        }
    };
    scroll.addComponentListener ( new ComponentAdapter ()
    {
        public void componentResized ( ComponentEvent e )
        {
            Rectangle vr = table.getVisibleRect ();
            int visibleRows = 0;
            for ( int i = 0; i < table.getRowCount (); i++ )
            {
                Rectangle cell = table.getCellRect ( i, 0, false );
                if ( cell.y <= vr.y && cell.y + cell.height >= vr.y ||
                        cell.y <= vr.y + vr.height &&
                                cell.y + cell.height >= vr.y + vr.height ||
                        cell.y >= vr.y && cell.y + cell.height <= vr.y + vr.height )
                {
                    visibleRows++;
                }
            }
            rows.setText ( "Visible rows: " + visibleRows );
        }
    } );

    JPanel panel = new JPanel ( new BorderLayout ( 25, 25 ) );
    panel.setBorder ( BorderFactory.createEmptyBorder ( 25, 25, 25, 25 ) );
    panel.add ( rows, BorderLayout.NORTH );
    panel.add ( scroll, BorderLayout.CENTER );

    JFrame frame = new JFrame ();
    frame.add ( panel );
    frame.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
    frame.pack ();
    frame.setLocationRelativeTo ( null );
    frame.setVisible ( true );
}
publicstaticvoidmain(字符串参数[])
{
最终JLabel行=新JLabel(“可见行:?”,JLabel.CENTER);
最终JTable表格=新JTable(新DefaultTableModel(30,3)
{
公共字符串getColumnName(int列)
{
返回“标题”;
}
公共对象getValueAt(int行,int列)
{
返回“单元格”;
}
} );
JScrollPane scroll=新的JScrollPane(表)
{
公共维度getPreferredSize()
{
维度ps=super.getPreferredSize();
ps.高度=150;
返回ps;
}
};
scroll.addComponentListener(新的ComponentAdapter()
{
公共无效组件已恢复(组件事件e)
{
矩形vr=table.getVisibleRect();
int visibleRows=0;
对于(inti=0;icell.y>=vr.y&&cell.y+cell.height问问桌子,它正在为你做所有的艰苦工作:

Rectangle vr = table.getVisibleRect ();
int first = table.rowAtPoint(vr.getLocation());
vr.translate(0, vr.height);
int visibleRows = table.rowAtPoint(vr.getLocation()) - first;

这些尝试都不正确。只是因为它不关心向下滚动…:-/

我根据能够找到的正确答案编写了代码,这些答案经过了尝试,并且是正确的(我希望如此)

但我只在第0列尝试过

public static int getNumberOfVisibleRows(JTable table) {
    Rectangle vr = table.getVisibleRect();
    int first = table.rowAtPoint(vr.getLocation());
    vr.translate(0, vr.height);
    return table.rowAtPoint(vr.getLocation()) - first;
}

public static void scrollToVisible(JTable table, int rowIndex, int columnIndex, int numberOfVisibleRows) {
    Rectangle visibleRect = table.getVisibleRect();
    Rectangle rect1 = table.getCellRect(rowIndex, columnIndex, false);
    if (visibleRect.y > rect1.y) {
        table.scrollRectToVisible(rect1);
    } else {
        Rectangle rect2 = table.getCellRect(rowIndex + numberOfVisibleRows, columnIndex, false);
        int width = rect2.y - rect1.y;
        table.scrollRectToVisible(new Rectangle(rect1.x, rect1.y, rect1.width, rect1.height + width));
    }
}

这将返回表中的总行数,而不是当前可见的行数。抱歉,我的错误。这可能与JScrollPane有关。投票删除此答案,因为它没有回答所问的问题。抱歉……但是a)不完整(正如@Mikle Garin已经指出的),而且级别太低(如您所知,始终使用可用的最高抽象;-)@SteveMcLeod此线程中有更好的答案。如果您不接受此答案,我可以删除此答案,因为它不正确。但仍然太低级(无需循环,只需询问表:-)@kleopatra如果要排除只有几个可见像素的单元格,或者如果要正确使用一些复杂的表(如Jide包中的表),则必须循环。例如,某些树表中的某些行已折叠,在这种情况下,aproach将不起作用;)必须循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环循环看不出有任何理由想要排除给定高度以下的行-或者想要实际计数,或者不想要。@kleopatra在树表(或其他类似于表的结构)的情况下您可以在可见行之间获取隐藏行。这意味着当您获取lastVisibleRow firstVisibleRow值时,它可能会大于实际可见行数。这是我的观点。但一般来说,是的,您的方法会更快,花费更少的精力。重复我自己:如果jide树表计算折叠的行数(也称为:隐藏行)然后它的实现是错误的:返回的rowIndex必须在视图坐标中,并且这些坐标必须是连续的。现在运行以检查我们的JXTreeTable是否表现良好:-)
getVisibleRect
…我知道在
JTable
中有一个方法,但我在视图上的搜索没有给出任何好的结果。对此答案进行投票,需要检查是否可以删除已接受的文件