Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: How this GUI functions

  1. #1
    Junior Member mathobject's Avatar
    Join Date
    Oct 2012
    Posts
    2
    My Mood
    Dead
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How this GUI functions

    Can anyone who is patient enough help me to understand this program much better.


    /**
     * @(#)STIregformmodule.java
     *
     * STIregformmodule application
     *
     * @author 
     * @version 1.8 2012/10/6
     */
     
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.text.*;
     
    public class STIregformmodule extends JFrame {
       // Declare variables:
       // array lists
       String[] columnNames = {"GENDER", "NAME", "COURSE", "YEAR", "ID #"};
       Object[][] data = new Object[25][5];
     
       // table
       JTable table = new JTable(data, columnNames) {
          // sets the ability of the cells to be edited by the user
          public boolean isCellEditable(int row, int column) {
             return false; // returns false, cannot be edited
          }
       };
       // frames
       JFrame frame, frame1;
       // panels
       JPanel buttonPanel, buttonPanel2, tablePanel, addPanel, editPanel;
       // labels
       JLabel labelGender, labelName, labelCourse, labelYear, labelID;
       // text fields
       JTextField txtGender, txtName, txtCourse, txtYear, txtID;
       // buttons
       JButton btnAdd, btnEdit, btnDelete, btnAddInput, btnCancel;
       // additionals
       int keyCode, rowIndex, rowNumber, noOfStudents;
       // button handler
       ButtonHandler bh = new ButtonHandler();
     
       public STIregformmodule() {
          // setting/modifying table components
          table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
          table.getSelectionModel().addListSelectionListener(new RowListener());
          table.getColumnModel().getColumn(1).setPreferredWidth(200);
          table.getColumnModel().getColumn(3).setPreferredWidth(50);
          table.getTableHeader().setResizingAllowed(false);
          table.getTableHeader().setReorderingAllowed(false);
          JScrollPane scrollPane = new JScrollPane(table);
     
          // main buttons
          btnAdd = new JButton("ADD");
          btnAdd.addActionListener(bh);
          btnEdit = new JButton("EDIT");
          btnEdit.addActionListener(bh);
          btnEdit.setEnabled(false); // disables the component
          btnDelete = new JButton("DELETE");
          btnDelete.addActionListener(bh);
          btnDelete.setEnabled(false); // disables the component
     
          // with button Listeners
     
          // sub buttons
          btnAddInput = new JButton("Add");
          btnAddInput.addActionListener(bh);
          btnCancel = new JButton("Cancel");
          btnCancel.addActionListener(bh);
     
          // set label names
          labelGender = new JLabel(" Gender:");
          labelName = new JLabel("    Name:");
          labelCourse = new JLabel(" Course:");
          labelYear = new JLabel("      Year:");
          labelID = new JLabel("           ID:");
     
          // set text fields width
          txtGender = new JTextField(20);
          txtName = new JTextField(20);
          txtCourse = new JTextField(20);
          txtYear = new JTextField(20);
          txtYear.setDocument(new JTextFieldLimit(1)); // limits the length of
                                                       // input: max of 1
          txtYear.addKeyListener(keyListener); // accepts only numerals
          txtID = new JTextField(20);
          txtID.setDocument(new JTextFieldLimit(3)); // limits the length of input:
                                                     // max of 3
          txtID.addKeyListener(keyListener); // accepts only numerals
     
          // main frame
          // panel for the table
          tablePanel = new JPanel();
          tablePanel.setLayout(new BoxLayout(tablePanel, BoxLayout.PAGE_AXIS));
          tablePanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 0, 10));
          tablePanel.add(table.getTableHeader());
          tablePanel.add(table);
     
          // panel for the main buttons
          buttonPanel = new JPanel();
          buttonPanel.setLayout(new GridBagLayout());
          GridBagConstraints c = new GridBagConstraints();
     
          // positions the main buttons
          c.gridx = 0;
          c.gridy = 0;
          c.ipady = 20;
          c.insets = new Insets(10, 10, 10, 10);
          c.fill = GridBagConstraints.HORIZONTAL;
          buttonPanel.add(btnAdd, c);
          c.gridx = 0;
          c.gridy = 1;
          c.fill = GridBagConstraints.HORIZONTAL;
          c.ipady = 20;
          c.insets = new Insets(10, 10, 10, 10);
          buttonPanel.add(btnEdit, c);
          c.gridx = 0;
          c.gridy = 2;
          c.fill = GridBagConstraints.HORIZONTAL;
          c.ipady = 20;
          c.insets = new Insets(10, 10, 10, 10);
          buttonPanel.add(btnDelete, c);
          c.gridx = 0;
          c.gridy = 3;
          c.ipady = 20;
          c.insets = new Insets(10, 10, 10, 10);
          c.fill = GridBagConstraints.HORIZONTAL;
     
          frame = new JFrame("Student Database");
          frame.setVisible(true);
          frame.setResizable(false);
          frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
          frame.add(tablePanel, BorderLayout.CENTER);
          frame.add(buttonPanel, BorderLayout.EAST);
          frame.pack();
     
          // ADD frame
          // panel for adding
          addPanel = new JPanel();
          addPanel.setLayout(new GridBagLayout());
     
          // positions the components for adding
          // labels
          c.insets = new Insets(1, 0, 1, 1);
          c.gridx = 0;
          c.gridy = 0;
          addPanel.add(labelID, c);
          c.gridy = 1;
          addPanel.add(labelName, c);
          c.gridy = 2;
          addPanel.add(labelCourse, c);
          c.gridy = 3;
          addPanel.add(labelYear, c);
          c.gridy = 4;
          addPanel.add(labelGender, c);
          // text fields
          c.gridx = 1;
          c.gridy = 0;
          c.ipady = 1;
          addPanel.add(txtID, c);
          c.gridy = 1;
          c.ipady = 1;
          addPanel.add(txtName, c);
          c.gridy = 2;
          c.ipady = 1;
          addPanel.add(txtCourse, c);
          c.gridy = 3;
          c.ipady = 1;
          addPanel.add(txtYear, c);
          c.gridy = 4;
          c.ipady = 1;
          addPanel.add(txtGender, c);
     
          // panel for other necessary buttons
          buttonPanel2 = new JPanel();
          buttonPanel2.setLayout(new GridLayout(1, 1));
          buttonPanel2.add(btnAddInput);
          buttonPanel2.add(btnCancel);
     
          frame1 = new JFrame("Student Database");
          frame1.setVisible(false);
          frame1.setResizable(false);
          frame1.setDefaultCloseOperation(HIDE_ON_CLOSE);
          frame1.add(addPanel, BorderLayout.CENTER);
          frame1.add(buttonPanel2, BorderLayout.PAGE_END);
          frame1.pack();
       }// end
     
       KeyListener keyListener = new KeyListener() {
          public void keyTyped(KeyEvent e) {
          }
     
          public void keyPressed(KeyEvent e) {
             keyCode = e.getKeyCode();
     
             if (!(keyCode >= 48 && keyCode <= 57) && !(keyCode >= 96 && keyCode <= 105)
                      && !(keyCode >= 37 && keyCode <= 40) && !(keyCode == 127 || keyCode == 8)) {
                txtID.setEditable(false);
             }
          }
     
          public void keyReleased(KeyEvent e) {
             txtID.setEditable(true);
          }
       };
     
       class RowListener implements ListSelectionListener {
          public void valueChanged(ListSelectionEvent event) {
             if (event.getValueIsAdjusting()) {
                rowIndex = table.getSelectedRow();
                if (data[rowIndex][0] == null || data[rowIndex][0] == "") {
                   btnEdit.setEnabled(false);
                   btnDelete.setEnabled(false);
                } else {
                   btnEdit.setEnabled(true);
                   btnDelete.setEnabled(true);
                }
             }
          }
       }// end
     
       class ButtonHandler implements ActionListener {
          public void actionPerformed(ActionEvent e) {
             try {
                if (e.getActionCommand().equals("ADD")) {
                   // text fields for Student input data
                   txtGender.setText("");
                   txtName.setText("");
                   txtCourse.setText("");
                   txtYear.setText("");
                   txtID.setText("");
     
                   frame1.setTitle("Add Student data"); // title bar name for add
                   btnAddInput.setActionCommand("Add2");
                   btnAddInput.setText("ADD");
     
                   frame1.setVisible(true); // sets the visibility of frame1
                } else if (e.getActionCommand().equals("EDIT")) {
                   txtID.setText(data[rowIndex][4] + ""); // will preview the ID
                                                          // input during Add
                   txtName.setText(data[rowIndex][1] + ""); // will preview the Name
                                                            // input during Add
                   txtCourse.setText(data[rowIndex][2] + ""); // will preview the
                                                              // Course input during
                                                              // Add
                   txtYear.setText(data[rowIndex][3] + ""); // will preview the Year
                                                            // input during Add
                   txtGender.setText(data[rowIndex][0] + ""); // will preview the
                                                              // Gender input during
                                                              // Add
     
                   txtID.setEditable(false); // forbids the user to edit the entered
                                             // ID number
     
                   frame1.setTitle("Edit Student data"); // title bar name for edit
                   btnAddInput.setActionCommand("Edit2");
                   btnAddInput.setText("ACCEPT");
     
                   frame1.setVisible(true); // sets the visibility of frame1
                } else if (e.getActionCommand().equals("DELETE")) {
                   int confirm = JOptionPane.showConfirmDialog(frame, "ARE YOU SURE?", "CONFIRM",
                            JOptionPane.YES_NO_OPTION);
     
                   if (confirm == 0) {
                      rowIndex = table.getSelectedRow();
                      rowNumber = 0;
     
                      noOfStudents--;
                      for (int i = 0; i <= 10; i++) {
                         if (rowIndex != i && i <= noOfStudents) {
                            data[rowNumber][4] = data[i][4];
                            data[rowNumber][1] = data[i][1];
                            data[rowNumber][2] = data[i][2];
                            data[rowNumber][3] = data[i][3];
                            data[rowNumber][0] = data[i][0];
                            rowNumber++;
                         } else if (rowIndex != i && i > noOfStudents) {
                            data[rowNumber][4] = "";
                            data[rowNumber][1] = "";
                            data[rowNumber][2] = "";
                            data[rowNumber][3] = "";
                            data[rowNumber][0] = "";
                            rowNumber++;
                         }
                      }
                      if (noOfStudents == 50) // if the number of students id equal
                                              // to 50, can no longer add students
                         btnAdd.setEnabled(false);
                      else
                         btnAdd.setEnabled(true); // continues to add students if
                                                  // not equal to 50
                      if (noOfStudents == 0) {
                         btnDelete.setEnabled(false);
                         btnEdit.setEnabled(false);
                      } else {
                         btnDelete.setEnabled(true);
                         btnEdit.setEnabled(true);
                      }
     
                      rowIndex = table.getSelectedRow();
                      if (data[rowIndex][0] == null || data[rowIndex][0] == "") {
                         btnEdit.setEnabled(false);
                         btnDelete.setEnabled(false);
                      } else {
                         btnEdit.setEnabled(true);
                         btnDelete.setEnabled(true);
                      }
     
                      table.updateUI();
     
                   }
                } else if (e.getActionCommand().equals("Add2")) {
     
                   if (txtID.getText().isEmpty() || txtName.getText().isEmpty()
                            || txtCourse.getText().isEmpty()// /
                            || txtYear.getText().isEmpty() || txtGender.getText().isEmpty()) {
                      JOptionPane.showMessageDialog(null, "PLEASE FILL IN THE BLANKS.", "ERROR!",
                               JOptionPane.ERROR_MESSAGE);
                   } else {
                      int dup = 0;
                      for (int i = 0; i < 10; i++) {
                         if (txtID.getText().equals(data[i][0])) {
                            JOptionPane.showMessageDialog(null, "ID NUMBER ALREADY EXISTS.", "ERROR!",
                                     JOptionPane.ERROR_MESSAGE);
                            dup++;
                            break;
                         }
                      }
                      if (dup == 0) {
                         rowIndex = table.getSelectedRow();
                         data[noOfStudents][4] = txtID.getText();
                         data[noOfStudents][1] = txtName.getText();
                         data[noOfStudents][2] = txtCourse.getText();
                         data[noOfStudents][3] = txtYear.getText();
                         data[noOfStudents][0] = txtGender.getText();
     
                         table.updateUI();
                         frame1.dispose();
                         noOfStudents++;
                         if (noOfStudents == 50)
                            btnAdd.setEnabled(false);
                         else
                            btnAdd.setEnabled(true);
                         if (data[rowIndex][0] == null) {
                            btnEdit.setEnabled(false);
                            btnDelete.setEnabled(false);
                         } else {
                            btnEdit.setEnabled(true);
                            btnDelete.setEnabled(true);
                         }
                      }
                   }
                   table.updateUI();
                } else if (e.getActionCommand().equals("Edit2")) {
                   if (txtID.getText().isEmpty() || txtName.getText().isEmpty()
                            || txtCourse.getText().isEmpty() || txtYear.getText().isEmpty()
                            || txtGender.getText().isEmpty()) {
                      JOptionPane.showMessageDialog(null, "INCOMPLETE INPUT.", "ERROR!",
                               JOptionPane.ERROR_MESSAGE);
                   } else {
                      data[rowIndex][4] = txtGender.getText();
                      data[rowIndex][1] = txtName.getText();
                      data[rowIndex][2] = txtCourse.getText();
                      data[rowIndex][3] = txtYear.getText();
                      data[rowIndex][0] = txtID.getText();
                      frame1.dispose();
                   }
     
                   table.updateUI();
     
                } else if (e.getActionCommand().equals("Cancel")) {
                   frame1.dispose();
                }
     
             } catch (Exception error) {
             }
     
          }
       }// end
     
       class JTextFieldLimit extends PlainDocument {
          private int limit;
     
          JTextFieldLimit(int limit) {
             super();
             this.limit = limit;
          }
     
          JTextFieldLimit(int limit, boolean upper) {
             super();
             this.limit = limit;
          }
     
          public void insertString(int offset, String str, AttributeSet attr)
                   throws BadLocationException {
             if (str == null)
                return;
     
             if ((getLength() + str.length()) <= limit) {
                super.insertString(offset, str, attr);
             }
          }
       }// end
     
       public static void main(String[] args) {
          new STIregformmodule();
       }// end of main
     
    }// end of class


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,166
    My Mood
    Hungover
    Thanks
    141
    Thanked 597 Times in 512 Posts

    Default Re: How this GUI functions

    What don't you understand about it? Have you stepped through it with a debugger or at least added some print statements to help figure out what's going on?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member mathobject's Avatar
    Join Date
    Oct 2012
    Posts
    2
    My Mood
    Dead
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How this GUI functions

    Yeah I've read through it. Everything in it works fine. Basically the problem is I'm just a beginner I don't understand how the methods in it work . I'm trying to create a simpler GUI just like it. A simple add, edit and delete GUI that prints my saved data in an array.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,166
    My Mood
    Hungover
    Thanks
    141
    Thanked 597 Times in 512 Posts

    Default Re: How this GUI functions

    What don't you understand about it?

    Let me guess: this code was created by a gui builder, right? Most people would advise against using a gui builder until you understand what it's doing behind the scenes.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Need help with basic math functions
    By ImOwen in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 9th, 2012, 09:24 AM
  2. Hash Functions
    By Blueshark in forum Java Theory & Questions
    Replies: 3
    Last Post: July 2nd, 2012, 03:04 PM
  3. need help with using trig functions
    By Tdogg in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 1st, 2012, 06:47 AM
  4. Functions, real numbers and for loops
    By grimhildr in forum Loops & Control Statements
    Replies: 1
    Last Post: October 15th, 2011, 07:11 AM
  5. Directing JButtons to functions?
    By scopolamine in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 10th, 2011, 11:22 AM