简体   繁体   中英

IDEA Java swing GUI form Jtable. table model is overwritten by auto generated code

Some tutorials say I can use Jtable like this:

JTable table = new JTable(myModel); // where myModel is a table model

But how can I do that if the IDEA GUI Form automatically generates the code? I can't use the constructor function to pass the model.

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by mark on 17-11-8.
 */
public class StudentManager {
    private JTextField textField1;
    private JTextField textField2;
    private JTextField textField3;
    private JTextField textField4;
    private JTextField textField5;
    private JPanel myPanel;
    private JButton saveButton;
    private JButton sumButton;
    private JButton averageButton;
    private JButton sortButton;
    private JButton button5;
    private JTable table1;


    public StudentManager() {

        saveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

            }
        });
    }

    public static void main(String[] args) {

        JFrame frame = new JFrame("StudentManager");
        frame.setContentPane(new StudentManager().myPanel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}

Your generated code :

JTable table = new JTable();

Later, your code to set the model :

DefaultTableModel model = new DefaultTableModel();
model.addRow(...*your data*...);
table.setModel(model);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM