The program uses a text file input.txt containing the information below: Bobby J
ID: 3621421 • Letter: T
Question
The program uses a text file input.txt containing the information below:Bobby Jones 1 15.30
Dorthy Smith 2 12.30
John Tolbert 3 11.90
Adam Johnson 2 10.30
Michael Daniel 2 18.55
Gilbert Jones 3 13.97
_______________________________
The text file contains student name, student type, and plan rate.
The text from the file above is to be read and displayed in combobox but displayed in the following format last name, first initial (Jones, B) with the default as select student.
I have the combobox reading the lines of text from input.txt but it is not displayed in the format that I need (last name, first initial).
I need to store the student type and rate for use to be displayed in text fields on the panel.
How can I modify my program below to display the names in the correct format without changing the text file, input.txt?
_________________________________________
The results should show in a text field on the GUI panel:
_______________________________________________________
| |Please select student| <===combobox |___| <==textbox
|Student Name: First Last Name (from input.txt) <==output in text fields on panel
|Student Type: Freshman, SophomoreJunior, or Senior |
|Plan Rate: (from input.txt) |
|Calculated Plan Cost: Plan Cost |
| |
| _____ |
| |clear| |
|_______________________________________________|
Above is a rough of what the GUI should look like containing a combobox, textbox, textfields and clear button.
Below is the code that I have, the names show up in the drop down menu of the combo box but not in the correct format of last, first initial(the text file that is read from cannot be changed). There is no textbox for user input of weeks meal plan used and no text fields for the output.
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class JComboBoxSample extends JFrame {
private JComboBox comboBox;
private JButton jbtclr;
//main method
public static void main(String[] args) {
new JComboBoxSample();
}
public JComboBoxSample() {
super("Meal Plan");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
setSize(400, 400);
setLocationRelativeTo(null);
JPanel jpTextFields = new JPanel();
comboBox = new JComboBox();
add(comboBox);
jbtclr = new JButton("Clear");
jbtclr.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
displaySelectedItem();
}
});
add(jbtclr);
loadDataFromFile();
setVisible(true);
}
private void displaySelectedItem() {
Object item = comboBox.getSelectedItem();
if (item == null) {
JOptionPane.showMessageDialog(null, "No item selected");
} else {
// JOptionPane.showMessageDialog(null, item.toString());
}
}
private void loadDataFromFile() {
Scanner fileReader = new Scanner(getClass().getResourceAsStream(
"input.txt"));
try {
DefaultComboBoxModel model = new DefaultComboBoxModel();
while (fileReader.hasNextLine()) {
model.addElement(fileReader.nextLine());
}
comboBox.setModel(model);
} finally {
fileReader.close();
}
}
}
Explanation / Answer
please rate - thanks
to get you started
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
@SuppressWarnings("serial")
public class JComboBoxSample extends JFrame{
private JComboBox comboBox;
private JButton jbtclr;
private JTextField nameBox;
public String last[]=new String[10];
public String first[]=new String[10];
public int type[]=new int[10];
public double rate[]=new double[10];
//main method
public static void main(String[] args) {
new JComboBoxSample();
}
public JComboBoxSample() {
super("Meal Plan");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
setSize(400, 400);
setLocationRelativeTo(null);
JPanel jpTextFields = new JPanel();
comboBox = new JComboBox();
add(comboBox);
nameBox = new JTextField(10);
jbtclr = new JButton("Clear");
jbtclr.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
displaySelectedItem();
}
});
add(nameBox);
add(jbtclr);
loadDataFromFile();
setVisible(true);
}
private void displaySelectedItem() {
Object item = comboBox.getSelectedItem();
if (item == null) {
JOptionPane.showMessageDialog(null, "No item selected");
} else {
JOptionPane.showMessageDialog(null, item.toString());
}
}
private void loadDataFromFile() {
int i=0;
String out;
Scanner fileReader = new Scanner(getClass().getResourceAsStream(
"input.txt"));
try {
DefaultComboBoxModel model = new DefaultComboBoxModel();
while (fileReader.hasNext()) {
first[i]=fileReader.next();
last[i]=fileReader.next();
type[i]=fileReader.nextInt();
rate[i]=fileReader.nextDouble();
out=last[i]+", "+first[i].charAt(0);
model.addElement(out);
i++;
}
comboBox.setModel(model);
} finally {
fileReader.close();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.