JButtons necesitan modificar 8 JTextFields utilizando una matriz. Escuchar a los botones o texto?
-
21-09-2019 - |
Pregunta
Esta es la tarea, y esta pregunta Extiende éste
Así que hay un botón para First
, Prev
, Next
y Last
Cada uno debe modificar
Item ID, Name, Rating, Price, Units, Value, Fee, ValueW/Fee, Total Inventory Value
El último es un total estática de todas las unidades.
No estoy seguro de si debería hacer cada botón se consiguen múltiples llamadas de este tipo.
productName.setText( product.getProductName() );
itemNumber.setText( String.valueOf( product.getItemNumber() ) );
O hacer que cada JTextArea escuchan para el botón a continuación, cambiar su campo. Hace que el trabajo aún?
Solución
Registrar un ActionListener para cada botón. En el cuerpo del método actionPerformed de que ActionListener, obtener el elemento de visualización y pasarlo a un método que será responsable de establecer los valores de los campos de texto.
Algo así como:
JButton button = new JButton("Next");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DVDObject obj = getNextDVD();
populateFields(obj);
}
});
...
private DVDObject getNextDVD() {
// gets the next object to display
// you could call this method for each of the buttons,
// passing in an argument that determines which Object
// to return (first, last, next, previous, whatever)
}
private void populateFields(DVDObject dvd) {
// write out the values from the object passed in to the
// fields
}
supongo que tienes algún tipo de colección de objetos que contienen toda la información sobre DVDs, he tomado una puñalada en la oscuridad y lo llamó "DVDObject" aquí.