JTable——三种基础创建方法

1.方法1

        不提供任何数据,也不创建列和行。

1.1代码

public class Main {

    public static void main(String[] args){
        JFrame jf = new JFrame("JTable");
        jf.setBounds(400, 200, 1000, 800);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JScrollPane scrollPane=new JScrollPane();
        JTable table=new JTable();
        GroupLayout groupLayout=new GroupLayout(jf.getContentPane());
        groupLayout.setHorizontalGroup(groupLayout.createSequentialGroup()
                .addComponent(scrollPane));
        groupLayout.setVerticalGroup(groupLayout.createSequentialGroup()
                .addComponent(scrollPane));
        scrollPane.setViewportView(table);
        jf.setVisible(true);
    }
}

1.2效果

方法一效果

        从图片可以直观的看出,生成的就是零行零列的表,从界面来看就是一片空白,啥都没有,没啥好说的。

2.方法2

        不提供任何数据,但是创建列和行。

2.1代码

public class Main {

    public static void main(String[] args){
        JFrame jf = new JFrame("JTable");
        jf.setBounds(400, 200, 1000, 800);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JScrollPane scrollPane=new JScrollPane();
        JTable table=new JTable(10,3);
        GroupLayout groupLayout=new GroupLayout(jf.getContentPane());
        groupLayout.setHorizontalGroup(groupLayout.createSequentialGroup()
                .addComponent(scrollPane));
        groupLayout.setVerticalGroup(groupLayout.createSequentialGroup()
                .addComponent(scrollPane));
        scrollPane.setViewportView(table);
        jf.setVisible(true);
    }
}

2.2效果

方法二效果-1

        从图中可以很容易地发现一个有意思的地方,那就是表的列头竟然是ABC,因为我们是没有提供表头的,那么如果把当前的3列改为27,会怎么样呢?


方法二效果-2

        通过上面两张图,可以很明显的得出一个结论,就是在我们没有提供列头的情况下,Java会默认提供列头。但是这种情况,很明显不是我们想要的显示结果。

3.方法3

        提供数据(单元格内容和列头)并且创建列和行。

3.1代码

public class Main {

    public static void main(String[] args) {
        JFrame jf = new JFrame("JTable");
        jf.setBounds(400, 200, 1000, 800);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JScrollPane scrollPane = new JScrollPane();
        JTable table = getThreeMethodTable();
        GroupLayout groupLayout = new GroupLayout(jf.getContentPane());
        groupLayout.setHorizontalGroup(groupLayout.createSequentialGroup()
                .addComponent(scrollPane));
        groupLayout.setVerticalGroup(groupLayout.createSequentialGroup()
                .addComponent(scrollPane));
        scrollPane.setViewportView(table);
        jf.setVisible(true);
    }

    private static JTable getThreeMethodTable() {
        String[][] data = new String[][]{{"1", "1", "1"}, {"2", "2", "2"}, {"3", "3", "3"}};
        String[] columnNames = new String[]{"test1", "test2", "test3"};
        JTable jTable = new JTable(data, columnNames);
        return jTable;
    }
}

3.2效果

方法三效果

        这种方法是一般最常用的方法,也是最基础最简单的方法,但正因为最基础,所以有很大局限性,比如说显示自定义类型的数据,就需要另写渲染,以及添加索引行,移动数据、排序、显示图片等等都不适合用这种方法,而上面提到的在Table显示图片,移动数据、排序等内容会在以后文章中陆续总结。但是,如果只是用来显示一些基础类型的数据,不会对数据做动态操作,那么这种方法是完全够用了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.ios高性能编程 (1).内层 最小的内层平均值和峰值(2).耗电量 高效的算法和数据结构(3).初始化时...
    欧辰_OSR阅读 29,662评论 8 265
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,148评论 1 32
  • 北京的讲座很多,只要想看那是每周都有,我偶尔也去。去讲座当然是为了听嘉宾分享,但去的次数多了,渐渐觉得提问互动环节...
    玛雅蓝阅读 293评论 0 0
  • 每次值班都是思考的好时光。没有楼上永远停不下的咣咣铛铛;没有爸妈偶尔的催促;这大片大片的时间都是我的。 今天我想说...
    松鼠的核桃仁阅读 452评论 1 1
  • 羅老头3L阅读 173评论 0 0