JTable动态加载表格

JTable动态加载表格,数据是模拟的

这里写图片描述

public class LayoutTest extends JFrame {

    /** serialVersionUID = 4567832137864545L */
    private static final long serialVersionUID = 4567832137864545L;
    /** 获取屏幕大小所用的变量 */
    private Dimension screeSize;
    private Insets insets;
    /** 图片路径,image文件夹在项目根目录下 */
    public final static String IMAGEFILEPATH = "image" + File.separator;

    private static JTable table = null;

    private LayoutTest() {
        try {
            // 设置显示样式
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 获取屏幕大小
        screeSize = Toolkit.getDefaultToolkit().getScreenSize();
        insets = Toolkit.getDefaultToolkit().getScreenInsets(this.getGraphicsConfiguration());
        // 设置窗口大小和位置,下面代码代表全屏
        // this.setSize((screeSize.width - insets.left - insets.right),
        // (screeSize.height - insets.top - insets.bottom));
        this.setSize((screeSize.width - insets.left - insets.right) /2,
                (screeSize.height - insets.top - insets.bottom) *2/3);
        this.setTitle("动态表格测试");
        // 设置窗体图标,需要的时候用
        // String logoPath = IMAGEFILEPATH + "bz.png";
        // this.setIconImage(new ImageIcon(logoPath).getImage());

        JScrollPane scrollPanelService = new JScrollPane(getJTable());
        this.getContentPane().add(scrollPanelService);
        this.setVisible(true);
        this.setLocationRelativeTo(null); // 窗口居于屏幕正中央
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }


    public static JTable getJTable(){
        if(table==null){
            table = new JTable();
        }
        String[] columnName = {"序号", "第一个字段","第二个字段", "第三个字段"}; 
        Object[][] rowData = getAcceptAreaTableResult(); 
        table.setModel(new DefaultTableModel(rowData, columnName));
        return table;
    }

    /**
     * 处理结果集
     * @return
     */
    private static Object[][] getAcceptAreaTableResult() {
        //模拟数据
        Vector< Vector<Object>> nodeMessageQuery = new Vector<Vector<Object>>();
        Vector<Object> v = new Vector<Object>();
        v.add(new Random().nextInt(999));
        v.add(new Random().nextInt(200));
        v.add(new Random().nextInt(200));
        nodeMessageQuery.add(v);
        Vector<Object> v1 = new Vector<Object>();
        v1.add(new Random().nextInt(999));
        v1.add(new Random().nextInt(200));
        v1.add(new Random().nextInt(200));
        nodeMessageQuery.add(v1);
        Vector<Object> v2 = new Vector<Object>();
        v2.add(new Random().nextInt(999));
        v2.add(new Random().nextInt(200));
        v2.add(new Random().nextInt(200));
        nodeMessageQuery.add(v2);

        //处理数据,添加序号的大小以及返回的结果类型 
        int h = 1;
        List<List<Object>> listResult = new ArrayList<List<Object>>();
        List<Object> stringList = null;

        Object[][] result = null;
        for (int i = 0; i < nodeMessageQuery.size(); i++) {
            stringList = new ArrayList<Object>();
            for (int j = 0; j < nodeMessageQuery.get(i).size()+1; j++) {
                        if(j==0){
                            stringList.add(Integer.toString(h));//序号
                        }else{
                            stringList.add(nodeMessageQuery.get(i).get(j-1));
                        }
                }
            h++;
            if(stringList!=null){
                listResult.add(stringList);
            }
        }
        result = new Object[listResult.size()][4];
        //此for循环是将集合中的值赋值给数组当中
        for (int i = 0; i < listResult.size(); i++) {//行
            for (int j = 0; j < 4; j++) {//列
                if(listResult.get(i).size()!=0){
                    result[i][j] = listResult.get(i).get(j);
                }
            }
        }
        return result;
    }

    public static void main(String[] args) {

        new LayoutTest();
        int delay = 2000; // seconds
        ActionListener taskTimer = new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                getJTable();
            }
        };
        new Timer(delay,taskTimer).start();
    }

}

表格的样式设计:如,表格内容居中,设计斑马线,字体的大小等等

public static void makeFace(JTable table) {
        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer(){
            /**serialVersionUID*/
            private static final long serialVersionUID = 1234579841267L;

            @Override
            public Component getTableCellRendererComponent(JTable table,
                    Object value, boolean isSelected,boolean hasFocus,
                    int row, int column) {
                if(row%2==0){
                    setBackground(new Color(202,241,245));
                }else{
                    setBackground(new Color(255,255,255));
                }

                setHorizontalAlignment(JLabel.CENTER);// 表格内容居中
                ((DefaultTableCellRenderer) table.getTableHeader().getDefaultRenderer())
                .setHorizontalAlignment(DefaultTableCellRenderer.CENTER);// 列头内容居中
                 table.getTableHeader().setFont(new Font("微软雅黑", Font.PLAIN, 13));
                 table.getTableHeader().setResizingAllowed(true);
                 table.setRowHeight(26);//设置行高

                return super.getTableCellRendererComponent(table, value, 
                        isSelected, hasFocus, row, column);
            }
        };
        for (int i = 0; i < table.getColumnCount(); i++) {
            TableColumn col = table.getColumn(
                    table.getColumnName(i));
            col.setCellRenderer(renderer);
        }
        table.getTableHeader().setBackground(new Color(51, 102, 255));
    }

运行有结果如下:

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值