SwingPropertyChangeSupport 动态更新 JTextArea [英] SwingPropertyChangeSupport to dynamically update JTextArea

查看:36
本文介绍了SwingPropertyChangeSupport 动态更新 JTextArea的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试回答有关 SwingPropertyChangeSupport 的问题

I'm trying to build upon the answer to a question regarding SwingPropertyChangeSupport

我正在尝试修改非常有用的充满鳗鱼的气垫船的答案中给出的代码:WindowListener 未按预期工作,以允许显示数组通过输入对话框输入更改时更新.

I am attempting to modify the code given here in an answer by the very helpful, Hovercraft Full Of Eels: WindowListener does not work as expected, to allow a displayed array to be updated when changes are entered via an input dialog.

阵列已更新,但未在 GUI 中刷新.我希望有人能告诉我哪里出错了.

The array is updated OK but not refreshed in the GUI. I was hoping someone might be able to tell me where I've gone wrong.

代码如下:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.event.SwingPropertyChangeSupport;

public class Main {
public static void main(String[] arg) {
    GuiForUpdate display = new GuiForUpdate();
    display.setVisible(true);
}
}

class GuiForUpdate extends JFrame implements ActionListener {

/**
 * 
 */
private static final long serialVersionUID = 1L;
private FocusListener focusListener;
private String mList;
private JButton changeArrayButton;
private JTextArea codeIn, displayOutput;
private int arrayIndex;
private JPanel displayPanel;
private ArrayForUpdating arrayForUpdate = new ArrayForUpdating();

public GuiForUpdate() {
    setSize(224, 180);
    layoutLeft();
    layoutDisplay();
    layoutBottom();
}

/**
 * adds a display area for array
 */
public void layoutDisplay() {
    displayPanel = new JPanel();
    add(displayPanel, BorderLayout.CENTER);
    displayOutput = new JTextArea();
    displayPanel.add(displayOutput);
    displayOutput.addFocusListener(focusListener);

    mList = arrayForUpdate.getBoundProperty();

    arrayForUpdate.addPropertyChangeListener(new PropertyChangeListener() {

        @Override
        public void propertyChange(PropertyChangeEvent pcEvt) {
            if (pcEvt.getPropertyName().equals(
                    ArrayForUpdating.BOUND_PROPERTY)) {
                mList = (pcEvt.getNewValue().toString());
            }
        }
    });

    displayOutput.setText(mList);
}

/**
 * adds left hand side elements to left of GUI
 */
public void layoutLeft() {
    JPanel left = new JPanel();
    add(left, BorderLayout.WEST);
    codeIn = new JTextArea(2, 2);
    left.add(codeIn);
    codeIn.addFocusListener(focusListener);
}

/**
 * adds bottom elements to bottom of GUI
 */
public void layoutBottom() {
    JPanel bottom = new JPanel();
    changeArrayButton = new JButton("Modify array");
    changeArrayButton.addActionListener(this);
    bottom.add(changeArrayButton);
    add(bottom, BorderLayout.SOUTH);
}

/**
 * Process button clicks
 */
public void actionPerformed(ActionEvent ae) {

    if (ae.getSource() == changeArrayButton) {

        // first check if any code entered
        if (codeIn.getText().trim().length() != 0) {

            // call modifyMemory() method
            modifyArray();

        } else
            JOptionPane.showMessageDialog(null,
                    "Please enter something first.");
    }
}

/**
 * method to process modify array
 */
public void modifyArray() {

    // show dialog to retrieve entered address
    String addressToModify = (String) JOptionPane
            .showInputDialog("At which location?");

    // confirm if a string was entered
    if ((addressToModify != null) && (addressToModify.length() > 0)) {

        // convert to integer if decimal address entered
        arrayIndex = Integer.parseInt(addressToModify);
    }
    // pass as integer
    processInput(arrayIndex);
}

public void processInput(int a) {

    String newValue = codeIn.getText();
    arrayForUpdate.instructionsIn(newValue, a);
}
}

class ArrayForUpdating {

public static final String BOUND_PROPERTY = "bound property";
private String boundProperty = "";

private SwingPropertyChangeSupport spcSupport = new SwingPropertyChangeSupport(
        this);
private StringBuilder mList;
private int[] myArray;

public ArrayForUpdating() {

    myArray = new int[5];
    for (int i = 0; i < myArray.length; i++) {
        myArray[i] = 0;
    }
    setArrayyDisplayString();
}

/** 
 * method to create formatted string of array
 */
public void setArrayyDisplayString() {

    // create StringBuilder for display in memory tab
    mList = new StringBuilder();
    for (int i = 0; i < myArray.length; i++) {

        mList.append(String.format("%10s %02d %10s %02d", "Pos:   ", i,
                "Value:  ", myArray[i]));
        mList.append("\n");
    }
    setBoundProperty(mList.toString());
}

/**
 * This method takes in a string passed through from the GUI
 */
public void instructionsIn(String codeIn, int loc) {

    String code = codeIn.trim();
    int oc = Integer.parseInt(code);

    // add the data to the array
    setArrayData(loc, oc);
    loc++;
}

/**   
 * method to add data to the array
 */
public void setArrayData(int a, int memData) {
    myArray[a] = memData;

    // print array to console for checking
    for (int i = 0; i < myArray.length; i++) {
        System.out.println("location: " + i + " value: " + myArray[i]);
    }
    setArrayyDisplayString();
}

public SwingPropertyChangeSupport getSpcSupport() {
    return spcSupport;
}

public void setSpcSupport(SwingPropertyChangeSupport spcSupport) {
    this.spcSupport = spcSupport;
}

public String getBoundProperty() {
    return boundProperty;
}

public void setBoundProperty(String boundProperty) {
        String oldValue = this.boundProperty;
        System.out.println("old = " + oldValue);
        String newValue = boundProperty;
        System.out.println("new = " + newValue);
        this.boundProperty = newValue;
        spcSupport.firePropertyChange(BOUND_PROPERTY, oldValue, newValue);
    }

    public void addPropertyChangeListener(PropertyChangeListener listener) {
    spcSupport.addPropertyChangeListener(listener);
    }
}

-编辑-希望删除编译错误.

-EDIT- Hopefully removed compilation errors.

推荐答案

当我将 displayOutput.setText(mList) 放在 propertyChange 方法中时,它起作用了:

When I put displayOutput.setText(mList) within the propertyChange method, it worked:

@Override
public void propertyChange(PropertyChangeEvent pcEvt) {
    if (pcEvt.getPropertyName().equals(
        ArrayForUpdating.BOUND_PROPERTY)) {
        mList = (pcEvt.getNewValue().toString());
        displayOutput.setText(mList);
    }
}

感谢大家的帮助,尤其是您的耐心.

Thank you to everyone for your help and especially your patience.

这篇关于SwingPropertyChangeSupport 动态更新 JTextArea的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆