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 3 of 3

Thread: Problem with JTable and jFrame

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with JTable and jFrame

    Is it possible that jFrame with form that will fill up are connected to jFrame that have JTable. Here's my code

    import javax.swing.table.DefaultTableModel;
     
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
     
    /**
     *
     * @author Owner
     */
    public class Registration extends javax.swing.JFrame {
     
        /**
         * Creates new form Registration
         */
        public Registration() {
            initComponents();
        }
     
        /**
         * This method is called from within the constructor to initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is always
         * regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
     
            REGISTRATION = new javax.swing.JLabel();
            lStudentno = new javax.swing.JLabel();
            fStudentno = new javax.swing.JTextField();
            lGivename = new javax.swing.JLabel();
            fGivenname = new javax.swing.JTextField();
            lMiddlename = new javax.swing.JLabel();
            fMiddlename = new javax.swing.JTextField();
            lSurname = new javax.swing.JLabel();
            fSurname = new javax.swing.JTextField();
            lCourse = new javax.swing.JLabel();
            fCourse = new javax.swing.JTextField();
            lYear = new javax.swing.JLabel();
            fYear = new javax.swing.JTextField();
            lSection = new javax.swing.JLabel();
            fSection = new javax.swing.JTextField();
            bAdd = new javax.swing.JButton();
            bExit = new javax.swing.JButton();
            lContactno = new javax.swing.JLabel();
            fContactno = new javax.swing.JTextField();
            lEmail = new javax.swing.JLabel();
            fEmail = new javax.swing.JTextField();
            CLEAR = new javax.swing.JButton();
            Reminder = new javax.swing.JLabel();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
     
            REGISTRATION.setFont(new java.awt.Font("Courier New", 1, 36)); // NOI18N
            REGISTRATION.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
            REGISTRATION.setText("REGISTRATION");
     
            lStudentno.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
            lStudentno.setText("Student no.         :");
     
            lGivename.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
            lGivename.setText("Given Name        :");
     
            lMiddlename.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
            lMiddlename.setText("Middle Name       :");
     
            lSurname.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
            lSurname.setText("Surname             :");
     
            lCourse.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
            lCourse.setText("Course                :");
     
            fCourse.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    fCourseActionPerformed(evt);
                }
            });
     
            lYear.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
            lYear.setText("Year                   :");
     
            lSection.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
            lSection.setText("Section               :");
     
            bAdd.setText("ADD");
            bAdd.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    bAddActionPerformed(evt);
                }
            });
     
            bExit.setText("EXIT");
            bExit.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    bExitActionPerformed(evt);
                }
            });
     
            lContactno.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
            lContactno.setText("Contact no.         :");
     
            fContactno.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    fContactnoActionPerformed(evt);
                }
            });
     
            lEmail.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
            lEmail.setText("E-mail                 :");
     
            fEmail.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    fEmailActionPerformed(evt);
                }
            });
     
            CLEAR.setText("CLEAR");
            CLEAR.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    CLEARActionPerformed(evt);
                }
            });
     
            Reminder.setFont(new java.awt.Font("Gungsuh", 1, 18)); // NOI18N
            Reminder.setForeground(new java.awt.Color(255, 0, 51));
     
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(68, 68, 68)
                            .addComponent(REGISTRATION))
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(lStudentno)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(fStudentno, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(lGivename)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(fGivenname, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(lMiddlename)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(fMiddlename, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(lSurname)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(fSurname, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(bAdd)
                                        .addComponent(lSection))
                                    .addGap(27, 27, 27)
                                    .addComponent(CLEAR)
                                    .addGap(41, 41, 41)
                                    .addComponent(bExit))
                                .addComponent(lCourse)
                                .addComponent(lYear)))
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(lContactno)
                                .addComponent(lEmail))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(fEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(fSection, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(fContactno, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(fYear, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(fCourse, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addGap(0, 51, Short.MAX_VALUE)
                    .addComponent(Reminder, javax.swing.GroupLayout.PREFERRED_SIZE, 307, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(42, 42, 42))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(30, 30, 30)
                    .addComponent(REGISTRATION, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lStudentno)
                        .addComponent(fStudentno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lGivename)
                        .addComponent(fGivenname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lMiddlename)
                        .addComponent(fMiddlename, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lSurname)
                        .addComponent(fSurname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lCourse)
                        .addComponent(fCourse, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lYear)
                        .addComponent(fYear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lSection)
                        .addComponent(fSection, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lContactno)
                        .addComponent(fContactno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lEmail)
                        .addComponent(fEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(Reminder, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(bAdd)
                        .addComponent(bExit)
                        .addComponent(CLEAR))
                    .addGap(30, 30, 30))
            );
     
            pack();
        }// </editor-fold>                        
     
        private void bAddActionPerformed(java.awt.event.ActionEvent evt) {                                     
            DefaultTableModel model = (DefaultTableModel) tStudents.getModel();
            model.addRow(new Object[]{fStudentno.getText(),fGivenname.getText(),fSurname.getText(),fCourse.getText(),fYear.getText(),fSection.getText()});
        }                                    
     
        private void bExitActionPerformed(java.awt.event.ActionEvent evt) {                                      
            System.exit(0);
        }                                     
     
        private void fContactnoActionPerformed(java.awt.event.ActionEvent evt) {                                           
            // TODO add your handling code here:
        }                                          
     
        private void fEmailActionPerformed(java.awt.event.ActionEvent evt) {                                       
            // TODO add your handling code here:
        }                                      
     
        private void CLEARActionPerformed(java.awt.event.ActionEvent evt) {                                      
            fStudentno.setText("");
            fGivenname.setText("");
            fMiddlename.setText("");
            fSurname.setText("");
            fCourse.setText("");
            fYear.setText("");
            fSection.setText("");
            fContactno.setText("");
            fEmail.setText("");
        }                                     
     
        private void fCourseActionPerformed(java.awt.event.ActionEvent evt) {                                        
            // TODO add your handling code here:
        }                                       
     
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            /* Set the Nimbus look and feel */
            //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
            /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
             * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
             */
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(Registration.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(Registration.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(Registration.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(Registration.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>
     
            /* Create and display the form */
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new Registration().setVisible(true);
                }
            });
        }
     
        // Variables declaration - do not modify                     
        private javax.swing.JButton CLEAR;
        private javax.swing.JLabel REGISTRATION;
        private javax.swing.JLabel Reminder;
        private javax.swing.JButton bAdd;
        private javax.swing.JButton bExit;
        private javax.swing.JTextField fContactno;
        private javax.swing.JTextField fCourse;
        private javax.swing.JTextField fEmail;
        private javax.swing.JTextField fGivenname;
        private javax.swing.JTextField fMiddlename;
        private javax.swing.JTextField fSection;
        private javax.swing.JTextField fStudentno;
        private javax.swing.JTextField fSurname;
        private javax.swing.JTextField fYear;
        private javax.swing.JLabel lContactno;
        private javax.swing.JLabel lCourse;
        private javax.swing.JLabel lEmail;
        private javax.swing.JLabel lGivename;
        private javax.swing.JLabel lMiddlename;
        private javax.swing.JLabel lSection;
        private javax.swing.JLabel lStudentno;
        private javax.swing.JLabel lSurname;
        private javax.swing.JLabel lYear;
        // End of variables declaration                   
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 688 Times in 675 Posts

    Default Re: Problem with JTable and jFrame

    Thread moved.

    Unfortunately, there are very few here (if any) who will slog through the code generated by a graphical GUI designer. Your question is more about how to use the graphical GUI tool than about Java programming. I recommend you find a forum specific to the tool or the IDE and ask your question(s) there.

  3. #3

    Default Re: Problem with JTable and jFrame

    I had the same problem some time ago. This is how I solved it:

    public class JScrollPaneWithTableRowsWithBoxAndStrings extends JScrollPane {
    .................
    public JScrollPaneWithTableRowsWithBoxAndStrings(Boolean smallFont,
    String boxType,
    String[] columnNames,
    String[] data) throws InitCheckException {
    .................
    table.setPreferredScrollableViewportSize(table.get PreferredSize());
    setColumnHeaderView(table.getTableHeader()); // So that JScrollPane's height fits table's height + table's header's height
    setViewportView(table);
    }

    .................
    }[COLOR="Silver"]

Similar Threads

  1. Replies: 1
    Last Post: February 5th, 2014, 09:22 AM
  2. Re: how to hide jtable in jframe
    By Linimol in forum AWT / Java Swing
    Replies: 1
    Last Post: January 31st, 2014, 09:44 AM
  3. problem with my JFrame; JFrame not closing and stay in background
    By golominator in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 6th, 2012, 08:20 AM
  4. how to hide jtable in jframe
    By vitiazaltair in forum AWT / Java Swing
    Replies: 2
    Last Post: February 7th, 2012, 11:12 AM
  5. [B]Insert a JFrame and a JTable in a JSplitPane[/B]
    By sincerelibran in forum AWT / Java Swing
    Replies: 1
    Last Post: January 23rd, 2011, 01:59 PM