swingpropertychangeSupport pour mettre à jour dynamiquement jTextArea
swingpropertychangeSupport est une classe qui peut être utilisée pour surveiller les modifications des propriétés d'un objet. Cela permet à d'autres objets d'être informés lorsqu'une propriété change, ce qui peut être utile pour mettre à jour l'interface graphique en conséquence.
Dans cet exemple, nous utilisons SwingProperTyChangeSupport permettre à un tableau affiché dans une JTextArea d'être mis à jour lorsque les modifications sont entré via une boîte de dialogue d'entrée. Le tableau est mis à jour OK, mais l'interface graphique n'est pas actualisée.
Voici le code modifié:
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());
updateDisplay();
}
}
});
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);
}
public void updateDisplay() {
displayOutput.setText(mList);
}
}
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();
Clause de non-responsabilité: Toutes les ressources fournies proviennent en partie d'Internet. En cas de violation de vos droits d'auteur ou d'autres droits et intérêts, veuillez expliquer les raisons détaillées et fournir une preuve du droit d'auteur ou des droits et intérêts, puis l'envoyer à l'adresse e-mail : [email protected]. Nous nous en occuperons pour vous dans les plus brefs délais.
Copyright© 2022 湘ICP备2022001581号-3