I am trying to open a new JFrame when I click on the add button but I am having
ID: 3779415 • Letter: I
Question
I am trying to open a new JFrame when I click on the add button but I am having trouble adding the code correctly. Below is my code. Basically I need to know what I have to put in the add case. I attached a picture of the window I want to create when the add button is pressed.
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import java.nio.*;
import java.nio.charset.Charset;
import java.nio.file.*;
public class SwingDemo implements ActionListener
{
JTextArea txtFileOutput;
JLabel search,output, firstName, lastName, EMPLID, GPA, venusLogin;
JFileChooser fileChooser = new JFileChooser();
JFrame viewer;
JTable table;
public SwingDemo()
{
//Creates Java Frame
viewer = new JFrame("Final Project");
viewer.setSize(570, 650);
viewer.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
viewer.setLayout(new FlowLayout());
viewer.setLocationRelativeTo(viewer);
//Creates Drop Down Menu
String[] option = { "Row ID", "First Name", "Last Name", "EMPLID", "GPA", "Venus Login" };
JComboBox options = new JComboBox(option);
options.setSelectedIndex(0);
//Creates Button and Labels
search = new JLabel("Search by: ");
JButton addButton = new JButton("Add");
JButton deleteButton = new JButton("Delete");
JButton exportButton = new JButton("Export Data");
//Creates JTable
JTextField textBox = new JTextField(20);
Object rowData[][] = { {null, null, null, null, null, null},
{null, null, null, null, null, null} };
Object columnNames[] = { "Row ID", "First Name", "Last Name", "EMPLID", "GPA", "Venus Login"};
table = new JTable(rowData, columnNames);
JScrollPane scrollPane = new JScrollPane(table);
table.setFillsViewportHeight(true);
//Creates Menu Bar
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenu helpMenu = new JMenu("Help");
menuBar.add(fileMenu);
menuBar.add(helpMenu);
JMenuItem openAction = new JMenuItem("Open");
JMenuItem exitAction = new JMenuItem("Exit");
JMenuItem exportAction = new JMenuItem("Export");
JMenuItem aboutAction = new JMenuItem("About");
fileMenu.add(openAction);
fileMenu.add(exportAction);
fileMenu.add(exitAction);
helpMenu.add(aboutAction);
fileChooser.setDialogTitle("Choose a file");
//ActionListener
openAction.addActionListener(this);
exitAction.addActionListener(this);
aboutAction.addActionListener(this);
addButton.addActionListener(this);
//Action Events
//Adds Elements to Java Frame
viewer.setJMenuBar(menuBar);
viewer.add(search);
viewer.add(options);
viewer.add(textBox);
viewer.add(addButton);
viewer.add(deleteButton);
viewer.add(scrollPane);
viewer.add(exportButton);
viewer.setVisible(true);
}
public void addB()
{
JFrame addFrame;
JTextField first, last, emplid, gpa, venus = new JTextField ();
addFrame = new JFrame ("Add Student Information");
addFrame.setSize(570, 650);
addFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addFrame.setLayout(new FlowLayout());
addFrame.setLocationRelativeTo(addFrame);
addFrame.add(firstName);
//addFrame.add(first);
addFrame.add(lastName);
//addFrame.add(last);
addFrame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e)
{
switch(e.getActionCommand())
{
case "Exit":
System.exit(0);
break;
case "Open":
fileChooser.setDialogTitle("Import a txt file ");
int userAction = fileChooser.showOpenDialog(null);
if(userAction == JFileChooser.APPROVE_OPTION)
{
try
{
FileReader fr = new FileReader(fileChooser.getSelectedFile());
}
catch (FileNotFoundException ex)
{
output.setText("File is not found");
}
catch (IOException e1)
{
JOptionPane.showMessageDialog(fileChooser, "There was an IO Exception that was caught. Error: "+ e1.getMessage(), null, JOptionPane.ERROR_MESSAGE);
e1.printStackTrace();
}
}
else
{
JOptionPane.showMessageDialog(fileChooser, "File Not Selected", null, JOptionPane.ERROR_MESSAGE);
}
break;
case "About":
JOptionPane.showMessageDialog(viewer, "This is an App made by");
break;
case "Add":
addB();
break;
}
}
}
Explanation / Answer
public void addB()
{
JTextField firstName=new JTextField(10), lastName=new JTextField(10), emplid=new JTextField(10), gpa=new JTextField(10), venus = new JTextField (10);
JFrame frame=new JFrame("Create New Student Record");
JPanel jPanel=new JPanel(new GridLayout(6, 1));
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(frame);
JPanel first=new JPanel(new FlowLayout());
first.add(new JLabel("First Name: "));
first.add(firstName);
jPanel.add(first);
JPanel second=new JPanel(new FlowLayout());
second.add(new JLabel("Last Name: "));
second.add(lastName);
jPanel.add(second);
JPanel third=new JPanel(new FlowLayout());
third.add(new JLabel("CUNY ID: "));
third.add(emplid);
jPanel.add(third);
JPanel fourth=new JPanel(new FlowLayout());
fourth.add(new JLabel("GPA: "));
fourth.add(gpa);
jPanel.add(fourth);
JPanel fifth=new JPanel(new FlowLayout());
fifth.add(new JLabel("Venus Login: "));
fifth.add(venus);
jPanel.add(fifth);
JPanel sixth=new JPanel(new FlowLayout());
JButton jButton1=new JButton("Add New Student");
JButton jButton=new JButton("Cancel");
sixth.add(jButton1);
sixth.add(jButton);
jPanel.add(sixth);
frame.add(jPanel);
frame.setVisible(true);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.