In: Computer Science
Write a program in Java, that creates a Jframe with a menu containing only file. Inside file there should be items: Open, Save, and Save As. Selecting open prompts the user to input a file name to a txt document containing employee information and displays a Jtable with the information, which can be edited. With column headers {"First Name" , "Last Name" , "Occupation" , "Office #"}
Example: Gary Osbourn Teacher 113
Michelle Ramirez Teacher 101
Ava Gomez Principal 120
The user should be able to save any changes made to the table with save. Using Save As the program should ask the user for a file name to save the new program as.
import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Test { public JFrame frame; public JTextField text; public JLabel message; public JTable table; public JButton save,open,saveas; public Test() { String cols[] = {"First Name","Last Name","Occupation","Office #"}; frame = new JFrame("Table Software"); frame.setLayout(new FlowLayout()); text = new JTextField(); text.setBounds(10,10,400,40); message = new JLabel(""); table = new JTable(); save = new JButton("Save"); open = new JButton("Open"); saveas = new JButton("Save As"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(500,500); JPanel panel = new JPanel(); panel.setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.fill = GridBagConstraints.HORIZONTAL; gbc.gridx = 1; gbc.gridy = 0; panel.add(open,gbc); gbc.gridx = 0; gbc.gridy = 0; panel.add(save,gbc); gbc.gridx = 2; gbc.gridy = 0; panel.add(saveas,gbc); gbc.fill = GridBagConstraints.HORIZONTAL; gbc.gridx = 0; gbc.gridy = 3 ; gbc.gridwidth = 3; panel.add(text,gbc); gbc.fill = GridBagConstraints.HORIZONTAL; gbc.gridx = 0; gbc.gridy = 5 ; gbc.gridwidth = 5; panel.add(table,gbc); frame.add(panel); frame.setVisible(true); open.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { } }); saveas.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { } }); save.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { } }); } public static void main(String Args[]) { Test t = new Test(); } }
This is the base construct. Please build over this.