Results 1 to 3 of 3
  1. #1
    raffs03 is offline Member
    Join Date
    Sep 2012
    Posts
    41
    Rep Power
    0

    Default ActionListener or JFrame Problem?

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.util.*;
    import javax.swing.table.*;
    import java.awt.event.*;
    
    
    public class Trypack {
        
        public class ViewRec extends JFrame{
             private String[][] data;
           public ViewRec(ArrayList<StudNum> records){
               data = new String[records.size()][5];
            for(int i=0;i<records.size();i++)
            {
                data[i][0] = ""+(i+1);
                data[i][1] = records.get(i).getStudentNo();
                data[i][2] = records.get(i).getName();
                data[i][3] = records.get(i).getDateOfBirth();
                data[i][4] = records.get(i).getCourse();
            }
            table = new JTable(data, new String[]{"#","Student No.","Name","Date of Birth","Course"})
                    {
    
                @Override
                public boolean isCellEditable(int row, int column) {
                    return false;
                } 
            };
            setSize(500,300);
            scrollPane = new JScrollPane(table);
            
           }   
        }
        
        public class StudNum{
            private String StudentNo;
            private String LastName;
            private String FirstName;
            private String MiddleName;
            private String DateOfBirth;
            private String Course;
        
            public void setStudentNo(String StudentNo)
        {
            this.StudentNo = StudentNo;
        }
        
        public void setLastName(String LastName)
        {
            this.LastName = LastName;
        }
        
        public void setFirstName(String FirstName)
        {
            this.FirstName = FirstName;
        }
        
        public void setMiddleName(String MiddleName)
        {
            this.MiddleName = MiddleName;
        }
        
        public void setName(String LastName, String FirstName, String MiddleName)
        {
            setLastName(LastName);
            setMiddleName(MiddleName);
            setFirstName(FirstName);
        }
        
        public void setDateOfBirth(String DateOfBirth)
        {
            this.DateOfBirth = DateOfBirth;
        }
        
        public void setCourse(String Course)
        {
            this.Course = Course;
        }
        
        public String getStudentNo()
        {
            return StudentNo;
        }
        
        public String getLastName()
        {
            return LastName;
        }
        
        public String getFirstName()
        {
            return FirstName;
        }
        
        public String getMiddleName()
        {
            return MiddleName;
        }
        
        public String getName()
        {
            return getLastName()+", "+getFirstName()+" "+getMiddleName();
        }
        
        public String getDateOfBirth()
        {
            return DateOfBirth;
        }
        
        public String getCourse()
        {
            return Course;
        }
        
        @Override
        public String toString()
        {
            String sRecord = 
                    "[Record]"
                    + "\nStudent No.:   " + getStudentNo()
                    + "\nFull Name:     " + getName()
                    + "\nDate of Birth: " + getDateOfBirth()
                    + "\nCourse:        " + getCourse();
            return sRecord;
        }
        }
        
        
        private ArrayList<StudNum> records = new ArrayList<>();
    
    public Trypack(){
            final JFrame Visual3 = new JFrame();
            final JFrame Visual5 = new JFrame();
            final  JPanel Pnl1 = new JPanel();
            final  JPanel Pnl3 = new JPanel();
    
             final JMenuItem Item1 = new JMenuItem("Logout");
             JMenuBar menuBar = new JMenuBar();
              JMenu menu1 = new JMenu("File");
              JMenu menu2 = new JMenu("Help");
              final JComboBox cmbx = new JComboBox(new String[]{"-select-","BSIT","BSCS","COMVUL","BSBS"});
              Btn5 = new JButton("Save"); // on visual3 Frame
              Btn6 = new JButton("Clear");// on Visual3 Frame
              Btn7 = new JButton("Save");// on Visual5 Frame
              Btn8 = new JButton("Clear");// on VIsual5 Frame
              Btn11 = new JButton("Add new Student");
              Btn12 = new JButton("Close");
    
            Pnl1.setAlignmentX(JPanel.TOP_ALIGNMENT);
            Pnl1.setLayout(new GridLayout());
            Pnl1.setVisible(true);
            
          
            Pnl3.setAlignmentX(JPanel.BOTTOM_ALIGNMENT);
            Pnl3.setLayout(new GridLayout(1,3));
            Pnl3.setVisible(true);
    
               Txt3 = new JTextField();
              Txt4 = new JTextField();
              Txt5 = new JTextField();
              Txt7 = new JTextField();
              Txt8 = new JTextField();
              Txt9 = new JTextField();
              Txt10 = new JTextField();
              Txt11 = new JTextField();
              Txt12 = new JTextField();
    
    
              Lbl3 = new JLabel("Student #: ");//on Visual3 Frame
              Lbl4 = new JLabel("Lastname: ");
              Lbl5 = new JLabel("FirstName: ");
              Lbl6 = new JLabel("");
              Lbl7 = new JLabel("Course: ");
              Lbl8 = new JLabel("Middle Name: ");
              Lbl9 = new JLabel("Math Grade: ");
              Lbl10 = new JLabel("Science Grade: ");
              Lbl11 = new JLabel("English Grade: ");
              Lbl12 = new JLabel("Makabayan Grade: ");
              Lbl13 = new JLabel("Date of Birth: ");
    
              Btn5.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                     if(cmbx.getSelectedIndex()==0 || Txt5.getText().isEmpty() || Txt4.getText().isEmpty() 
                         || Txt7.getText().isEmpty()  || Txt12.getText().isEmpty() || Txt3.getText().isEmpty())
                         {
                        JOptionPane.showMessageDialog(null, "Please complete all required fields before saving.", "Input Required", JOptionPane.ERROR_MESSAGE);
                         }
                     else{
                         StudNum sRecord = new StudNum();
                        sRecord.setCourse(cmbx.getSelectedItem().toString());
                        sRecord.setDateOfBirth(Txt12.getText());
                        sRecord.setName(Txt4.getText(), Txt5.getText(), Txt7.getText());
                        sRecord.setStudentNo(Txt3.getText());
                        records.add(sRecord);
                        JOptionPane.showMessageDialog(null, "["+sRecord.getStudentNo()+"] "+sRecord.getName()+" has been added to the database.", "Success!", JOptionPane.INFORMATION_MESSAGE);
                        Btn10.setEnabled(true);
                    
                        Visual3.show(false);
                        Visual5.show();
                    Lbl17 = new JLabel("Student #:        \t" +Txt3.getText());
                    Lbl18 = new JLabel("Name:        \t"+Txt4.getText()+ " " + Txt5.getText()+ ", " + Txt7.getText());
                    Lbl24 = new JLabel("Course:        \t"+cmbx.getSelectedItem().toString());
                   Pnl4.add(Lbl18);// Name
                   Pnl4.add(Lbl24);// Course
                   Pnl4.add(Lbl17);// Student #
                     }
                }
            });
    
    Btn7.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                     Visual5.show(false);
                     Visual4.show();
                     Object citem = cmbx.getSelectedItem();
                     Lbl25 = new JLabel("Student #:       \t"+Txt3.getText());//on Visual4 Frame
                     Lbl14 = new JLabel("Name:        \t"+Txt4.getText()+","+Txt5.getText()+" "+Txt7.getText());// on Visual 4 Frame
                     Lbl26 = new JLabel("Course:        \t"+citem);//on Visual4 Frame
                     Lbl19 = new JLabel("Math:        \t"+Txt8.getText());//on Visual4 Frame
                     Lbl20 = new JLabel("Science:        \t"+Txt9.getText());//on Visual4 Frame
                     Lbl21 = new JLabel("English:         \t"+Txt10.getText());//on Visual4 Frame
                     Lbl22 = new JLabel("Makabayan:      \t"+Txt11.getText());//on Visual4 Frame
                     Visual4.add(Lbl25);
                     Visual4.add(Lbl14);
                     Visual4.add(Lbl26);
                     Visual4.add(Lbl19);
                     Visual4.add(Lbl20);
                     Visual4.add(Lbl21);
                     Visual4.add(Lbl22);
                     Visual4.add(Btn12);
                     
                }
            });
    
    Btn12.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                    Visual4.show(false);
                   ViewRec dialog = new ViewRec(records);
                    dialog.setSize(500,400);
                    dialog.setTitle("Manage Your Students");
                    dialog.setLocationRelativeTo(null);
                    dialog.setVisible(true);
                    dialog.getContentPane().add(Pnl1);
                    dialog.getContentPane().add(Pnl3);
                    dialog.setLayout(new GridLayout(2,1));
                    Pnl1.add(scrollPane);
                    Pnl3.add(Btn10);
                    Pnl3.add(Btn11);
                    Pnl3.add(Btn9);
                  
                }
            });
    Everything is fine on the first Attempt and this is the Output:


    But when i Add a new Student something will get wrong and this is the output(and my problem that i want to solve):


    I want my table to look like this:


    I guess the Problem is this code:
    Java Code:
    Btn12.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                    Visual4.show(false);
                   ViewRec dialog = new ViewRec(records);
                    dialog.setSize(500,400);
                    dialog.setTitle("Manage Your Students");
                    dialog.setLocationRelativeTo(null);
                    dialog.setVisible(true);
                    dialog.getContentPane().add(Pnl1);
                    dialog.getContentPane().add(Pnl3);
                    dialog.setLayout(new GridLayout(2,1));
                    Pnl1.add(scrollPane);
                    Pnl3.add(Btn10);
                    Pnl3.add(Btn11);
                    Pnl3.add(Btn9);
                  
                }
            });
    Any Suggestions or Code sample will greatly Appreciated. I'm Struggling at this problem probably 5hrs just to solve but no luck at all

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: ActionListener or JFrame Problem?

    Please change the names of your variables as your current variable names make it hard for us to understand your code and hard for you to debug. For instance how will anyone know at a glance what btn11, btn12, or btn3021323 is? Why not change these to addNewStudentBtn and viewGradesBtn. For instance it appears to me that your button of interest is btn11 not btn12, and so you appear to be showing us the *wrong* ActionListener.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

    Default Re: ActionListener or JFrame Problem?

    Additionally, learn to respect and follow coding conventions; variable names should begin with a lowercase letter.
    Code Conventions for the Java Programming Language: Contents

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Have problem with add.ActionListener()
    By emily.m in forum AWT / Swing
    Replies: 5
    Last Post: 02-22-2012, 09:36 PM
  2. Problem with ActionListener?
    By Manish87 in forum AWT / Swing
    Replies: 5
    Last Post: 03-31-2011, 02:54 PM
  3. Problem implementing ActionListener??
    By ryanonnfire54 in forum New To Java
    Replies: 3
    Last Post: 11-16-2010, 04:39 PM
  4. Problem Actionlistener
    By Questionmark in forum New To Java
    Replies: 18
    Last Post: 08-07-2010, 12:29 PM
  5. ActionListener Applet problem
    By xander5511 in forum Java Applets
    Replies: 1
    Last Post: 02-21-2009, 03:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •