zoukankan      html  css  js  c++  java
  • Java Swing实现展示数据,以及过滤排序


    public class RelationCostctrTable extends DefaultTableModel {
    
        public RelationCostctrTable(Vector<Vector<String>> tableValues, Vector<String> columnNames) {
            super(tableValues, columnNames);
        }
        
        @Override
        public boolean isCellEditable(int row, int column) {
            
            return false;
        }
        
    }
    public class JtableDemo4 extends JFrame {
        JtableDemoTbale2 defaultTableModel;
        /**
         * 取消按钮
         */
        private JButton cancelBtn;
        // 选中行索引
        int selectedRow;
        
        public static void main(String[] args) {
            JtableDemo4 demo = new JtableDemo4();
            demo.setVisible(true);
        }
    
        public JtableDemo4() {
            this.setTitle("进项发票关联成本合同");
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setSize(700, 450);
            // JFrame窗体居中
            this.setLocationRelativeTo(null);
            String src = "/image/logo.png"; // 图片路径
            Image image = null;
            try {
                image = ImageIO.read(this.getClass().getResource(src));
            } catch (IOException e) {
                e.printStackTrace();
            }
            // 创建图片对象
            this.setIconImage(image); // 设置图标
            final JScrollPane scrollPane = new JScrollPane();
            this.getContentPane().add(scrollPane, BorderLayout.CENTER);
            // 设置JTable中的列名
            Vector<String> columnNames = new Vector<String>();
            columnNames.add("组织");
            columnNames.add("合同编码");
            columnNames.add("合同主键");
            Vector<Vector<String>> tableValues = new Vector<Vector<String>>();
            for (int row = 1; row < 29; row++) {
                Vector<String> rowV = new Vector<String>();
                rowV.add("A" + row);
                rowV.add("B" + row);
                rowV.add("C" + row);
                tableValues.add(rowV);
            }
            // 创建JTable (表格)
            final JTable table = new JTable();
            // 创建表格模型
            defaultTableModel = new JtableDemoTbale2(tableValues, columnNames);
    
            final TableRowSorter<JtableDemoTbale2> sorter = new TableRowSorter<JtableDemoTbale2>(
                    defaultTableModel);
            table.setRowSorter(sorter);
            
            // 设置JTable的表格模型
            table.setModel(defaultTableModel);
            table.getTableHeader().setReorderingAllowed(false);
            /*
             * 将JTable添加到JScrollPane中,
             */
            scrollPane.setViewportView(table);
            // 设置表格的选择模式---为单选模式
            table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    
            final JPanel panel = new JPanel();
            panel.setBackground(Color.gray);
            this.getContentPane().add(panel, BorderLayout.SOUTH);
            final JTextField filterText = new JTextField(20);
            JButton gl = new JButton("过滤");
            JButton glcbht = new JButton("关联成本合同");
            panel.add(filterText);
            panel.add(gl);
            panel.add(glcbht);
            /**
             * 点击JScrollPane滚动面板,取消选中的行
             */
            scrollPane.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    // 取消掉选中行,但是选中行的索引值还是在的,没有取消掉
                    table.clearSelection();
                    // 显示选中行的索引值。
                    System.out.println("-----------------" + selectedRow);
                }
            });
            gl.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    String text = filterText.getText();
                    if (text.length() == 0) {
                        sorter.setRowFilter(null);
                    } else {
                        sorter.setRowFilter(RowFilter.regexFilter(text));
                    }
                }
            });
    
            glcbht.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    // 获取到JTable中选定行的---索引值
                    selectedRow = table.getSelectedRow();
                    // 获取到指定单元格的值
                    if(selectedRow < 0)
                        return;
                    int i = table.convertRowIndexToModel(table.getSelectedRow());
                    Object oa = defaultTableModel.getValueAt(i, 0);
                    Object ob = defaultTableModel.getValueAt(i, 1);
                    Object oc = defaultTableModel.getValueAt(i, 2);
                    int row = selectedRow+1;
                    System.out.println("第" + row + "行," + "内容:" + oa + " "
                            + ob + " " + oc);
                }
            });
        }
    
    }

    作者:冬瓜茶饮料
    出处:http://www.cnblogs.com/dongguacha/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    从零开始搭建Wpf基础9-使用MaterialDesignToolkit对界面进行美化下
    从零开始搭建Wpf基础8-登录界面成功后显示主窗体
    从零开始搭建Wpf基础7-Api数据接入
    从零开始搭建Wpf基础6-Tab选项卡MVVM实现
    Wpf下dragablz使用Prism8进行导航-3
    从零开始搭建Wpf基础5-无限级菜单MVVM实现
    从零开始搭建Wpf基础篇4-使用Prism进行模块化
    从零开始搭建Wpf初学篇3-界面模块化
    手写es5和es6实现原型继承
    判断类型(通用类型检测方法)和手写深拷贝
  • 原文地址:https://www.cnblogs.com/dongguacha/p/7762681.html
Copyright © 2011-2022 走看看