码迷,mamicode.com
首页 > 其他好文 > 详细

维护表格模型

时间:2019-04-09 12:40:46      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:void   opera   generated   jtable   exit   content   operation   one   gets   

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);
    }

}

 

维护表格模型

标签:void   opera   generated   jtable   exit   content   operation   one   gets   

原文地址:https://www.cnblogs.com/dulute/p/10676124.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!