jtextfield值未更新

fcwjkofz  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(379)

我是java swing的新手。我正在使用swing创建患者注册应用程序。有一个名为clear的按钮,该按钮应清除文本字段中的输入数据。当按钮为inputpanel.clear()时,请正确设置值。但它不会更新。我的课在下面。
mainwindow类创建并显示gui。

public class MainWindow extends JFrame implements ActionListener {
private static final long serialVersionUID = 1905122041950251207L;

  transient TableRowSorter<PatientTableModel> sorter;

  private PatientListView patientListViewPanel = new PatientListView();

  private InputPanel inputPanel = new InputPanel();
  private SearchCriteriaPanel searhCriteriaPanel = new SearchCriteriaPanel(this);
    public void createAndShowGUI() {
        ButtonPanel btnPanel = new ButtonPanel(new MainWindow());
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(600, 600);

        sorter = new TableRowSorter<>(patientListViewPanel.getTableModel());

        GridBagLayout gbl = new GridBagLayout();
        this.setLayout(gbl);

        GridBagConstraints gcon = new GridBagConstraints();
        gcon.weightx = 1;
        gcon.weighty = 5;
        gcon.fill = GridBagConstraints.BOTH;

        gcon.gridx = 0;
        gcon.gridy = 0;
        gcon.gridwidth = 11;
        gcon.gridheight = 10;
        gbl.setConstraints(inputPanel, gcon);
        this.add(inputPanel);

        gcon.gridx = 4;
        gcon.gridy = 10;
        gcon.gridwidth = 11;
        gcon.gridheight = 5;
        gbl.setConstraints(btnPanel, gcon);
        this.add(btnPanel);

        gcon.gridx = 0;
        gcon.gridy = 22;
        gcon.gridwidth = 11;
        gcon.gridheight = 10;
        gbl.setConstraints(searhCriteriaPanel, gcon);
        this.add(searhCriteriaPanel);

        gcon.gridx = 0;
        gcon.gridy = 33;
        gcon.gridwidth = 11;
        gcon.gridheight = 10;
        gbl.setConstraints(patientListViewPanel, gcon);
        this.add(patientListViewPanel);

        this.setVisible(true);

        inputPanel.getNameText().addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                super.keyReleased(e);
                if (inputPanel.getNameText().getText().length() > 0)
                    btnPanel.getSaveBtn().setEnabled(true);

            }
        });

        patientListViewPanel.getTable().addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                getSelectedData();
            }
        });

    }

    public MainWindow() {
        super("Patient Registration");
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JButton button = (JButton) e.getSource();
        switch (button.getText()) {
        case "Save":
            patientListViewPanel.getTableModel()
                    .addData(inputPanel.getData(patientListViewPanel.getTable().getRowCount()));
            button.setEnabled(false);
            break;

        case "Clear":
            inputPanel.clear();
            break;

        case "Search":
            SearchCriteria s = new SearchCriteria(searhCriteriaPanel.getSearchNameText().getText(),
                    searhCriteriaPanel.getBirthYearText().getText(), searhCriteriaPanel.getMaleChkBx().isSelected(),
                    searhCriteriaPanel.getFemaleChkBx().isSelected());
            patientListViewPanel.filter(s);
            break;
        default:

        }
    }
}

inputpanel处理与输入数据相关的字段和方法

public class InputPanel extends JPanel implements PropertyChangeListener {

    /**
     * clear fields in input panel
     */
    public void clear() {

        nameText.setText(" ");
        phnText.setText("0");
        datePickerObj.jDatePicker.getJFormattedTextField().setText("");
        maleBtn.setSelected(true);
        femaleBtn.setSelected(false);
        adrsTxt.setText("");
        statusList.setSelectedIndex(4);

    }

    public InputPanel() {

        // fiels will be set to panel
        addDataChangedListner();
        isDataValid(phnText);
    }

    public void addDataChangedListner() {

        PatientData model = new PatientData();

        model.addPropertyChangeListener(this);
        nameText.getDocument().addDocumentListener(new DataChangedListener(model, "name"));

        phnText.getDocument().addDocumentListener(new DataChangedListener(model, "phnNumber"));
        adrsTxt.getDocument().addDocumentListener(new DataChangedListener(model, "address"));

    }

    @Override
    public void propertyChange(PropertyChangeEvent evt) {

        String property = evt.getPropertyName();
        String newValue = (String) evt.getNewValue();
        switch (property) {
        case "name":
            updatedName = newValue;
            break;
        case "phnNumber":
            updatedPhoneNumber = newValue;
            break;
        case "address":
            updatedAddress = newValue;
            break;
        default:
        }

    }
}

有人能帮我解决这个问题吗?

7kqas0il

7kqas0il1#

下面的mre演示了您遇到困难的基本功能:(1)

import java.awt.BorderLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class MainWindow extends JFrame{

  private InputPanel inputPanel;
  private  JButton saveBtn;

    public void createAndShowGUI() {

        inputPanel = new InputPanel();
        saveBtn = new JButton("Save");
        saveBtn.setEnabled(false);
        add(saveBtn, BorderLayout.SOUTH);

        JButton clearButton = new JButton("Clear");
        clearButton.addActionListener(e->clearInput());
        add(clearButton, BorderLayout.CENTER);

        add(inputPanel, BorderLayout.NORTH);

        inputPanel.getNameText().addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                super.keyReleased(e);
                if (inputPanel.getNameText().getText().length() > 0) {
                    saveBtn.setEnabled(true);
                }
            }
        });

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.pack();
        this.setVisible(true);
    }

    private void clearInput(){
        inputPanel.clear();
        saveBtn.setEnabled(false);
    }

    public MainWindow() {
        createAndShowGUI();
    }

    public static void main(String[] args) {
        new MainWindow();
    }
}

class InputPanel extends JPanel {

    JTextField nameText;

    /**
     * clear fields in input panel
     */
    public void clear() {
        nameText.setText(""); //use "" and not " " so text length = 0
    }

    public InputPanel() {
        nameText = new JTextField(5);
        add(nameText);
    }

    public JTextField getNameText() {
        return nameText;
    }
}

但是,更好的方法是使用模型来实现数据的更改:

public class MainWindow extends JFrame implements PropertyChangeListener{

    private InputPanel inputPanel;
    private  JButton saveBtn;
    private final PatientData model;

    public void createAndShowGUI() {

        inputPanel = new InputPanel(model);
        saveBtn = new JButton("Save");
        saveBtn.setEnabled(false);
        add(saveBtn, BorderLayout.SOUTH);

        JButton clearButton = new JButton("Clear");
        clearButton.addActionListener(e->clearInput());
        add(clearButton, BorderLayout.CENTER);

        add(inputPanel, BorderLayout.NORTH);

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.pack();
        this.setVisible(true);
    }

    private void clearInput(){
        inputPanel.clear();
    }

    public MainWindow() {
        model = new PatientData();
        model.setPropertyChangeListener(this);
        createAndShowGUI();
    }

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        updateGui();
    }

    private void updateGui() {
        if(model.getName().trim().isEmpty()){
            saveBtn.setEnabled(false);
        }else{
            saveBtn.setEnabled(true);
        }
    }

    public static void main(String[] args) {
        new MainWindow();
    }
}

class InputPanel extends JPanel implements DocumentListener{

    private final JTextField nameText;
    private final PatientData model;

    /**
     * clear fields in input panel
     */
    public void clear() {
        nameText.setText(""); //use "" and not " " so text length = 0
        updateModel();
    }

    public InputPanel(PatientData model) {
        this.model = model;
        nameText = new JTextField("",5);
        nameText.getDocument().addDocumentListener(this);
        add(nameText);
    }

    @Override
    public void changedUpdate(DocumentEvent e) {
        updateModel();
    }
    @Override
    public void removeUpdate(DocumentEvent e) {
        updateModel();
    }
    @Override
    public void insertUpdate(DocumentEvent e) {
        updateModel();
    }

    private void updateModel() {
        model.setName(nameText.getText());
    }
}

class PatientData{

    private String name ="";
    private PropertyChangeListener listener;

    public String getName() {
        return name;
    }

    public void setPropertyChangeListener(PropertyChangeListener listener) {
        this.listener = listener;
    }

    public void setName(String name) {
        String oldName = this.name;
        this.name = name;

        if(listener != null) {
            listener.propertyChange(new PropertyChangeEvent(this,"name", oldName, name));
        }
    }
}

(此处在线测试)
(1) 发布问题或答案时,请始终考虑mre。

相关问题