1 question regarding textfield
hi all,
i wish to read the text input by user on 1 class and use that textfield input in another class to create a file.
but currently , when i just click the menu item, it auto create the file out with the default text in the textfield.
i attached my code in below, tried to attached the file but unable to attach.
the one in bold are the 2 classes i trying to get the thing to work.
Any help is greatly appreciated.
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
public class Gui extends JFrame implements ActionListener, ItemListener
{
Container contentPanel = getContentPane();
JMenuBar menuBar,menuBar2,menuBar3,menuBar4,menuBar5,menuBar6;
JMenu menu1,menu2,menu3,menu4,menu5,menu6;
JMenuItem menuItem,menuItem2,menuItem3,menuItem4,menuItem5,menuItem6;
JPopupMenu popup;
String idText;
public JFrame frame,frame2,frame3,frame4,frame5,frame6;
public Gui()
{
frame = new JFrame("MDIS Student Regstration System");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400 , 200);
frame.setVisible(true);
frame.setResizable(false);
//frame.setLocationRelativeTo(null);
contentPanel = frame.getContentPane();
contentPanel.setLayout(new BorderLayout());
contentPanel.add(getHeader(), BorderLayout.NORTH);
//=====================================================================================
//This section creates the menubar and menu items
menuBar = new JMenuBar();
//File Menu
menu1 = new JMenu("File");
menuItem = new JMenuItem("Quit");
menu1.add(menuItem);
menuBar.add(menu1);
//This exit the Gui when the quit button is clicked.
menuItem.addActionListener(new quitButton());
frame.setJMenuBar(menuBar);
//Student Menu
menu2 = new JMenu("Menu");
menuItem = new JMenuItem("Enroll a new student");
menu2.add(menuItem);
menuItem.addActionListener(new enrollNewStudent());
menuItem = new JMenuItem("Remove an existing tudent");
menu2.add(menuItem);
menuItem.addActionListener(new deleteStudent());
menuItem = new JMenuItem("Modify existing student information");
menu2.add(menuItem);
menuItem.addActionListener(new modifyStudent());
menuBar.add(menu2);
frame.setJMenuBar(menuBar);
//Search Menu
menu3 = new JMenu("Search");
menuItem = new JMenuItem("Search");
menu3.add(menuItem);
menuItem.addActionListener(new search());
menuBar.add(menu3);
frame.setJMenuBar(menuBar);
//Help Menu
menu4 = new JMenu("Help");
menuItem = new JMenuItem("About...");
menu4.add(menuItem);
menuItem = new JMenuItem("FAQ");
menu4.add(menuItem);
menuItem = new JMenuItem("Support");
menu4.add(menuItem);
menuItem = new JMenuItem("Getting Started");
menu4.add(menuItem);
menuBar.add(menu4);
frame.setJMenuBar(menuBar);
menuBar.setVisible(true);
}
//=====================================================================================
//Main Method
public static void main (String [] args)
{
new Gui();
}
//=====================================================================================
//=====================================================================================
//This enables the title to be displayed in the north of the border
protected JComponent getHeader()
{
JLabel label = new JLabel("Student Registration System", JLabel.CENTER);
label.setFont(new Font("Courier", Font.BOLD, 16));
return label;
}
//=====================================================================================
//=====================================================================================
//This allow another layout (flow layout) to be put inside the border layout center
protected JComponent deleteStudent()
{
JPanel panel = new JPanel();
JLabel idLabel = new JLabel("Enter the id of the Student you wish to remove:");
JTextField idTextField = new JTextField(5);
JButton deleteButton = new JButton("Submit");
panel.setLayout(new FlowLayout());
panel.add(idLabel);
panel.add(idTextField);
panel.add(deleteButton);
return panel;
}
//=====================================================================================
//=====================================================================================
//This allow another layout (Gridbaglayout) to be put inside the border layout center
protected JComponent modifyStudent()
{
JPanel panel = new JPanel();
JLabel idLabel = new JLabel("Enter the id of the Student you wish to remove:");
JTextField idTextField = new JTextField(5);
JButton deleteButton = new JButton("Submit");
panel.setLayout(new FlowLayout());
panel.add(idLabel);
panel.add(idTextField);
panel.add(deleteButton);
return panel;
}
//=====================================================================================
//=====================================================================================
//This class quit the program
class quitButton implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
//=====================================================================================
//=====================================================================================
//This class hide the pop up window for enroll new student
class hideEnrollFrame implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
frame2.setVisible(false);
}
}
//=====================================================================================
//=====================================================================================
//This class hide the pop up window for delete existing student
class hideDeleteFrame implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
frame3.setVisible(false);
}
}
//=====================================================================================
//=====================================================================================
//This class hide the pop up window for delete existing student
class hideModifyFrame implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
frame4.setVisible(false);
}
}
//=====================================================================================
//=====================================================================================
//This class hide the pop up window for delete existing student
class hideSearchFrame implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
frame5.setVisible(false);
}
}
//=====================================================================================
//=====================================================================================
//This class hide the pop up window for delete existing student
class hideSearchResult implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
frame6.setVisible(false);
}
}
//=====================================================================================
//=====================================================================================
//This class hide the pop up window for delete existing student
class searchResult implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
frame6 = new JFrame("MDIS Student Regstration System");
frame6.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
frame6.setSize(340 , 450);
frame6.setVisible(true);
frame6.setResizable(false);
frame6.setLocationRelativeTo(null);
contentPanel = frame6.getContentPane();
contentPanel.setLayout(new BorderLayout());
JPanel panel = new JPanel(new GridBagLayout());
frame6.getContentPane().add(panel, BorderLayout.NORTH);
//This section creates the menubar and menu items
menuBar6 = new JMenuBar();
//File Menu
menu6 = new JMenu("File");
menuItem6 = new JMenuItem("Quit");
menu6.add(menuItem6);
menuBar6.add(menu1);
//This exit the Gui when the quit button is clicked.
menuItem6.addActionListener(new hideSearchResult());
frame6.setJMenuBar(menuBar6);
}
}
//=====================================================================================
//=====================================================================================
//This class pop up a new window for enroll a new student
[B]class enrollNewStudent implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
frame2 = new JFrame("MDIS Student Regstration System");
frame2.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
frame2.setSize(340 , 450);
frame2.setVisible(true);
frame2.setResizable(false);
frame2.setLocationRelativeTo(null);
contentPanel = frame2.getContentPane();
contentPanel.setLayout(new BorderLayout());
JPanel panel = new JPanel(new GridBagLayout());
frame2.getContentPane().add(panel, BorderLayout.NORTH);
GridBagConstraints c = new GridBagConstraints();
JLabel id = new JLabel("Student ID(*)");
c.gridx = 0;
c.gridy = 0;
//c.weightx = 4;
//c.weighty = 4;
c.insets = new Insets(1,10,10,10);
c.fill = GridBagConstraints.BOTH;
panel.add(id, c);
JTextField idField = new JTextField(10);
idField.setText("Please enter NRIC for Id");
c.gridx = 0;
c.gridy = 1;
c.fill = GridBagConstraints.BOTH;
panel.add(idField, c);
JLabel name = new JLabel("Student Name(*)");
c.gridx = 0;
c.gridy = 2;
c.fill = GridBagConstraints.BOTH;
panel.add(name, c);
JTextField nameField = new JTextField(20);
nameField.setText("Please enter Student Name.");
c.gridx = 0;
c.gridy = 3;
c.fill = GridBagConstraints.BOTH;
panel.add(nameField, c);
JLabel courseBox = new JLabel("Select your desire course(*)");
c.gridx = 0;
c.gridy = 4;
c.fill = GridBagConstraints.BOTH;
panel.add(courseBox, c);
String [] courseList = {"Test 1" , "Test 2" , "Test 3"};
JComboBox course = new JComboBox(courseList);
course.setSelectedIndex(2);
c.gridx = 0;
c.gridy = 5;
panel.add(course, c);
JLabel code = new JLabel("Enter the Course Code(*)");
c.gridx = 0;
c.gridy = 6;
panel.add(code, c);
JTextField courseField = new JTextField(5);
courseField.setText("Please enter Course Code.");
c.gridx = 0;
c.gridy = 7;
c.fill = GridBagConstraints.BOTH;
panel.add(courseField, c);
JLabel stage = new JLabel("Select the Course Stages");
c.gridx = 0;
c.gridy = 8;
c.fill = GridBagConstraints.BOTH;
panel.add(stage, c);
String stage1 = "Stage 1";
String stage2 = "Stage 2";
String stage3 = "Stage 3";
JRadioButton stage1Button = new JRadioButton(stage1);
JRadioButton stage2Button = new JRadioButton(stage2);
JRadioButton stage3Button = new JRadioButton(stage3);
JPanel radioPanel = new JPanel(new FlowLayout());
ButtonGroup group = new ButtonGroup();
group.add(stage1Button);
group.add(stage2Button);
group.add(stage3Button);
radioPanel.add(stage1Button);
radioPanel.add(stage2Button);
radioPanel.add(stage3Button);
c.gridx = 0;
c.gridy = 9;
c.fill = GridBagConstraints.BOTH;
panel.add(radioPanel, c);
JButton submit = new JButton("Submit");
try
{
submit.addActionListener(new submitNewEnroll(idField.getText()));
}
catch (IOException ee)
{
}
JButton clear = new JButton("Clear");
JButton close = new JButton("Quit");
close.addActionListener(new hideEnrollFrame());
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(submit);
buttonPanel.add(clear);
buttonPanel.add(close);
c.gridx = 0;
c.gridy = 10;
c.fill = GridBagConstraints.BOTH;
panel.add(buttonPanel, c);
//This section creates the menubar and menu items
menuBar2 = new JMenuBar();
//File Menu
menu2 = new JMenu("File");
menuItem2 = new JMenuItem("Quit");
menu2.add(menuItem2);
menuBar2.add(menu2);
//This exit the Gui when the quit button is clicked.
menuItem2.addActionListener(new hideEnrollFrame());
frame2.setJMenuBar(menuBar2);
}
}[/B]
//=====================================================================================
//=====================================================================================
//This class pop up a new window for delete a new student
class deleteStudent implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
frame3 = new JFrame("MDIS Student Regstration System");
frame3.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
frame3.setSize(600 , 200);
frame3.setVisible(true);
frame3.setResizable(false);
frame3.setLocationRelativeTo(null);
contentPanel = frame3.getContentPane();
contentPanel.setLayout(new BorderLayout());
JLabel label = new JLabel("Enter the ID or NRIC of the student you wish to remove. ");
JTextField idField = new JTextField(10);
JPanel deletePanel = new JPanel(new GridBagLayout());
JButton submit = new JButton("Submit");
JTextArea resultArea = new JTextArea();
frame3.getContentPane().add(deletePanel, BorderLayout.NORTH);
deletePanel.add(label);
deletePanel.add(idField);
deletePanel.add(submit);
deletePanel.add(resultArea);
//This section creates the menubar and menu items
menuBar3 = new JMenuBar();
//File Menu
menu3 = new JMenu("File");
menuItem3 = new JMenuItem("Quit");
menu3.add(menuItem3);
menuBar3.add(menu3);
//This exit the Gui when the quit button is clicked.
menuItem3.addActionListener(new hideDeleteFrame());
frame3.setJMenuBar(menuBar3);
}
}
//=====================================================================================
//=====================================================================================
//This class pop up a new window for modify student detail
class modifyStudent implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
frame4 = new JFrame("MDIS Student Regstration System");
frame4.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
frame4.setSize(340 , 450);
frame4.setVisible(true);
frame4.setResizable(false);
frame4.setLocationRelativeTo(null);
contentPanel = frame4.getContentPane();
contentPanel.setLayout(new BorderLayout());
JPanel panel = new JPanel(new GridBagLayout());
frame4.getContentPane().add(panel, BorderLayout.NORTH);
GridBagConstraints c = new GridBagConstraints();
JLabel id = new JLabel("Student ID");
c.gridx = 0;
c.gridy = 0;
c.insets = new Insets(1,5,5,5);
c.fill = GridBagConstraints.BOTH;
panel.add(id, c);
JTextField idField = new JTextField(10);
idField.setText("Please enter NRIC for Id");
c.gridx = 0;
c.gridy = 1;
c.fill = GridBagConstraints.BOTH;
panel.add(idField, c);
JLabel name = new JLabel("Student Name");
c.gridx = 0;
c.gridy = 2;
c.fill = GridBagConstraints.BOTH;
panel.add(name, c);
JTextField nameField = new JTextField(20);
nameField.setText("Please enter Student Name.");
c.gridx = 0;
c.gridy = 3;
c.fill = GridBagConstraints.BOTH;
panel.add(nameField, c);
JLabel courseBox = new JLabel("Select your desire course");
c.gridx = 0;
c.gridy = 4;
c.fill = GridBagConstraints.BOTH;
panel.add(courseBox, c);
String [] courseList = {"Test 1" , "Test 2" , "Test 3"};
JComboBox course = new JComboBox(courseList);
course.setSelectedIndex(2);
c.gridx = 0;
c.gridy = 5;
panel.add(course, c);
JLabel code = new JLabel("Enter the Course Code");
c.gridx = 0;
c.gridy = 6;
panel.add(code, c);
JTextField courseField = new JTextField(5);
courseField.setText("Please enter Course Code.");
c.gridx = 0;
c.gridy = 7;
c.fill = GridBagConstraints.BOTH;
panel.add(courseField, c);
JLabel stage = new JLabel("Select the Course Stages");
c.gridx = 0;
c.gridy = 8;
c.fill = GridBagConstraints.BOTH;
panel.add(stage, c);
String stage1 = "Stage 1";
String stage2 = "Stage 2";
String stage3 = "Stage 3";
JRadioButton stage1Button = new JRadioButton(stage1);
JRadioButton stage2Button = new JRadioButton(stage2);
JRadioButton stage3Button = new JRadioButton(stage3);
JPanel radioPanel = new JPanel(new FlowLayout());
ButtonGroup group = new ButtonGroup();
group.add(stage1Button);
group.add(stage2Button);
group.add(stage3Button);
radioPanel.add(stage1Button);
radioPanel.add(stage2Button);
radioPanel.add(stage3Button);
c.gridx = 0;
c.gridy = 9;
c.fill = GridBagConstraints.BOTH;
panel.add(radioPanel, c);
JButton submit = new JButton("Update");
JButton clear = new JButton("Clear");
JButton close = new JButton("Quit");
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(submit);
buttonPanel.add(clear);
buttonPanel.add(close);
c.gridx = 0;
c.gridy = 10;
c.fill = GridBagConstraints.BOTH;
panel.add(buttonPanel, c);
JTextArea resultArea = new JTextArea();
resultArea.setPreferredSize(new Dimension(50,70));
c.gridx = 0;
c.gridy = 11;
c.fill = GridBagConstraints.BOTH;
panel.add(resultArea, c);
//This section creates the menubar and menu items
menuBar4 = new JMenuBar();
//File Menu
menu4 = new JMenu("File");
menuItem4 = new JMenuItem("Quit");
menu4.add(menuItem4);
menuBar4.add(menu4);
//This exit the Gui when the quit button is clicked.
menuItem4.addActionListener(new hideModifyFrame());
frame4.setJMenuBar(menuBar4);
}
}
//=====================================================================================
//=====================================================================================
//This class create the pop up frame for search
class search implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
frame5 = new JFrame("MDIS Student Regstration System");
frame5.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
frame5.setSize(600 , 350);
frame5.setVisible(true);
frame5.setResizable(false);
frame5.setLocationRelativeTo(null);
JPanel panel = new JPanel(new GridBagLayout());
frame5.getContentPane().add(panel, BorderLayout.NORTH);
GridBagConstraints c = new GridBagConstraints();
JLabel label = new JLabel("Enter Keyword to search:");
c.gridx = 0;
c.gridy = 0;
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(1,5,5,5);
panel.add(label, c);
JTextField searchField = new JTextField();
searchField.setPreferredSize(new Dimension(100,10));
c.gridx = 1;
c.gridy = 0;
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(1,5,5,5);
panel.add(searchField, c);
JButton submit = new JButton("Submit");
c.gridx = 2;
c.gridy = 0;
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(1,5,5,5);
submit.addActionListener(new searchResult());
panel.add(submit, c);
//This section creates the menubar and menu items
menuBar5 = new JMenuBar();
//File Menu
menu5 = new JMenu("File");
menuItem5 = new JMenuItem("Quit");
menu5.add(menuItem5);
menuBar5.add(menu5);
//This exit the Gui when the quit button is clicked.
menuItem5.addActionListener(new hideSearchFrame());
frame5.setJMenuBar(menuBar5);
}
}
//=====================================================================================
//=====================================================================================
//This class create a new file and saved the detail of the newly enrolled student
[B]class submitNewEnroll implements ActionListener
{
public submitNewEnroll(String nn) throws IOException
{
File f = new File(nn +".txt");
f.createNewFile();
}
public void actionPerformed(ActionEvent e)
{
}
}
[/B]
public void actionPerformed(ActionEvent e)
{
//...Get information from the item event...
//...Display it in the text area...
}
public void itemStateChanged(ItemEvent e)
{
//...Get information from the item event...
//...Display it in the text area...
}
}
Moderator Edit: Code tags added