Home About Projects Resume Contact

School Project Source Code

This application demonstrates inputting your name into a text box, if you’re a male, female or robot. It also uses radio buttons as well as check boxes asking what operating system you use. It also demonstrates saving, loading, clearing the fields as well as exiting the program. This is one of my first college projects.

    //import...maybe more than I need.
    import javax.swing.*;
    import java.awt.BorderLayout;
    import java.awt.GridLayout;
    import java.awt.event.*;
    import java.io.*;
    public class Window extends JFrame implements ActionListener {
        //Text Field inputs and Labels
        private JTextField input1;
        private JTextField input2;
        private JTextField input3;
        private JLabel firstname;
        private JLabel lastname;
        private JLabel middlename;

        //Check Box and Label
        private JLabel check;
        private JCheckBox chk;
        private JCheckBox chk2;
        private JCheckBox chk3;

        //Radio button
        private JRadioButton radio1;
        private JRadioButton radio2;
        private JRadioButton radio3;


        //Window

        public Window() {
            super("Project 4"); //Title for window
            setupButtons();
            setupTextFields();
            setupCheckbox();
            setupRadio();
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setLocation(100, 100);
            pack();
            setVisible(true);

        }

        //setting up the buttons
        private void setupButtons() {
            //Creating the Buttons & the Panel
            JPanel bPanel = new JPanel();

            bPanel.setLayout(new GridLayout(4, 1));

            //JButtons Clear, Save, Load, Quit
            JButton clearButton = new JButton("Clear");
            JButton saveButton = new JButton("Save");
            JButton loadButton = new JButton("Load");
            JButton quitButton = new JButton("Quit");

            //adds buttons to bPanel
            bPanel.add(clearButton);
            bPanel.add(saveButton);
            bPanel.add(loadButton);
            bPanel.add(quitButton);

            //Places the panel with the buttons to
            //the left side of program
            add(BorderLayout.WEST, bPanel);

            //
            clearButton.addActionListener(this);
            saveButton.addActionListener(this);
            loadButton.addActionListener(this);
            quitButton.addActionListener(this);

        }


        private void setupTextFields() {
            JPanel textPanel = new JPanel();
            textPanel.setLayout(new GridLayout(3, 1));


            //First Text Box (First Name) with label
            JPanel firstPanel = new JPanel();
            input1 = new JTextField(10);
            firstname = new JLabel("First Name:");
            firstPanel.add(firstname);
            firstPanel.add(input1);

            textPanel.add(firstPanel);

            //Second Text Box (Middle Letter) with label
            JPanel sPanel = new JPanel();
            middlename = new JLabel("Middle Name:");
            input2 = new JTextField(1);
            sPanel.add(middlename);
            sPanel.add(input2);

            textPanel.add(sPanel);

            //Thrid TextBox (Last Name) with label
            JPanel tPanel = new JPanel();
            lastname = new JLabel("Last Name:");
            input3 = new JTextField(10);
            tPanel.add(lastname);
            tPanel.add(input3);

            textPanel.add(tPanel);


            add(BorderLayout.CENTER, textPanel);

        }

        private void setupCheckbox() {

            //Check Box layout
            JPanel cPanel = new JPanel();
            cPanel.setLayout(new GridLayout(1, 3));
            //Check Box
            chk = new JCheckBox("Windows");
            chk2 = new JCheckBox("Mac");
            chk3 = new JCheckBox("Linux");

            //Label for check box
            check = new JLabel("What do you use?:");

            //adds label and checkboxes to the panel
            cPanel.add(check);
            cPanel.add(chk);
            cPanel.add(chk2);
            cPanel.add(chk3);

            //Puts cPanel in the bottom of program
            add(BorderLayout.SOUTH, cPanel);

            //If check boxes are selected it will
            //be pickup sent to the actionPerformed
            chk.addActionListener(this);
            chk2.addActionListener(this);
            chk3.addActionListener(this);


        }


        private void setupRadio() {
            //Radio Buttons layout
            JPanel rPanel = new JPanel();
            rPanel.setLayout(new GridLayout(3, 1));

            //Male radio button
            radio1 = new JRadioButton("Male", false);
            radio1.setActionCommand("Male");

            //Female radio button
            radio2 = new JRadioButton("Female", false);
            radio2.setActionCommand("Female");

            //Robot radio button
            radio3 = new JRadioButton("Robot", false);
            radio3.setActionCommand("Robot");

            rPanel.add(radio1);
            rPanel.add(radio2);
            rPanel.add(radio3);

            //adds radio buttons to the right of program
            add(BorderLayout.EAST, rPanel);

            //If radio buttons are selected it will
            //be pickup sent to the actionPerformed
            radio1.addActionListener(this);
            radio2.addActionListener(this);
            radio3.addActionListener(this);

        }

        //Tell what the buttons do
        //like Quit, clear, save, load
        public void actionPerformed(ActionEvent e) {
            //Setting up
            String buttonName = e.getActionCommand();

            //Quit
            if (buttonName.equals("Quit"))
                System.exit(0);

            //Clear
            if (buttonName.equals("Clear")) {
                //sets the text field to blank
                input1.setText("");
                input2.setText("");
                input3.setText("");

                //sets the check boxes to unselected
                chk.setSelected(false);
                chk2.setSelected(false);
                chk3.setSelected(false);

                //sets the radio buttons to unselected
                radio1.setSelected(false);
                radio2.setSelected(false);
                radio3.setSelected(false);
            }


            //If one radio button is selected the others will be unselected
            if (buttonName.equals("Male")) {

                radio2.setSelected(false);
                radio3.setSelected(false);
            }
            if (buttonName.equals("Female")) {
                radio1.setSelected(false);
                radio3.setSelected(false);
            }
            if (buttonName.equals("Robot")) {
                radio1.setSelected(false);
                radio2.setSelected(false);
            }


            //Save

            if (buttonName.equals("Save")) {
                PrintWriter pw;
                try {
                    //saves everything to Project4.txt
                    pw = new PrintWriter("Project4.txt");

                    //creating a string so it can be save line by line
                    //so it later can be easier to read
                    String s = input1.getText();
                    String ss = input2.getText();
                    String sss = input3.getText();
                    String r = radio1.getActionCommand();
                    String rr = radio2.getActionCommand();
                    String rrr = radio3.getActionCommand();
                    String c = chk.getActionCommand();
                    String cc = chk2.getActionCommand();
                    String ccc = chk3.getActionCommand();

                    //Sets text, radio and check boxes next line
                    //below
                    s = s + "\n";
                    ss = ss + "\n";
                    sss = sss + "\n";

                    r = r + "\n";
                    rr = rr + "\n";
                    rrr = rrr + "\n";

                    c = c + "\n";
                    cc = cc + "\n";
                    ccc = ccc + "\n";
                    //Above
                    //Sets text, radio and check boxes next line

                    //Prints Text fields to text field
                    pw.print(s);
                    pw.print(ss);
                    pw.print(sss);

                    //Prints radio button whatever was selected
                    //to text file
                    if (radio1.isSelected())
                        pw.print(r);
                    if (radio2.isSelected())
                        pw.print(rr);
                    if (radio3.isSelected())
                        pw.print(rrr);

                    //prints Checkboxes to text file
                    if (chk.isSelected())
                        pw.print(c);
                    if (chk2.isSelected())
                        pw.print(cc);
                    if (chk3.isSelected())
                        pw.print(ccc);

                    pw.close();
                } catch (FileNotFoundException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

                //String message = "The file has been Saved";



            }
            //Load
            if (buttonName.equals("Load")) {
                try {
                    // Open the file
                    FileInputStream fstream = new FileInputStream("Project4.txt");
                    // Get the object of DataInputStream
                    DataInputStream in = new DataInputStream(fstream);
                    BufferedReader br = new BufferedReader(new InputStreamReader( in ));

                    //Creates string for reading each line
                    String strLine;
                    String strLine2;
                    String strLine3;
                    String strLine4;
                    String strLine5;
                    String strLine6;
                    String strLine7;

                    //Read File Line By Line
                    strLine = br.readLine();
                    strLine2 = br.readLine();
                    strLine3 = br.readLine();
                    strLine4 = br.readLine();
                    strLine5 = br.readLine();
                    strLine6 = br.readLine();
                    strLine7 = br.readLine();

                    // Print the content
                    //TextFields
                    input1.setText(strLine);
                    input2.setText(strLine2);
                    input3.setText(strLine3);
                    //Radio Button
                    if (strLine4.equals("Male"))
                        radio1.setSelected(true);
                    if (strLine4.equals("Female"))
                        radio2.setSelected(true);
                    if (strLine4.equals("Robot"))
                        radio3.setSelected(true);

                    //CheckBoxes
                    if (strLine5.equals("Windows"))
                        chk.setSelected(true);
                    if (strLine5.equals("Mac"))
                        chk2.setSelected(true);
                    if (strLine5.equals("Linux"))
                        chk3.setSelected(true);

                    if (strLine6.equals("Windows"))
                        chk.setSelected(true);
                    if (strLine6.equals("Mac"))
                        chk2.setSelected(true);
                    if (strLine6.equals("Linux"))
                        chk3.setSelected(true);

                    if (strLine7.equals("Windows"))
                        chk.setSelected(true);
                    if (strLine7.equals("Mac"))
                        chk2.setSelected(true);
                    if (strLine7.equals("Linux"))
                        chk3.setSelected(true);


                    //Close the input stream
                    in .close();
                } catch (Exception e2) { //Catch exception if any
                    System.err.println("Error: " + e2.getMessage());
                }
            }

        }

        public static void main(String[] args) {
            new Window();
        }

    }

ShaneKaplan.com