AbstractTableModel getValueAt性能 [英] AbstractTableModel getValueAt perfomance

查看:135
本文介绍了AbstractTableModel getValueAt性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 JTable 的新手,也许我不明白。

I am newbie in JTable, maybe I don't understand something.

假设我有 ArrayList 1000 学生 id,姓名,年龄)。我希望以 JTable 显示所有学生。据我所知,我必须创建 StudentTableModel 扩展AbstractTableModel 并设置 StudentTableModel JTable 。因此,我们可以将 StudentTableModel 视为我们的 ArrayList 与表之间的适配器。在互联网上,我发现 getValueAt 的示例实现:

Let's suppose I have ArrayList of 1000 Students (id, name, surname, age). And I want to show all students in JTable. As far as I understood I must create StudentTableModel that extends AbstractTableModel and set StudentTableModel to JTable. Therefore we can consider StudentTableModel as an "adapter" between our ArrayList and the table. On the internet I found such example implementation of getValueAt:

 public Object getValueAt(int row, int col) {
      Student student = arrayList.get(row);
      switch (col) {
      case 0:
        return student.getId();
      case 1:
        return student.getName();
      case 2:
        return student.getSurname();
      case 3:
        return student.getAge();
      }
    }

问题是有1000名学生(行)和4个字段(列)我们将运行此开关4000次。请解释我的错误或告诉我更好的解决方案。

The problem is that having 1000 students (rows) and 4 field (columns) we will run this switch 4000 times. Please explain what I do wrong or tell about a better solution.

推荐答案

拥有1000名学生(行)和4个领域(列),我们将运行此开关4000次。

前提是错误的,但你应该个人资料进行验证。 JTable 使用 flyweight模式 单元格呈现,所以仅检查可见单元格。这个简化的示例说明了基本机制。这个相关的示例可以很好地扩展到数千行。

The premise is false, but you should profile to verify. JTable uses the flyweight pattern for cell rendering, so only visible cells will be examined. This simplified example illustrates the essential mechanism. This related example scales well into the thousands of rows.

这篇关于AbstractTableModel getValueAt性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆