Create a java program to do the following: Create a UI dashboard to view the pla
ID: 3827855 • Letter: C
Question
Create a java program to do the following:
Create a UI dashboard to view the player's info.
You can save 5 text fies with 5 player's information (name dob height weight yearsPlayed pointspergame) with the name of the file as playername.txt.)
Create a Graphical User Interface that has a drop down for player's name.The drop down should have five player's name.(hard code the players names. Make sure that the file names are same)
If you select the player's name from drop down, it should read from the corresponding file and populate UI with the player's Info from the file.
The UI text boxes( for dob, height, weight, yearsPlayed, pointspergame ) should be populated with the data from the file.
Explanation / Answer
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class DashBoard{
private JFrame mainFrame;
private JPanel controlPanel;
JComboBox<String> nameCombo = new JComboBox<String>();
File folder = new File("./");
File[] listOfFiles = folder.listFiles();
JLabel lable = new JLabel("a jaf aksfj akdfas ");
public DashBoard(){
prepareGUI();
}
public static void main(String[] args){
DashBoard swingControlDemo = new DashBoard();
swingControlDemo.LineUpDemo();
}
private void prepareGUI(){
mainFrame = new JFrame("Dash Board");
mainFrame.setSize(500,400);
mainFrame.setLayout(null);
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
controlPanel = new JPanel();
controlPanel.setLayout(null);
mainFrame.add(controlPanel);
mainFrame.setVisible(true);
}
private void LineUpDemo(){
JLabel namelabel= new JLabel("User ID: ", JLabel.RIGHT);
JButton loginButton = new JButton("OK");
nameCombo.addItem("Select Name");
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
String name = listOfFiles[i].getName();
if( name.contains(".txt") ){
nameCombo.addItem(listOfFiles[i].getName());
//System.out.println("File " + listOfFiles[i].getName());
}
}
}
loginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String type = "Home";
}
});
nameCombo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try{
Scanner sc = new Scanner(new File((String)nameCombo.getSelectedItem()));
lable.setText(""+sc.next()+" "+sc.next()+" "+sc.next()+" "+sc.next()+" "+sc.next()+" "+sc.next()+"");
}
catch(Exception ex){
ex.printStackTrace();
}
}
});
loginButton.setBounds(50, 300, 70, 30);
JButton exit = new JButton("Exit");
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
exit.setBounds(200, 300, 70, 30);
nameCombo.setBounds(150, 100, 150, 30);
controlPanel.add(nameCombo);
lable.setBounds(50, 200, 400, 30);
controlPanel.add(lable);
controlPanel.add(namelabel);
controlPanel.add(loginButton);
controlPanel.add(exit);
controlPanel.setBounds(0, 0, 700, 700);
mainFrame.setVisible(true);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.