如何通过单击按钮添加包含行和列的新JTable?

时间:2015-06-05 08:48:38

标签: java swing jtable

我是Java的初学者,我需要帮助。

我想在点击按钮时在JFrameJPanel中添加包含行和列的表格。如何通过单击按钮添加包含行和列的新表?

1 个答案:

答案 0 :(得分:0)

  1. 首先,您必须将JButton添加到第
  2. 在按钮
  3. 中添加ActionListener
  4. 比按下按钮时列表

    JButton b = new JButton("Click Me");
    b.addActionListener(new ActionListener() {
    
    public void actionPerformed(ActionEvent e) {
    
    String[] columnNames = {"First Name",
                "Last Name",
                "Sport",
                "# of Years",
                "Vegetarian"};
    
    Object[][] data = {
                {"Kathy", "Smith",
                 "Snowboarding", new Integer(5), new Boolean(false)},
                {"John", "Doe",
                 "Rowing", new Integer(3), new Boolean(true)},
                {"Sue", "Black",
                 "Knitting", new Integer(2), new Boolean(false)},
                {"Jane", "White",
                 "Speed reading", new Integer(20), new Boolean(true)},
                {"Joe", "Brown",
                 "Pool", new Integer(10), new Boolean(false)}
            };
    
        JTable table = new JTable(data, columnNames);
        add(table);
    
    }
    });
    
  5. 有关JButton& JTable