维护表格模型

Posted dulute

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了维护表格模型相关的知识,希望对你有一定的参考价值。

public class ExampleFrame_06 extends JFrame{

    private DefaultTableModel tableModel;
    private JTable table ;
    private JTextField aTextField;
    private JTextField bTextField;
    
    public ExampleFrame_06() {
        // TODO Auto-generated constructor stub
        super();
        setTitle("维护表格模型");
        setBounds(100,100,510,375);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        final JScrollPane scrollPane = new JScrollPane();
        getContentPane().add(scrollPane, BorderLayout.CENTER);
        String[] columnNames = {"A","B"};
        String[][] tableValues = {{"A1","B1"},{"A2","B2"},{"A3","B3"}};
        tableModel = new DefaultTableModel(tableValues, columnNames);
        
        table = new JTable(tableModel);
        table.setRowSorter(new TableRowSorter<>(tableModel));
        
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        
        table.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                int selectedRow = table.getSelectedRow();
                Object oa = tableModel.getValueAt(selectedRow, 0);
                Object ob = tableModel.getValueAt(selectedRow, 1);
                aTextField.setText(oa.toString());
                bTextField.setText(ob.toString());
            }
        });
        
        scrollPane.setViewportView(table);
        final JPanel panel = new JPanel();
        getContentPane().add(panel,BorderLayout.SOUTH);
        panel.add(new JLabel("A: "));
        aTextField = new JTextField("A4",10);
        panel.add(aTextField);
        panel.add(new JLabel("B4",10));
        bTextField = new JTextField("B4",10);
        panel.add(bTextField);
        
        
        final JButton addbutton = new JButton("添加");
        addbutton.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                String[] rowValues = {aTextField.getText(),bTextField.getText()};
                tableModel.addRow(rowValues);
                int rowCount = table.getRowCount()+1;
                aTextField.setText("A" + rowCount);
                bTextField.setText("B" + rowCount);
            }
        });
        
        
        panel.add(addbutton);
        
        final JButton upButton = new JButton("修改");
        upButton.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                int selectedRow = table.getSelectedRow();
                if(selectedRow != -1 ){
                    tableModel.setValueAt(aTextField.getText(), selectedRow, 0);
                    tableModel.setValueAt(bTextField.getText(), selectedRow, 1);
                }
            }
        });
        panel.add(upButton);
        
        final JButton delButton = new JButton("删除");
        delButton.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                int selectedRow = table.getSelectedRow();
                if(selectedRow != -1){
                    tableModel.removeRow(selectedRow);
                }
            }
        });
        panel.add(delButton);
    }
    
    
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ExampleFrame_06 exampleFrame_06 = new ExampleFrame_06();
        exampleFrame_06.setVisible(true);
    }

}

 

以上是关于维护表格模型的主要内容,如果未能解决你的问题,请参考以下文章

android片段表格布局

查看发票组代码后的总结和有感

AJAX相关JS代码片段和部分浏览器模型

使用片段时 Intellij 无法正确识别 Thymeleaf 模型变量

php 一个自定义的try..catch包装器代码片段,用于执行模型函数,使其成为一个单行函数调用

如何防止在背面片段导航上再次设置视图模型