Reputation: 177
I am trying to create a car GUI project where if I click on the name of a car in my list it will populate a nearby text box with car information. I tried using a ListValueChanges method but that did not work.
package vehicle;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class VehiclesGUI extends javax.swing.JFrame implements ListSelectionListener{
public VehiclesGUI() {
initComponents();
Vehicle[] vehicle = new Vehicle[4];
vehicle[0] = new Altima();
vehicle[1] = new Model_S();
vehicle[2] = new Taurus();
vehicle[3] = new Volt();
vehicleList.setListData(vehicle);
vehicleList.setCellRenderer(new VehicleListCellRenderer());
}
private void vehicleListValueChanged(javax.swing.event.ListSelectionEvent evt) {
Vehicle vehicle = (Vehicle)vehicleList.getSelectedValue();
txtDisplay.setText(vehicle.getName());
vehicleList.addListSelectionListener( this );
}
public static void main(String args[]) {
/* Set the Nimbus look and feel */
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new VehiclesGUI().setVisible(true);
}
EDIT: Ive implemented the ListSelectionListener and the assListSelectionListener but the textbox still says txtDisplay
Upvotes: 2
Views: 930
Reputation: 324098
I tried using a ListValueChanges method but that did not work.
I don't see where you add the listener to the JList. You need:
Check out the Swing tutorial on How to Write a List Selection Listener for a working example. The tutorial example is a better approach because its not a good idea for the class to implement the listener, but I just gave you a quick idea of what is wrong.
Upvotes: 5