Results 1 to 15 of 15
Like Tree1Likes
  • 1 Post By jim829

Thread: JTable Problem here!!

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

    Default JTable Problem here!!

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.util.*;
    import javax.swing.table.*;
    import java.awt.event.*;
    
    
    
    public class Trypack {
        
        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 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;
                } 
            };
            setLayout(new GridLayout(2,1));
            setTitle("Manage Your Students");
            setSize(500,400);
            setLocationRelativeTo(null);
            scrollPane = new JScrollPane(table);
            
           }   
        }
        
        public Trypack(){
           
            final JFrame frame1 = new JFrame();
            final JFrame frame2 = new JFrame();
            final JFrame frame3 = new JFrame();
            final JFrame frame4 = new JFrame();
            final JFrame frame5 = new JFrame();
            final JFrame frame6 = new JFrame();
            final  JPanel Pnl1 = new JPanel();
            final  JPanel Pnl3 = new JPanel();
            final  JPanel Pnl4 = new JPanel();
            final  JPanel Pnl5 = new JPanel();
            final  JPanel Pnl6 = 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"});
              
              
            
             
              
              btnLogin = new JButton("Login");
              btnClear = new JButton("Clear");
              btnViewStud = new JButton("View Students");
              btnAddStud = new JButton("Add Students");
              btnSave = new JButton("Save"); 
              btnClear1 = new JButton("Clear");
              btnSave1 = new JButton("Save");
              btnClear2 = new JButton("Clear");
              btnBack = new JButton("Back");
              btnViewGrad = new JButton("View Grades");
              btnAddNewStud = new JButton("Add new Student");
              btnClose = new JButton("Close");
              
              lblUser = new JLabel("Username: ");
              lblPass = new JLabel("Password: ");
              lblStudNum = new JLabel("Student #: ");
              lblLastName = new JLabel("Lastname: ");
              lblFirstName = new JLabel("FirstName: ");
              lblCourse = new JLabel("Course: ");
              lblMidName = new JLabel("Middle Name: ");
              lblMath = new JLabel("Math Grade: ");
              lblScience = new JLabel("Science Grade: ");
              lblEnglish = new JLabel("English Grade: ");
              lblMakabayan = new JLabel("Makabayan Grade: ");
              lblBirth = new JLabel("Date of Birth: ");
              
              txtLogin = new JTextField();        
              pass2 = new JPasswordField(20);
              txtStudNum = new JTextField();
              txtLast = new JTextField();
              txtFirst = new JTextField();
              txtMid = new JTextField();
              txtMath = new JTextField();
              txtScience = new JTextField();
              txtEnglish = new JTextField();
              txtMakabayan = new JTextField();
              txtBirth = new JTextField();
              
             
              
              menuBar.add(menu1);
              menuBar.add(menu2);
              menu1.add(Item1);
              
              
              
            frame1.add(lblUser);
            frame1.add(txtLogin);
            frame1.add(lblPass);
            frame1.add(pass2);
            frame1.add(btnLogin);
            frame1.add(btnClear);
            
            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);
            
           
            Pnl4.setAlignmentX(JPanel.TOP_ALIGNMENT);
            Pnl4.setLayout(new GridLayout(3,1));
            Pnl4.setVisible(true);
            
            Pnl5.setAlignmentX(JPanel.CENTER_ALIGNMENT);
            Pnl5.setLayout(new GridLayout(4,1));
            Pnl5.setVisible(true);
            
            Pnl6.setAlignmentX(JPanel.BOTTOM_ALIGNMENT);
            Pnl6.setLayout(new GridLayout(1,2));
            Pnl6.setVisible(true);
            
            frame2.setVisible(false);
            frame2.setSize(350,150);
            frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame2.setLayout(new GridLayout(3,1));
            frame2.setLocationRelativeTo(null);
            
            
            ViewRec dialog = new ViewRec(records);
                    dialog.setVisible(true);
                    dialog.getContentPane().add(Pnl1);
                    dialog.getContentPane().add(Pnl3);
                    Pnl1.add(scrollPane);
                    Pnl3.add(btnViewGrad);
                    Pnl3.add(btnAddNewStud);
                    Pnl3.add(btnBack);
            
            frame3.setVisible(false);
            frame3.setSize(500,544);
            frame3.setTitle("Manage Your Students");
            frame3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame3.setLayout(new GridLayout(3,3));
            frame3.setLocationRelativeTo(null);
            
            
            frame4.setVisible(false);
            frame4.setSize(350,300);
            frame4.setTitle("Adding Students");
            frame4.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame4.setLayout(new GridLayout(7,2));
            frame4.setLocationRelativeTo(null);     
            
            frame5.setVisible(false);
            frame5.setSize(320,320);
            frame5.setTitle("Viewing Grades");
            frame5.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame5.setLayout(new GridLayout(8,1));
            frame5.setLocationRelativeTo(null);
            
            frame6.setVisible(false);
            frame6.setSize(300,400);
            frame6.setTitle("Adding Grades");
            frame6.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame6.setLayout(new GridLayout(3,2));
            frame6.setLocationRelativeTo(null);
            frame6.getContentPane().add(Pnl4);
            frame6.getContentPane().add(Pnl5);
            frame6.getContentPane().add(Pnl6);
            
            frame1.setVisible(true);
            frame1.setSize(350,150);
            frame1.setLayout(new GridLayout(3,2));
            frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame1.setLocationRelativeTo(null);
            frame1.setTitle("Login Your Account");
              
                    
                    
            
            frame2.add(menuBar);
            frame2.add(btnViewStud);
            frame2.add(btnAddStud);
                    
            
            
            
            frame4.add(lblStudNum);
            frame4.add(txtStudNum);
            frame4.add(lblLastName);
            frame4.add(txtLast);
            frame4.add(lblFirstName);
            frame4.add(txtFirst);
            frame4.add(lblMidName);
            frame4.add(txtMid);
            frame4.add(lblBirth);
            frame4.add(txtBirth);
            frame4.add(lblCourse);
            frame4.add(cmbx);
            frame4.add(btnSave);
            frame4.add(btnClear1);
            
            
            Pnl5.add(lblMath);
            Pnl5.add(txtMath);
            Pnl5.add(lblScience);
            Pnl5.add(txtScience);
            Pnl5.add(lblEnglish);
            Pnl5.add(txtEnglish);
            Pnl5.add(lblMakabayan);
            Pnl5.add(txtMakabayan);
            Pnl6.add(btnSave1);
            Pnl6.add(btnClear2);
            
            
           Item1.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent act1) {
                    int lo = JOptionPane.showConfirmDialog(null, "Are You Sure You Want To Logout?", "Logout", JOptionPane.YES_NO_OPTION);
                    if(lo == JOptionPane.YES_OPTION)
                    {
                        txtLogin.setText(null);
                        pass2.setText(null);
                        frame1.show();
                    }
                }
            });
            
            btnLogin.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                        frame2.show();
                        frame2.setTitle("Login as: "+ txtLogin.getText());
                        frame1.show(false);
                    
                }
            });
            
            btnClear.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                    txtLogin.setText(null);
                    pass2.setText(null);
                }
            });
          btnViewStud.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent act2) {
                        frame2.show(false);
                        btnViewGrad.setEnabled(false);
                        
                       
                }
            });
            
            btnAddStud.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent act3) {
                    cmbx.setSelectedIndex(0);
                        frame4.show();
                        frame2.show(false);
                    
                }
            });
            
            btnSave.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                     if(cmbx.getSelectedIndex()==0 || txtFirst.getText().isEmpty() || txtLast.getText().isEmpty() 
                         || txtMid.getText().isEmpty()  || txtBirth.getText().isEmpty() || txtStudNum.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(txtBirth.getText());
                        sRecord.setName(txtLast.getText(), txtFirst.getText(), txtMid.getText());
                        sRecord.setStudentNo(txtStudNum.getText());
                        records.add(sRecord);
                        JOptionPane.showMessageDialog(null, "["+sRecord.getStudentNo()+"] "+sRecord.getName()+" has been added to the database.", "Success!", JOptionPane.INFORMATION_MESSAGE);
                        btnViewGrad.setEnabled(true);
                    
                        frame4.show(false);
                        frame6.show();
                    lblStudNum1 = new JLabel("Student #:        \t" +txtStudNum.getText());
                    lblName = new JLabel("Name:        \t"+txtLast.getText()+ " " + txtFirst.getText()+ ", " + txtMid.getText());
                    lblCourse1 = new JLabel("Course:        \t"+cmbx.getSelectedItem().toString());
                   Pnl4.add(lblName);
                   Pnl4.add(lblCourse1);
                   Pnl4.add(lblStudNum1);
                     }
                }
            });
            
            btnClear1.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                    clearFields();
                }
            });
            
            btnSave1.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                     frame6.show(false);
                     frame5.show();
                     Object citem = cmbx.getSelectedItem();
                     lblStudNum2 = new JLabel("Student #:       \t"+txtStudNum.getText());
                     lblName1 = new JLabel("Name:        \t"+txtLast.getText()+","+txtFirst.getText()+" "+txtMid.getText());
                     lblCourse2 = new JLabel("Course:        \t"+citem);
                     lblMath2 = new JLabel("Math:        \t"+txtMath.getText());
                     lblScience2 = new JLabel("Science:        \t"+txtScience.getText());
                     lblEnglish2 = new JLabel("English:         \t"+txtEnglish.getText());
                     lblMakabayan2 = new JLabel("Makabayan:      \t"+txtMakabayan.getText());
                     frame5.add(lblStudNum2);
                     frame5.add(lblName1);
                     frame5.add(lblCourse2);
                     frame5.add(lblMath2);
                     frame5.add(lblScience2);
                     frame5.add(lblEnglish2);
                     frame5.add(lblMakabayan2);
                     frame5.add(btnClose);
                     
                }
            });
            
            
            
            btnClear2.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                    txtMath.setText(null);
                    txtScience.setText(null);
                    txtEnglish.setText(null);
                    txtMakabayan.setText(null);
                }
            });
            
            btnBack.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                    frame3.show(false);
                    frame2.show();
                }
            });
            
            btnViewGrad.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                    frame5.show();
                }
            });
            
            btnAddNewStud.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                   cmbx.setSelectedIndex(0);
                   frame3.show(false);
                   frame4.show();
                   Btn11clearFields();
                }
            });
            
            btnClose.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                    frame5.show(false);
                   
                    
                    
                }
            }); 
           
        
            
        }
        
         private void clearFields()
        {
            txtStudNum.setText(null);
            txtLast.setText(null);
            txtFirst.setText(null);
            
            txtMid.setText(null);
            txtBirth.setText(null);
        }
         
         private void Btn11clearFields(){
            txtStudNum.setText(null);
            txtLast.setText(null);
            txtFirst.setText(null);
        
            txtMid.setText(null);
            txtMath.setText(null);
            txtScience.setText(null);
            txtEnglish.setText(null);
            txtMakabayan.setText(null);
         }
        
        
        public static void main(String[]args){
        new Trypack();
            }
        private JButton btnLogin,btnClear,btnViewStud,btnAddStud,btnSave,btnClear1,btnSave1,btnClear2,btnBack,btnViewGrad,btnAddNewStud,btnClose;
        private JLabel lblUser,lblPass,lblStudNum,lblLastName,lblFirstName,lblCourse,lblMidName,lblMath,lblScience,lblEnglish,lblMakabayan,lblBirth,lblName;
        private JLabel lblStudNum1,lblStudNum2,lblCourse1,lblCourse2,lblMath2,lblScience2,lblEnglish2,lblMakabayan2,lblName1;
        private JPasswordField pass2;
        private JTextField txtLogin,txtStudNum,txtLast,txtFirst,txtMid,txtMath,txtScience,txtEnglish,txtMakabayan,txtBirth;
        private JTable table;
        private JScrollPane scrollPane;
        
        }
    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 try to fix the error but it get worst. everytime i want to add new student the value that i input doesn't show!!
    Last edited by raffs03; 03-01-2013 at 06:45 AM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,423
    Rep Power
    5

    Default Re: JTable Problem here!!

    Your code seems to be incomplete. Couldn't find a type for the variable "table" and the supplied code does not terminate with the appropriate curly braces "}"

    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

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

    Default Re: JTable Problem here!!

    I added some info and fixed some missing curly braces. I hope it's ok now...

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,423
    Rep Power
    5

    Default Re: JTable Problem here!!

    There is something really strange here. You show output from your supplied source. Yet you don't declare all of your fields as appropriate types. This should not have compiled and therefore could not have produced the output provided unless you simply are omitting lots of code.

    Where were the following fields declared as either JTextField's or JLabel's?

    Jim


    Java Code:
    txtStudentNum = new JTextField();
          txtLastName = new JTextField();
          txtMiddleName = new JTextField();
          txtFirstName = new JTextField();
          txtMath = new JTextField();
          txtScience = new JTextField();
          txtEnglish = new JTextField();
          txtMakabayan = new JTextField();
          txtDateofBirth = new JTextField();
    
          lblStudentNum = new JLabel("Student #: ");
          lblStudentNum1 = new JLabel("Student #: ");
          lblSpc = new JLabel(" ");
          lblCourse = new JLabel("Course: ");
          lblCourse1 = new JLabel("Course: ");
          lblName = new JLabel("Name: ");
          lblName1 = new JLabel("Name: ");
          lblMath = new JLabel("Math Grade: ");
          lblScience = new JLabel("Science Grade: ");
          lblEnglish = new JLabel("English Grade: ");
          lblMakabayan = new JLabel("Makabayan Grade: ");
          lblDateofBirth = new JLabel("Date of Birth: ");
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    raffs03 is offline Member
    Join Date
    Sep 2012
    Posts
    41
    Rep Power
    0

    Default Re: JTable Problem here!!

    i put the declaration at the bottom of the main method.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,423
    Rep Power
    5

    Default Re: JTable Problem here!!

    There is still stuff missing. Why don't you simply take *all* of your code and paste it into the thread.

    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    raffs03 is offline Member
    Join Date
    Sep 2012
    Posts
    41
    Rep Power
    0

    Default Re: JTable Problem here!!

    i put all of my code in my post.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,423
    Rep Power
    5

    Default Re: JTable Problem here!!

    Ok. I got it to compile. However, apparently (as you said) you changed some things since the output no longer matches your thread. For example you don't add anything to the table. What I suggest you do first is to rename your variables/fields to something meaningful and to document your code. For example, you have six JFrames and five or six JPanels and they are called frame1,frame2.., Pnl1,Pnl2.. etc. These are not very descriptive so it is hard to figure out what you are trying to do. Document sections to indicate the overall intent of the section. If you do something special or unclear within a section, document that. Even if this does not lead to solving your problem it is very good practice.

    Also, you are using deprecated methods. Example is frame1.show(). They are deprecated for a reason. Check out the API documentation and see what else to you should do. In this case it is frame1.setVisible(true).

    Finally, I highly recommend looking at The Java™ Tutorials. Check out the Big Index for a list of what they talk about. I still use them from time to time. They do a good job of discussing the Swing platform and how to use a lot of what you are using.

    I or others will be here to assist.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

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

    Default Re: JTable Problem here!!

    Quote Originally Posted by jim829 View Post
    What I suggest you do first is to rename your variables/fields to something meaningful and to document your code. For example, you have six JFrames and five or six JPanels and they are called frame1,frame2.., Pnl1,Pnl2.. etc. These are not very descriptive so it is hard to figure out what you are trying to do. Document sections to indicate the overall intent of the section. If you do something special or unclear within a section, document that.
    Better still, create a a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates your problem. I haven't tried to read that code, but solving a JTable problem probably doesn't require a host of JFrames and JTextFields.

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

  10. #10
    raffs03 is offline Member
    Join Date
    Sep 2012
    Posts
    41
    Rep Power
    0

    Default Re: JTable Problem here!!

    i'm trying to solve my problem even now. And i discover something. Everytime i put this code on btnClose ActionListener
    Java Code:
    ViewRec dialog = new ViewRec(records);
                    dialog.setVisible(true);
                    Pnl1.add(scrollPane);
    the table is multiplying at the JFrame
    When i try to take out the code from the actionlistener
    the value doesn't add to the JTable

    I try to use JList and it works using a DefaultListModel to take the value of TextFields and it doesn't multiply, but our instructor wants a JTable.

  11. #11
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: JTable Problem here!!

    Use a DefaultTableModel
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  12. #12
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,423
    Rep Power
    5

    Default Re: JTable Problem here!!

    One more piece of advice. Some of the more advanced Swing functions like JTable, JTree, etc can be overwhelming. Especially when you get into writing your own data models, and rendering routines. I recommend that before you attempt to integrate any of these into a big project you write some small apps to try various features of the class. For JTable, just create one and try adding and removing entries to columns and rows, without bothering with user input from other panels. Use the DefaultTableModel as PhHein suggested. Once you are familiar with how they operate, then try more advanced stuff and then integrate it into your project.

    Regards,
    Jim
    Fubarable likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  13. #13
    raffs03 is offline Member
    Join Date
    Sep 2012
    Posts
    41
    Rep Power
    0

    Default Re: JTable Problem here!!

    Hey i manage to fix my problem regarding of multiplying of JTable but another error appear
    here my full code:
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.util.*;
    import javax.swing.table.*;
    import java.awt.event.*;
    
    public class Trypack {
    
        
        
        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<>();
             private String[][] data;
             private String col[] = {"#","Student no.","Name","Course"};
               
        public Trypack(){
            
           final DefaultTableModel Mdl1 = new DefaultTableModel();
           
           
                table = new JTable(Mdl1)
                    {
    
                @Override
                public boolean isCellEditable(int row, int column) {
                    return false;
                } 
            };
           Mdl1.addColumn("#");
           Mdl1.addColumn("Student no.");
           Mdl1.addColumn("Name");
           Mdl1.addColumn("Course");
            
            
            scrollPane = new JScrollPane(table);
            final JFrame frame1 = new JFrame();
            final JFrame frame2 = new JFrame();
            final JFrame frame3 = new JFrame();
            final JFrame frame4 = new JFrame();
            final JFrame frame5 = new JFrame();
            final JFrame frame6 = new JFrame();
            final  JPanel Pnl1 = new JPanel();
            final  JPanel Pnl2 = new JPanel();
            final  JPanel Pnl3 = new JPanel();
            final  JPanel Pnl4 = new JPanel();
            final  JPanel Pnl5 = new JPanel();
            final  JPanel Pnl6 = 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"});
              
              btnLogin = new JButton("Login");
              btnClear = new JButton("Clear");
              btnViewStud = new JButton("View Students");
              btnAddStud = new JButton("Add Students");
              btnSave = new JButton("Save"); 
              btnClear1 = new JButton("Clear");
              btnSave1 = new JButton("Save");
              btnClear2 = new JButton("Clear");
              btnBack = new JButton("Back");
              btnViewGrad = new JButton("View Grades");
              btnAddNewStud = new JButton("Add new Student");
              btnClose = new JButton("Close");
              
              lblUser = new JLabel("Username: ");
              lblPass = new JLabel("Password: ");
              lblStudNum = new JLabel("Student #: ");
              lblLastName = new JLabel("Lastname: ");
              lblFirstName = new JLabel("FirstName: ");
              lblCourse = new JLabel("Course: ");
              lblMidName = new JLabel("Middle Name: ");
              lblMath = new JLabel("Math Grade: ");
              lblScience = new JLabel("Science Grade: ");
              lblEnglish = new JLabel("English Grade: ");
              lblMakabayan = new JLabel("Makabayan Grade: ");
              lblBirth = new JLabel("Date of Birth: ");
              
              txtLogin = new JTextField();        
              pass2 = new JPasswordField(20);
              txtStudNum = new JTextField();
              txtLast = new JTextField();
              txtFirst = new JTextField();
              txtMid = new JTextField();
              txtMath = new JTextField();
              txtScience = new JTextField();
              txtEnglish = new JTextField();
              txtMakabayan = new JTextField();
              txtBirth = new JTextField();
              
              menuBar.add(menu1);
              menuBar.add(menu2);
              menu1.add(Item1);
              
              
            frame1.add(lblUser);
            frame1.add(txtLogin);
            frame1.add(lblPass);
            frame1.add(pass2);
            frame1.add(btnLogin);
            frame1.add(btnClear);
            
            Pnl1.setAlignmentX(JPanel.TOP_ALIGNMENT);
            Pnl1.setLayout(new BorderLayout());
            Pnl1.setVisible(true);
            
            Pnl2.setAlignmentX(JPanel.CENTER_ALIGNMENT);
            Pnl2.setLayout(new GridLayout());
            Pnl2.setVisible(true);
          
            Pnl3.setAlignmentX(JPanel.BOTTOM_ALIGNMENT);
            Pnl3.setLayout(new GridLayout(1,3));
            Pnl3.setVisible(true);
            
           
            Pnl4.setAlignmentX(JPanel.TOP_ALIGNMENT);
            Pnl4.setLayout(new GridLayout(3,1));
            Pnl4.setVisible(true);
            
            Pnl5.setAlignmentX(JPanel.CENTER_ALIGNMENT);
            Pnl5.setLayout(new GridLayout(4,1));
            Pnl5.setVisible(true);
            
            Pnl6.setAlignmentX(JPanel.BOTTOM_ALIGNMENT);
            Pnl6.setLayout(new GridLayout(1,2));
            Pnl6.setVisible(true);
            
            frame2.setVisible(false);
            frame2.setSize(350,150);
            frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame2.setLayout(new GridLayout(3,1));
            frame2.setLocationRelativeTo(null);
                      
                    Pnl1.add(scrollPane,BorderLayout.CENTER);
                    Pnl2.add(table);
                    Pnl3.add(btnViewGrad);
                    Pnl3.add(btnAddNewStud);
                    Pnl3.add(btnBack);
            
                    
            frame3.setVisible(false);
            frame3.setSize(500,544);
            frame3.setTitle("Manage Your Students");
            frame3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame3.setLayout(new GridLayout(2,1));
            frame3.setLocationRelativeTo(null);
            frame3.getContentPane().add(new JScrollPane(table));
            frame3.getContentPane().add(Pnl3);
            
            frame4.setVisible(false);
            frame4.setSize(350,300);
            frame4.setTitle("Adding Students");
            frame4.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame4.setLayout(new GridLayout(7,2));
            frame4.setLocationRelativeTo(null);     
            
            frame5.setVisible(false);
            frame5.setSize(320,320);
            frame5.setTitle("Viewing Grades");
            frame5.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame5.setLayout(new GridLayout(8,1));
            frame5.setLocationRelativeTo(null);
            
            frame6.setVisible(false);
            frame6.setSize(300,400);
            frame6.setTitle("Adding Grades");
            frame6.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame6.setLayout(new GridLayout(3,2));
            frame6.setLocationRelativeTo(null);
            frame6.getContentPane().add(Pnl4);
            frame6.getContentPane().add(Pnl5);
            frame6.getContentPane().add(Pnl6);
            
            frame1.setVisible(true);
            frame1.setSize(350,150);
            frame1.setLayout(new GridLayout(3,2));
            frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame1.setLocationRelativeTo(null);
            frame1.setTitle("Login Your Account");
              
                    
            frame2.add(menuBar);
            frame2.add(btnViewStud);
            frame2.add(btnAddStud);
            
            frame4.add(lblStudNum);
            frame4.add(txtStudNum);
            frame4.add(lblLastName);
            frame4.add(txtLast);
            frame4.add(lblFirstName);
            frame4.add(txtFirst);
            frame4.add(lblMidName);
            frame4.add(txtMid);
            frame4.add(lblBirth);
            frame4.add(txtBirth);
            frame4.add(lblCourse);
            frame4.add(cmbx);
            frame4.add(btnSave);
            frame4.add(btnClear1);
            
            
            Pnl5.add(lblMath);
            Pnl5.add(txtMath);
            Pnl5.add(lblScience);
            Pnl5.add(txtScience);
            Pnl5.add(lblEnglish);
            Pnl5.add(txtEnglish);
            Pnl5.add(lblMakabayan);
            Pnl5.add(txtMakabayan);
            Pnl6.add(btnSave1);
            Pnl6.add(btnClear2);
            
            
           Item1.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent act1) {
                    int lo = JOptionPane.showConfirmDialog(null, "Are You Sure You Want To Logout?", "Logout", JOptionPane.YES_NO_OPTION);
                    if(lo == JOptionPane.YES_OPTION)
                    {
                        txtLogin.setText(null);
                        pass2.setText(null);
                        frame1.setVisible(true);
                    }
                }
            });
            
            btnLogin.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                        frame2.setVisible(true);
                        frame2.setTitle("Login as: "+ txtLogin.getText());
                        frame1.setVisible(false);
                    
                }
            });
            
            btnClear.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                    txtLogin.setText(null);
                    pass2.setText(null);
                }
            });
          btnViewStud.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent act2) {
                        frame2.setVisible(false);
                        frame3.setVisible(true);
                        btnViewGrad.setEnabled(false);
                        
                       
                }
            });
            
            btnAddStud.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent act3) {
                    cmbx.setSelectedIndex(0);
                        frame4.setVisible(true);
                        frame2.setVisible(false);
                    
                }
            });
            
            btnSave.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                     if(cmbx.getSelectedIndex()==0 || txtFirst.getText().isEmpty() || txtLast.getText().isEmpty() 
                         || txtMid.getText().isEmpty()  || txtBirth.getText().isEmpty() || txtStudNum.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(txtBirth.getText());
                        sRecord.setName(txtLast.getText(), txtFirst.getText(), txtMid.getText());
                        sRecord.setStudentNo(txtStudNum.getText());
                        records.add(sRecord);
                        JOptionPane.showMessageDialog(null, "["+sRecord.getStudentNo()+"] "+sRecord.getName()+" has been added to the database.", "Success!", JOptionPane.INFORMATION_MESSAGE);
                        btnViewGrad.setEnabled(true);
                        frame4.setVisible(false);
                        frame6.setVisible(true);
                        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();
                
            }
           Mdl1.addRow(data);
            
            
                    lblStudNum1 = new JLabel("Student #:        \t" +txtStudNum.getText());
                    lblName = new JLabel("Name:        \t"+txtLast.getText()+ " " + txtFirst.getText()+ ", " + txtMid.getText());
                    lblCourse1 = new JLabel("Course:        \t"+cmbx.getSelectedItem().toString());
                   Pnl4.add(lblName);
                   Pnl4.add(lblCourse1);
                   Pnl4.add(lblStudNum1);
                     }
                }
            });
            
            btnClear1.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                    clearFields();
                }
            });
            
            btnSave1.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                     frame6.setVisible(false);
                     frame5.setVisible(true);
                     Object citem = cmbx.getSelectedItem();
                     lblStudNum2 = new JLabel("Student #:       \t"+txtStudNum.getText());
                     lblName1 = new JLabel("Name:        \t"+txtLast.getText()+","+txtFirst.getText()+" "+txtMid.getText());// on Visual 4 Frame
                     lblCourse2 = new JLabel("Course:        \t"+citem);
                     lblMath2 = new JLabel("Math:        \t"+txtMath.getText());
                     lblScience2 = new JLabel("Science:        \t"+txtScience.getText());
                     lblEnglish2 = new JLabel("English:         \t"+txtEnglish.getText());
                     lblMakabayan2 = new JLabel("Makabayan:      \t"+txtMakabayan.getText());
                     frame5.add(lblStudNum2);
                     frame5.add(lblName1);
                     frame5.add(lblCourse2);
                     frame5.add(lblMath2);
                     frame5.add(lblScience2);
                     frame5.add(lblEnglish2);
                     frame5.add(lblMakabayan2);
                     frame5.add(btnClose);
                     
                }
            });
            
            
            
            btnClear2.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                    txtMath.setText(null);
                    txtScience.setText(null);
                    txtEnglish.setText(null);
                    txtMakabayan.setText(null);
                }
            });
            
            btnBack.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                    frame3.setVisible(false);
                    frame2.setVisible(true);
                }
            });
            
            btnViewGrad.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                    frame5.setVisible(true);
                }
            });
            
            btnAddNewStud.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                   cmbx.setSelectedIndex(0);
                   frame3.setVisible(false);
                   frame4.setVisible(true);
                   Btn11clearFields();
                }
            });
            
            btnClose.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                    frame5.setVisible(false);
                    frame3.setVisible(true);
                    
                    
                }
            }); 
           
        
            
        }
        
         private void clearFields()
        {
            txtStudNum.setText(null);
            txtLast.setText(null);
            txtFirst.setText(null);
            txtMid.setText(null);
            txtBirth.setText(null);
        }
         
         private void Btn11clearFields(){
            txtStudNum.setText(null);
            txtLast.setText(null);
            txtFirst.setText(null);
            txtMid.setText(null);
            txtMath.setText(null);
            txtScience.setText(null);
            txtEnglish.setText(null);
            txtMakabayan.setText(null);
         }
        
        
        public static void main(String[]args){
        new Trypack();
            }
        private JButton btnLogin,btnClear,btnViewStud,btnAddStud,btnSave,btnClear1,btnSave1,btnClear2,btnBack,btnViewGrad,btnAddNewStud,btnClose;
        private JLabel lblUser,lblPass,lblStudNum,lblLastName,lblFirstName,lblCourse,lblMidName,lblMath,lblScience,lblEnglish,lblMakabayan,lblBirth,lblName;
        private JLabel lblStudNum1,lblStudNum2,lblCourse1,lblCourse2,lblMath2,lblScience2,lblEnglish2,lblMakabayan2,lblName1;
        private JPasswordField pass2;
        private JTextField txtLogin,txtStudNum,txtLast,txtFirst,txtMid,txtMath,txtScience,txtEnglish,txtMakabayan,txtBirth;
        private JTable table;
        private JScrollPane scrollPane;
        
        }
    And this is my new error
    JTable Problem here!!-sample4n.jpg
    Java Code:
     btnSave.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                     if(cmbx.getSelectedIndex()==0 || txtFirst.getText().isEmpty() || txtLast.getText().isEmpty() 
                         || txtMid.getText().isEmpty()  || txtBirth.getText().isEmpty() || txtStudNum.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(txtBirth.getText());
                        sRecord.setName(txtLast.getText(), txtFirst.getText(), txtMid.getText());
                        sRecord.setStudentNo(txtStudNum.getText());
                        records.add(sRecord);
                        JOptionPane.showMessageDialog(null, "["+sRecord.getStudentNo()+"] "+sRecord.getName()+" has been added to the database.", "Success!", JOptionPane.INFORMATION_MESSAGE);
                        btnViewGrad.setEnabled(true);
                        frame4.setVisible(false);
                        frame6.setVisible(true);
                        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();
                
            }
           Mdl1.addRow(data);
            
            
                    lblStudNum1 = new JLabel("Student #:        \t" +txtStudNum.getText());
                    lblName = new JLabel("Name:        \t"+txtLast.getText()+ " " + txtFirst.getText()+ ", " + txtMid.getText());
                    lblCourse1 = new JLabel("Course:        \t"+cmbx.getSelectedItem().toString());
                   Pnl4.add(lblName);
                   Pnl4.add(lblCourse1);
                   Pnl4.add(lblStudNum1);
                     }
                }
            });
    i can't edit my main post because the "Edit post button" is dissappearing.
    Last edited by raffs03; 03-03-2013 at 06:30 AM.

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

    Default Re: JTable Problem here!!

    Each row of a table model is one single array of object or a Vector<Object>, and so each cell on the row should hold a single object. Your table display is showing you that each cell in your table holds an array of String, and so your rows must be arrays of arrays or Vectors of arrays. Your code in fact shows that it is an array of an array which is not correct.

    Before you venture further, I strongly urge you to follow Jim's excellent advice as it won't lead you wrong:

    Quote Originally Posted by jim829 View Post
    ...I recommend that before you attempt to integrate any of these into a big project you write some small apps to try various features of the class. For JTable, just create one and try adding and removing entries to columns and rows, without bothering with user input from other panels. Use the DefaultTableModel as PhHein suggested. Once you are familiar with how they operate, then try more advanced stuff and then integrate it into your project.

  15. #15
    raffs03 is offline Member
    Join Date
    Sep 2012
    Posts
    41
    Rep Power
    0

    Default Re: JTable Problem here!!

    Hey i manage to fix my JTable Problem and my project is nearly done!
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.util.*;
    import javax.swing.table.*;
    import java.awt.event.*;
    
    
    
    
    
    public class Trypack {
         
        private String [][] data;
        private ArrayList<StudentRecord> records = new ArrayList<>();
               
        public Trypack(){
         
           final DefaultTableModel Mdl1 = new DefaultTableModel();
           
           
                table = new JTable(Mdl1)
                    {
    
                @Override
                public boolean isCellEditable(int row, int column) {
                    return false;
                } 
            };
           
                
           Mdl1.addColumn("Student no.");
           Mdl1.addColumn("Name");
           Mdl1.addColumn("Course");
           Mdl1.addColumn("Date of Birth");
           Mdl1.addColumn("Math Gtade");
           Mdl1.addColumn("English Gtade");
           Mdl1.addColumn("Science Gtade");
           Mdl1.addColumn("Makabayan Grade");
            
            
            scrollPane = new JScrollPane(table);
            final JFrame frame1 = new JFrame();
            final JFrame frame2 = new JFrame();
            final JFrame frame3 = new JFrame();
            final JFrame frame4 = new JFrame();
            final JFrame frame5 = new JFrame();
            final JFrame frame6 = new JFrame();
            final  JPanel Pnl1 = new JPanel();
            final  JPanel Pnl2 = new JPanel();
            final  JPanel Pnl3 = new JPanel();
            final  JPanel Pnl4 = new JPanel();
            final  JPanel Pnl5 = new JPanel();
            final  JPanel Pnl6 = 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"});
              
              btnLogin = new JButton("Login");
              btnClear = new JButton("Clear");
              btnViewStud = new JButton("View Students");
              btnAddStud = new JButton("Add Students");
              btnSave = new JButton("Save"); 
              btnClear1 = new JButton("Clear");
              btnSave1 = new JButton("Save");
              btnClear2 = new JButton("Clear");
              btnBack = new JButton("Back");
              btnViewGrad = new JButton("View Grades");
              btnAddNewStud = new JButton("Add new Student");
              btnClose = new JButton("Close");
              
              lblUser = new JLabel("Username: ");
              lblPass = new JLabel("Password: ");
              lblStudNum = new JLabel("Student #: ");
              lblLastName = new JLabel("Lastname: ");
              lblFirstName = new JLabel("FirstName: ");
              lblCourse = new JLabel("Course: ");
              lblMidName = new JLabel("Middle Name: ");
              lblMath = new JLabel("Math Grade: ");
              lblScience = new JLabel("Science Grade: ");
              lblEnglish = new JLabel("English Grade: ");
              lblMakabayan = new JLabel("Makabayan Grade: ");
              lblBirth = new JLabel("Date of Birth: ");
              
              txtLogin = new JTextField();        
              pass2 = new JPasswordField();
              pass2.setColumns(10);
              txtStudNum = new JTextField();
              txtLast = new JTextField();
              txtFirst = new JTextField();
              txtMid = new JTextField();
              txtMath = new JTextField();
              txtScience = new JTextField();
              txtEnglish = new JTextField();
              txtMakabayan = new JTextField();
              txtBirth = new JTextField();
              
              menuBar.add(menu1);
              menuBar.add(menu2);
              menu1.add(Item1);
              
              
            frame1.add(lblUser);
            frame1.add(txtLogin);
            frame1.add(lblPass);
            frame1.add(pass2);
            frame1.add(btnLogin);
            frame1.add(btnClear);
            
            
            
            Pnl1.setAlignmentX(JPanel.TOP_ALIGNMENT);
            Pnl1.setLayout(new BorderLayout());
            Pnl1.setVisible(true);
            
            Pnl2.setAlignmentX(JPanel.CENTER_ALIGNMENT);
            Pnl2.setLayout(new GridLayout());
            Pnl2.setVisible(true);
          
            Pnl3.setAlignmentX(JPanel.BOTTOM_ALIGNMENT);
            Pnl3.setLayout(new GridLayout(1,3));
            Pnl3.setVisible(true);
            
           
            Pnl4.setAlignmentX(JPanel.TOP_ALIGNMENT);
            Pnl4.setLayout(new GridLayout(3,1));
            Pnl4.setVisible(true);
            
            Pnl5.setAlignmentX(JPanel.CENTER_ALIGNMENT);
            Pnl5.setLayout(new GridLayout(4,1));
            Pnl5.setVisible(true);
            
            Pnl6.setAlignmentX(JPanel.BOTTOM_ALIGNMENT);
            Pnl6.setLayout(new GridLayout(1,2));
            Pnl6.setVisible(true);
            
            frame2.setVisible(false);
            frame2.setSize(350,150);
            frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame2.setLayout(new GridLayout(3,1));
            frame2.setLocationRelativeTo(null);
                      
                    Pnl1.add(scrollPane,BorderLayout.CENTER);
                    Pnl2.add(table);
                    Pnl3.add(btnViewGrad);
                    Pnl3.add(btnAddNewStud);
                    Pnl3.add(btnBack);
            
                    
            frame3.setVisible(false);
            frame3.setSize(950, 350);
            frame3.setTitle("Manage Your Students");
            frame3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame3.setLayout(new GridLayout(2,1));
            frame3.setLocationRelativeTo(null);
            frame3.getContentPane().add(new JScrollPane(table));
            frame3.getContentPane().add(Pnl3);
            
            frame4.setVisible(false);
            frame4.setSize(350,300);
            frame4.setTitle("Adding Students");
            frame4.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame4.setLayout(new GridLayout(7,2));
            frame4.setLocationRelativeTo(null);     
            
            frame5.setVisible(false);
            frame5.setSize(320,320);
            frame5.setTitle("Viewing Grades");
            frame5.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame5.setLayout(new GridLayout(8,1));
            frame5.setLocationRelativeTo(null);
            
            
            frame6.setVisible(false);
            frame6.setSize(300,400);
            frame6.setTitle("Adding Grades");
            frame6.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame6.setLayout(new GridLayout(3,2));
            frame6.setLocationRelativeTo(null);
            frame6.getContentPane().add(Pnl4);
            frame6.getContentPane().add(Pnl5);
            frame6.getContentPane().add(Pnl6);
            
            frame1.setVisible(true);
            frame1.setSize(350,150);
            frame1.setLayout(new GridLayout(3,2));
            frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame1.setLocationRelativeTo(null);
            frame1.setTitle("Login Your Account");
              
                    
            frame2.add(menuBar);
            frame2.add(btnViewStud);
            frame2.add(btnAddStud);
            
            frame4.add(lblStudNum);
            frame4.add(txtStudNum);
            frame4.add(lblLastName);
            frame4.add(txtLast);
            frame4.add(lblFirstName);
            frame4.add(txtFirst);
            frame4.add(lblMidName);
            frame4.add(txtMid);
            frame4.add(lblBirth);
            frame4.add(txtBirth);
            frame4.add(lblCourse);
            frame4.add(cmbx);
            frame4.add(btnSave);
            frame4.add(btnClear1);
            
            
            Pnl5.add(lblMath);
            Pnl5.add(txtMath);
            Pnl5.add(lblScience);
            Pnl5.add(txtScience);
            Pnl5.add(lblEnglish);
            Pnl5.add(txtEnglish);
            Pnl5.add(lblMakabayan);
            Pnl5.add(txtMakabayan);
            Pnl6.add(btnSave1);
            Pnl6.add(btnClear2);
            
            
           Item1.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent act1) {
                    int lo = JOptionPane.showConfirmDialog(null, "Are You Sure You Want To Logout?", "Logout", JOptionPane.YES_NO_OPTION);
                    if(lo == JOptionPane.YES_OPTION)
                    {
                        txtLogin.setText(null);
                        pass2.setText(null);
                        frame1.setVisible(true);
                    }
                }
            });
            
            btnLogin.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                        frame2.setVisible(true);
                        frame2.setTitle("Login as: "+ txtLogin.getText());
                        frame1.setVisible(false);
                    
                }
            });
            
            btnClear.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                    txtLogin.setText(null);
                    pass2.setText(null);
                }
            });
          btnViewStud.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent act2) {
                        frame2.setVisible(false);
                         frame3.setVisible(true);
                        btnViewGrad.setEnabled(false);
                        
                       
                }
            });
            
            btnAddStud.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent act3) {
                    cmbx.setSelectedIndex(0);
                        frame4.setVisible(true);
                        frame2.setVisible(false);
                    
                }
            });
            
            btnSave.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                    int i =0;
                     if(cmbx.getSelectedIndex()==0 || txtFirst.getText().isEmpty() || txtLast.getText().isEmpty() 
                         || txtMid.getText().isEmpty()  || txtBirth.getText().isEmpty() || txtStudNum.getText().isEmpty())
                         {
                        JOptionPane.showMessageDialog(null, "Please complete all required fields before saving.", "Input Required", 
                                + JOptionPane.ERROR_MESSAGE);
                         }
                     else{
                         
                       btnViewGrad.setEnabled(true);
                        frame4.setVisible(false);
                        frame6.setVisible(true);
                        
                    lblStudNum1 = new JLabel("Student #:        \t" +txtStudNum.getText());
                    lblName = new JLabel("Name:        \t"+txtLast.getText()+ " " + txtFirst.getText()+ ", " + txtMid.getText());
                    lblCourse1 = new JLabel("Course:        \t"+cmbx.getSelectedItem().toString());
                   Pnl4.add(lblName);
                   Pnl4.add(lblCourse1);
                   Pnl4.add(lblStudNum1);
                   
                     }
                }
            });
            
            btnClear1.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                    clearFields();
                }
            });
            
            btnSave1.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                     frame6.setVisible(false);
                     frame5.setVisible(true);
                     Object citem = cmbx.getSelectedItem();
                     lblStudNum2 = new JLabel("Student #:       \t"+txtStudNum.getText());
                     lblName1 = new JLabel("Name:        \t"+txtLast.getText()+","+txtFirst.getText()+" "+txtMid.getText());// on Visual 4 Frame
                     lblCourse2 = new JLabel("Course:        \t"+citem);
                     lblMath2 = new JLabel("Math:        \t"+txtMath.getText());
                     lblScience2 = new JLabel("Science:        \t"+txtScience.getText());
                     lblEnglish2 = new JLabel("English:         \t"+txtEnglish.getText());
                     lblMakabayan2 = new JLabel("Makabayan:      \t"+txtMakabayan.getText());
                    frame5.add(lblStudNum2);
                     frame5.add(lblName1);
                     frame5.add(lblCourse2);
                     frame5.add(lblMath2);
                     frame5.add(lblScience2);
                     frame5.add(lblEnglish2);
                     frame5.add(lblMakabayan2);   
                     frame5.add(btnClose);
                     
                }
            });
            
            
            
            btnClear2.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                    txtMath.setText(null);
                    txtScience.setText(null);
                    txtEnglish.setText(null);
                    txtMakabayan.setText(null);
                }
            });
            
            btnBack.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                    frame3.setVisible(false);
                    frame2.setVisible(true);
                }
            });
            
            btnViewGrad.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                    frame5.setVisible(true);
                }
            });
            
            btnAddNewStud.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                   cmbx.setSelectedIndex(0);
                   frame3.setVisible(false);
                   frame4.setVisible(true);
                   Btn11clearFields();
                }
            });
            
            btnClose.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                    frame5.setVisible(false);
                    frame3.setVisible(true);
                    StudentRecord sRecord = new StudentRecord();
                        sRecord.setCourse(cmbx.getSelectedItem().toString());
                        sRecord.setDateOfBirth(txtBirth.getText());
                        sRecord.setName(txtLast.getText(), txtFirst.getText(), txtMid.getText());
                        sRecord.setStudentNo(txtStudNum.getText());
                        records.add(sRecord);
                        String[] socrates = {sRecord.getStudentNo(),sRecord.getName(),sRecord.getCourse(),sRecord.getDateOfBirth(),
    txtMath.getText(),txtScience.getText(),
    txtEnglish.getText(),txtMakabayan.getText()};
                        Mdl1.addRow(socrates);
                    JOptionPane.showMessageDialog(null, "["+sRecord.getStudentNo()+"] "+sRecord.getName()+" has been added to the database.", "Success!", JOptionPane.INFORMATION_MESSAGE);
            
                    
                    
                }
            }); 
           
        
            
        }
        
         private void clearFields()
        {
            txtStudNum.setText(null);
            txtLast.setText(null);
            txtFirst.setText(null);
            txtMid.setText(null);
            txtBirth.setText(null);
        }
         
         private void Btn11clearFields(){
            txtStudNum.setText(null);
            txtLast.setText(null);
            txtFirst.setText(null);
            txtMid.setText(null);
            txtMath.setText(null);
            txtScience.setText(null);
            txtEnglish.setText(null);
            txtMakabayan.setText(null);
         }
        
        
        public static void main(String[]args){
        new Trypack();
            }
        private JButton btnLogin,btnClear,btnViewStud,btnAddStud,btnSave,btnClear1,btnSave1,btnClear2,btnBack,btnViewGrad,btnAddNewStud,btnClose;
        private JLabel lblUser,lblPass,lblStudNum,lblLastName,lblFirstName,lblCourse,lblMidName,lblMath,lblScience,lblEnglish,lblMakabayan,lblBirth,lblName;
        private JLabel lblStudNum1,lblStudNum2,lblCourse1,lblCourse2,lblMath2,lblScience2,lblEnglish2,lblMakabayan2,lblName1;
        private JPasswordField pass2;
        private JTextField txtLogin,txtStudNum,txtLast,txtFirst,txtMid,txtMath,txtScience,txtEnglish,txtMakabayan,txtBirth;
        private JTable table;
        private JScrollPane scrollPane;
        
        }
    I want to solve this problem
    JTable Problem here!!-pample.jpg
    The Name,Student# and Course is multiplying everytime i add a new student
    and i want to use an Arraylist to show the output instead of using this code
    Java Code:
    String[] socrates = {sRecord.getStudentNo(),sRecord.getName(),sRecord.getCourse(),sRecord.getDateOfBirth(),
    txtMath.getText(),txtScience.getText(),
    txtEnglish.getText(),txtMakabayan.getText()};
    Mdl1.addRow(socrates);
    But everytime i attempt to put the arraylist on the addrow the output doesn't show instead i get this output "Java Lang String @3deee00d"
    is there a way on how to put arraylist value to the defaulttablemodel???

Similar Threads

  1. JTable problem
    By ebiwari@tenece.com in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2011, 12:59 PM
  2. JTable problem
    By sandeepsai39 in forum New To Java
    Replies: 2
    Last Post: 06-27-2009, 06:15 AM
  3. JTABLE problem
    By MaximInf in forum AWT / Swing
    Replies: 4
    Last Post: 06-21-2008, 10:45 AM
  4. JTable problem
    By robbertds1 in forum AWT / Swing
    Replies: 1
    Last Post: 05-20-2008, 06:34 PM
  5. Replies: 2
    Last Post: 05-15-2008, 10:15 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
  •