Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

3. (TCOs 1–6) A trucking company has an application to track data from their tru

ID: 3822927 • Letter: 3

Question

3. (TCOs 1–6) A trucking company has an application to track data from their trucks regarding miles driven and fuel used each month. This information is input from a GUI and stored in a file named “trucks.txt”. The GUI has the following components. Component Type Purpose txtID JTextField Input for TruckID txtMiles JTextField Input for miles driven txtFuel JTextField Input for fuel used btnSave JButton Click to save data to the file The data should be saved to the file with delimiters between the fields, as shown below. TruckID#miles#fuel The class method saveFile() is called by the action listener to do the work. Write the code for this method. Make sure to validate that all data is present. (Points : 15)

Explanation / Answer

Hi,

Please find the java swing code below-

import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class TruckData{
public static void main(String args[]) {
JFrame frame = new JFrame("Truck Details Here");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new BorderLayout());

JLabel label = new JLabel("Truck ID:");
JTextField txtID = new JTextField();

JLabel label = new JLabel("Miles :");
JTextField txtMiles = new JTextField();

JLabel label = new JLabel("Fuel :");
JTextField txtFuel = new JTextField();

JButton btnSave = new JButton("Save");
  
frame.setSize(250, 150);
frame.setVisible(true);

textField.setText("your text");
String filename = "test.txt";

FileWriter writer = null;

//Button click code

btnSave.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(this, textBox.getText());
}
try {
writer = new FileWriter(filename);
txtID.write(writer);
txtMiles.write(writer);
txtFuel.write(writer);

} catch (IOException exception) {
System.err.println("Save oops");
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException exception) {
System.err.println("Error closing writer");
exception.printStackTrace();
}
}
}
});
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote