how to add an attribute from an object to a JScrollPane in java
I have declared an ArrayList and DefaultListModel
DefaultListModel model;
List<AddFlight> Flights = new ArrayList<AddFlight>();
To this list I add an object as an element
Flights.add(new AddFlight(txtFlightNo.getText(),
(String)cmbMechanicalStatus.getSelectedItem(),
(String)cmbMedicalStatus.getSelectedItem(),
Float.valueOf((txtFuelLevel.getText()).trim()).floatValue(),
(String)cmbWeatherCondition.getSelectedItem(),
(String)cmbFrequency.getSelectedItem()));
In the action Performed (ActionEvent e) I want to print each object in one line in a understandable way on the JScrollPane.
model.add(0,Flights);
My output looks like this [When I add 2 objects to the ArrayList]:
WHAT I WANT:
1) I want each object in the ArrayList to appear in one line.
2) And I want it to appear like this in the JScrollPane:
Flight No: UL209, Mechanical Status:OK, Medical Status : Failure,Fuel Level : 12.0 //Line one
Flight No: UL210, Mechanical Status:OK, Medical Status : OK,Fuel Level : 22.0 // Line two
However, I managed to print out each element using the code below :
for (AddFlight Flight : Flights) {
System.out.println("FLight No : " + Flight.getFlightNo());
System.out.println("Mechanical Status : " + Flight.getMechanicalStatus());
System.out.println("Medical Status : " + Flight.getMedicalStatus());
System.out.println("Fuel Level : " + Flight.getFuelLevel());
System.out.println("Weather Condition: " + Flight.getWeatherCondition());
System.out.println("Frequency : " + Flight.getFrequency());
}
MY FULL CODE
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Flight implements ActionListener {
//String array
String[] MechanicalStatus = {"Select", "Ok", "Failure"};
String[] MedicalStatus = {"Select", "Ok", "Failure"};
String[] WeatherCondition = {"Select", "Sunny", "Raining", "Thunder", "Hurricane"};
String[] Frequency = {"Will be AutoSet", "ATC 1", "ATC 2", "ATC 3"};
//JPanel
JPanel pnlInput = new JPanel(new GridLayout(7, 2, 20, 20));
//Add textfields here
JTextField txtFlightNo = new JTextField(8);
JComboBox cmbMechanicalStatus = new JComboBox(MechanicalStatus);
JComboBox cmbMedicalStatus = new JComboBox(MedicalStatus);
JTextField txtFuelLevel = new JTextField(8);
JComboBox cmbWeatherCondition = new JComboBox(WeatherCondition);
JComboBox cmbFrequency = new JComboBox(Frequency);
//Add labels here
JLabel lblFlightNo = new JLabel("Flight No : ");
JLabel lblMechanicalStatus = new JLabel("Mechanical Status : ");
JLabel lblMedicalStatus = new JLabel("Medical Status : ");
JLabel lblFuelLevel = new JLabel("Fuel Level (gallons) : ");
JLabel lblWeatherCondition = new JLabel("Weather Condition :");
JLabel lblFrequency = new JLabel("Frequency : ");
List<AddFlight> Flights = new ArrayList<AddFlight>();
DefaultListModel model;
public Flight(DefaultListModel model) {
this.model = model;
//Adding flightno to panel
pnlInput.add(lblFlightNo);
pnlInput.add(txtFlightNo);
//Adding mechanicalstatus to the panel
pnlInput.add(lblMechanicalStatus);
pnlInput.add(cmbMechanicalStatus);
//Adding medicalstatus to the panel
pnlInput.add(lblMedicalStatus);
pnlInput.add(cmbMedicalStatus);
//Adding fuellevel to the panel
pnlInput.add(lblFuelLevel);
pnlInput.add(txtFuelLevel);
//Adding weathercondition to the panel
pnlInput.add(lblWeatherCondition);
pnlInput.add(cmbWeatherCondition);
//Adding frequency to the panel
pnlInput.add(lblFrequency);
pnlInput.add(cmbFrequency);
}
public void actionPerformed(ActionEvent e) {
int result = JOptionPane.showConfirmDialog(null, pnlInput, "Flight Details",
JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
System.out.println("Plane [" + txtFlightNo.getText() + "] has arrived at the airport.\n");
System.out.println("Mechanical status " + cmbMechanicalStatus.getSelectedItem() + "\n");
}
Flights.add(new AddFlight(txtFlightNo.getText(),
(String) cmbMechanicalStatus.getSelectedItem(),
(String) cmbMedicalStatus.getSelectedItem(),
Float.valueOf((txtFuelLevel.getText()).trim()).floatValue(),
(String) cmbWeatherCondition.getSelectedItem(),
(String) cmbFrequency.getSelectedItem()));
//persons.add(new AddFlight("UL210", "FAILURE", "OK", 90, "Rainy", "ATC 2"));
for (AddFlight Flight : Flights) {
System.out.println("FLight No : " + Flight.getFlightNo());
System.out.println("Mechanical Status : " + Flight.getMechanicalStatus());
System.out.println("Medical Status 开发者_运维技巧: " + Flight.getMedicalStatus());
System.out.println("Fuel Level : " + Flight.getFuelLevel());
System.out.println("Weather Condition: " + Flight.getWeatherCondition());
System.out.println("Frequency : " + Flight.getFrequency());
}
model.add(0, Flights);
}
}
Any Help is appreciated...
You're displaying your list twice in your control. Additionally, if you want to display relevant information rather than simply the object type and memory address, you need to define a toString
method:
public String toString()
{
return "Flight No: " + flightNo + ", Mechanical Status:" + mechStatus + ", Medical Status : " + medStatus + ",Fuel Level : " + fuel;
}
EDIT:
It looks like, in order to display each AddFlight
object on one line, you should be using
model.add(0, Flights.get(Flights.size() - 1));
(Add each AddFlight
object to your model individually)
Instead of
model.add(0,Flights);
(Adding the entire ArrayList
to your model each time you add a new element)
精彩评论