Results 1 to 17 of 17
  1. #1
    Silvertale is offline Member
    Join Date
    May 2014
    Posts
    11
    Rep Power
    0

    Default Textfield not updating text value

    So I've been working on an extensive java gui program that tracks inventory. At one point in my pprogram I have the user double click on a table and the program is supposed to automatically load a new jframe with several jTextFields that are to be filled out by the frame from which it was launched. However, the graphical part of the textboxes are not showing any change. I can use getText to obtain the correct value, but the value doesn't show in the actual textbox. I've tried repainting and established that it isn't a repaint problem. Also, if I try to access the value from outside the function, I get a NULL, and not the text I want. It seams that the text only lives within the functions called from the main jframe's mouseClicked method. I might also mention I'm using netbeans with it's gui editor, so these jframes are not overridden or anything.


    the second frame is called from here:
    Java Code:
     @Override
        public void mouseClicked(MouseEvent me) {
            if(me.getComponent() == inventoryTable){
    			if(me.getClickCount() == 2){//TODO make cells non-editable
                                this.product1.setName("blob");
    			System.out.println("Row: " + (inventoryTable.getSelectedRow()+1) + " was selected!");//starts with 0, not 1!
                            final ProductEditView editWindow = new ProductEditView();
                            Runnable r = new Runnable() {//start in new thread
                                @Override
                                public void run() {
                                    editWindow.showMe(product1);
                                    editWindow.setProduct(product1); <-------each jframe has a product which is another class that just stores the string values I need to transfer between frames!
                                    //editWindow.getProduct().setName("testVal");//passes through fine
                                    //editWindow.getProduct().setUnitsInStock(123);
                                    editWindow.setUIFromProduct(product1);
                                    
                                    
                                    
                 
                                }
                            };
    
            new Thread(r).start(); 
            
    			}
    		}
        }






    that calls this in the second frame:

    Java Code:
    public void setUIFromProduct(Product product){//TODO set in design mode indevidually!
        //TODO everything in here is deleted!
    
        System.out.println("here: " + product1.getName());<------gets the string value just fine
        productNameUI.setText(product1.getName());<-------text not shown here
    }

    Does anyone have an idea? I've tried commenting out the runnable thinking it was a threadding problem, but no luck.
    Last edited by Silvertale; 05-02-2014 at 06:46 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Textfield not updating text value

    Can you make a small, complete program that compiles, executes and shows the problem?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Silvertale is offline Member
    Join Date
    May 2014
    Posts
    11
    Rep Power
    0

    Default Re: Textfield not updating text value

    Sure, I'll get right on that.

  4. #4
    Silvertale is offline Member
    Join Date
    May 2014
    Posts
    11
    Rep Power
    0

    Default Re: Textfield not updating text value

    So I got a smaller version of the program up with the same problem. The first frame calls the second frame when the button is clicked and is supposed to update the second frame's Data object (similar to the product object in my earlier post) from it's own Data object. Then it's supposed to call the second frame's updateGUI method. this is called, but doesn't seam to work. Here's the code:


    Java Code:
    public class mainView extends javax.swing.JFrame {
    
        /**
         * Creates new form mainView
         */
        public mainView() {
            initComponents();
        }
    
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
    
            data1 = new Data();
            jTextField1 = new javax.swing.JTextField();
            jButton2 = new javax.swing.JButton();
            jButton3 = new javax.swing.JButton();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            jTextField1.setText("name");
    
            jButton2.setText("Update Data");
            jButton2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton2ActionPerformed(evt);
                }
            });
    
            jButton3.setText("Go!");
            jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    jButton3MouseClicked(evt);
                }
            });
            jButton3.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton3ActionPerformed(evt);
                }
            });
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(74, 74, 74)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 319, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addContainerGap(429, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(49, 49, 49)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(58, 58, 58)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButton2)
                        .addComponent(jButton3))
                    .addContainerGap(372, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>                        
    
        private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
                this.data1.setName(this.jTextField1.getText());
        }                                        
    
        private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    
        }                                        
    
        private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {                                      
                //I've tried using runnables here, but the problem is not threadding. 
                secondView sv = new secondView();//makes a second window that has a Data object
                sv.showMe();//shows the new frame
                sv.data1.setData(data1);//sets the Data object of the new frame            
                sv.updateGui();//updates the gui to match what is in the Data object
        }                                     
    
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new mainView().setVisible(true);
                }
            });
        }
    
        // Variables declaration - do not modify                     
        private Data data1;
        private javax.swing.JButton jButton2;
        private javax.swing.JButton jButton3;
        private javax.swing.JTextField jTextField1;
        // End of variables declaration                   
    }

    secondView's code:

    Java Code:
    public class secondView extends javax.swing.JFrame {
    
        /**
         * Creates new form secondView
         */
        public secondView() {
            initComponents();
        }
    
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
    
            data1 = new Data();
            jTextField1 = new javax.swing.JTextField();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            jTextField1.setText("No Data Yet!");
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(161, 161, 161)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 281, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(340, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(143, 143, 143)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(397, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>                        
    
        /**
         * @param args the command line arguments
         */
        public static void showMe() {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new secondView().setVisible(true);
                }
            });
        }
        
        public void updateGui(){
            System.out.println(this.data1.getName());//prints out name just fine
            this.jTextField1.setText(this.data1.getName());//dousn't update the giu to show "name"
            System.out.println(this.jTextField1.getText());//outputs "name"
        }
        
        // Variables declaration - do not modify                     
        public Data data1;
        private javax.swing.JTextField jTextField1;
        // End of variables declaration                   
    }
    the data class looks like so:

    Java Code:
    public class Data {
        String name;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
        
        public void setData(Data d){
            this.name = d.getName();
        }
        
    }

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Textfield not updating text value

    There are two buttons. What does a user do to test the code?


    This generated code is ugly and hard to work with.
    I see two button variable named ..1 and ..2. I can not tell Which does what?
    The same for all the variable names. They have suffix numbers but no description of what they hold or are used for.

    Recommend you try debugging the code by adding some println statements that print out the values of variables as their values are changed and to print messages when methods are called to see the execution flow.
    Last edited by Norm; 05-04-2014 at 11:06 PM.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    Silvertale is offline Member
    Join Date
    May 2014
    Posts
    11
    Rep Power
    0

    Default Re: Textfield not updating text value

    The idea is that you type something into jTextField1 and then hit jButton2 (update button). This updates the mainView's Data object. Then you click jButton1 which constructs the secondView frame and sets it's Data object and supposedly updates the gui (secondView's jTextField1) to reflect the Data. I know the generated code is a mess, but it's all the normal stuff and shouldn't really be causing any problems. It's just init'ing the GUI components and setting their default values and positions. The thig to really look at is in MainView's mouseClicked method and secondView's updateGUI method. Also, I know that I should have a test to see what component is procuding the mouseClicked, but this is just a test to fix a bug, not real software.

    The needed println statements are already in there as you'll see if you look at the code. there's only really one relevant variable that needs to be tracked.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Textfield not updating text value

    The needed println statements are already in there
    There are not enough println() statements to show what is happening.

    Copy what is printed out and paste it here so I can see what is printed that shows where the code is executing and what is happening.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    Silvertale is offline Member
    Join Date
    May 2014
    Posts
    11
    Rep Power
    0

    Default Re: Textfield not updating text value

    Ok, I've put more println in for absolutly everything. the main problem is in secondView's updateGUI, or at least that's where it is visible. Here's the updated code:


    MainView:
    Java Code:
    public class mainView extends javax.swing.JFrame {
    
        /**
         * Creates new form mainView
         */
        public mainView() {
            initComponents();
        }
    
        @SuppressWarnings("unchecked")                          
        private void initComponents() {
    
            data1 = new Data();
            jTextField1 = new javax.swing.JTextField();
            jButton2 = new javax.swing.JButton();
            jButton3 = new javax.swing.JButton();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            jTextField1.setText("name");
    
            jButton2.setText("Update Data");
            jButton2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton2ActionPerformed(evt);
                }
            });
    
            jButton3.setText("Go!");
            jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    jButton3MouseClicked(evt);
                }
            });
            jButton3.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton3ActionPerformed(evt);
                }
            });
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(74, 74, 74)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 319, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addContainerGap(429, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(49, 49, 49)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(58, 58, 58)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButton2)
                        .addComponent(jButton3))
                    .addContainerGap(372, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>                        
    
        private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
                this.data1.setName(this.jTextField1.getText());
        }                                        
    
        private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    
        }                                        
    
        private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {                                      
                //I've tried using runnables here, but the problem is not threadding.
                System.out.println("in buttonClicked method");
                secondView sv = new secondView();//makes a second window that has a Data object
                sv.showMe();//shows the new frame
                System.out.println("mainview's data.name: " + data1.getName());
                sv.data1.setData(data1);//sets the Data object of the new frame
                System.out.println("secondView's data.name: " + sv.data1.getName());//sv has public accessors for debugging perposes
                sv.updateGui();//updates the gui to match what is in the Data object
        }                                     
    
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new mainView().setVisible(true);
                }
            });
        }
    
        // Variables declaration - do not modify                     
        private Data data1;
        private javax.swing.JButton jButton2;
        private javax.swing.JButton jButton3;
        private javax.swing.JTextField jTextField1;
        // End of variables declaration                   
    }
    SecondView:

    Java Code:
    import java.util.Set;
    
    public class secondView extends javax.swing.JFrame {
    
        /**
         * Creates new form secondView
         */
        public secondView() {
            initComponents();
        }
    
        @SuppressWarnings("unchecked")                       
        private void initComponents() {
    
            data1 = new Data();
            jTextField1 = new javax.swing.JTextField();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            jTextField1.setText("No Data Yet!");//jTextField's default value (says "no data yet" if the data has not been propperly set)
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(161, 161, 161)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 281, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(340, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(143, 143, 143)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(397, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>                        
    
        /**
         * @param args the command line arguments
         */
        public static void showMe() {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new secondView().setVisible(true);
                }
            });
        }
        
        public void updateGui(){//THIS IS WHERE THE PROBLEM IS VISIBLE!!!
    
            System.out.println("printout of what was set in secondView's data.name: " + this.data1.getName());//prints out name just fine
            this.jTextField1.setText(this.data1.getName());//dousn't update the giu to show "name"
            System.out.println("printout of the text that was set for jTextField1: " + this.jTextField1.getText());//outputs "name"
            //not if I look at the GUI of secondview however, the textfield still shows "not working" which is it's default value
        }
        
        // Variables declaration - do not modify                     
        public Data data1;
        private javax.swing.JTextField jTextField1;
        // End of variables declaration                   
    }

    The data class is still the same. Here is my output:
    in buttonClicked method
    mainview's data.name: name
    secondView's data.name: name
    printout of what was set in secondView's data.name: name
    printout of the text that was set for jTextField1: name

    the last statement shows that the jTextField.setText() did set jTextField's text, however, the actual GUI's text is still it's default ("no data yet"). As I've said, it's not a repain problem. I've already tried putting repaint statements in the updateGUI method to no avail. Also, if I put in a method outside of the updateGUI and check secondView's data it will say it's empty, so somehow the variable only lives as long as it's in mainView's mouse clicked or secondView's updateGUI.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Textfield not updating text value

    When debugging a problem you need to print out messages for all the events that happen. There should be a message printed at the start of all the constructors and at the start of all the methods so that you can see where the execution flow goes.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    Silvertale is offline Member
    Join Date
    May 2014
    Posts
    11
    Rep Power
    0

    Default Re: Textfield not updating text value

    Ok, heres the newest version with printlns in every sindle function. You might want to look at the incode comments too since they show exactly what's going on.

    MAINVIEW:
    Java Code:
    public class mainView extends javax.swing.JFrame {
    
        /**
         * Creates new form mainView
         */
        public mainView() {
            System.out.println("in mainView's constructor");
            initComponents();
        }
    
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
    
            data1 = new Data();
            jTextField1 = new javax.swing.JTextField();
            jButton2 = new javax.swing.JButton();
            jButton3 = new javax.swing.JButton();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            jTextField1.setText("name");
    
            jButton2.setText("Update Data");
            jButton2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton2ActionPerformed(evt);
                }
            });
    
            jButton3.setText("Go!");
            jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    jButton3MouseClicked(evt);
                }
            });
            jButton3.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton3ActionPerformed(evt);
                }
            });
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(74, 74, 74)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 319, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addContainerGap(429, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(49, 49, 49)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(58, 58, 58)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButton2)
                        .addComponent(jButton3))
                    .addContainerGap(372, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>                        
    
        private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            System.out.println("jButton2 (updateGui) button pressed on mainView");    
            this.data1.setName(this.jTextField1.getText());
        }                                        
    
        private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            System.out.println("jButton3 (mainView's call to secondView) action performed. this is not used!");
        }                                        
    
        private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {                                      
            System.out.println("in mainView's mouseClicked method. This is where the secondView is called and where the problems are showing up");    
        //I've tried using runnables here, but the problem is not threadding.
                System.out.println("in buttonClicked method");
                secondView sv = new secondView();//makes a second window that has a Data object
                sv.showMe();//shows the new frame
                System.out.println("mainview's data.name: " + data1.getName());
                sv.data1.setData(data1);//sets the Data object of the new frame
                System.out.println("secondView's data.name: " + sv.data1.getName());//sv has public accessors for debugging perposes
                sv.updateGui();//updates the gui to match what is in the Data object
        }                                     
    
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            System.out.println("in main");
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    System.out.println("in mainView's main's invokeLater");
                    new mainView().setVisible(true);
                }
            });
        }
    
        // Variables declaration - do not modify                     
        private Data data1;
        private javax.swing.JButton jButton2;
        private javax.swing.JButton jButton3;
        private javax.swing.JTextField jTextField1;
        // End of variables declaration                   
    }

    SECONDVIEW:

    Java Code:
    import java.util.Set;
    
    public class secondView extends javax.swing.JFrame {
    
        /**
         * Creates new form secondView
         */
        public secondView() {
            System.out.println("interface secondView constructor");
            initComponents();
        }
    
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
    
            data1 = new Data();
            jTextField1 = new javax.swing.JTextField();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            jTextField1.setText("No Data Yet!");
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(161, 161, 161)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 281, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(340, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(143, 143, 143)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(397, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>                        
    
        /**
         * @param args the command line arguments
         */
        public static void showMe() {
            System.out.println("interface secondView.showMe");
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    System.out.println("interface secondView.showme's invokeLater");
                    new secondView().setVisible(true);
                }
            });
        }
        
        public void updateGui(){
            System.out.println("interface secondView's updateGUI");
            System.out.println("printout of what was set in secondView's data.name: " + this.data1.getName());//prints out name just fine
            this.jTextField1.setText(this.data1.getName());//dousn't update the giu to show "name"
            System.out.println("printout of the text that was set for jTextField1: " + this.jTextField1.getText());//outputs "name"
            //not if I look at the GUI of secondview however, the textfield still shows "not working" which is it's default value
        }
        
        // Variables declaration - do not modify                     
        public Data data1;
        private javax.swing.JTextField jTextField1;
        // End of variables declaration                   
    }
    DATA:

    Java Code:
    public class Data {
        String name;
        
        public Data(){
            this.name = "";
        }
    
        public String getName() {
            System.out.println("in Data's getName");
            return name;
        }
    
        public void setName(String name) {
            System.out.println("in Data's setName");
            this.name = name;
        }
        
        public void setData(Data d){
            System.out.println("in Data's setData");
            this.name = d.getName();
        }
    output:

    in main
    in mainView's main's invokeLater
    in mainView's constructor
    jButton2 (updateGui) button pressed on mainView
    in Data's setName
    jButton3 (mainView's call to secondView) action performed. this is not used!
    in mainView's mouseClicked method. This is where the secondView is called and where the problems are showing up
    in buttonClicked method
    interface secondView constructor
    interface secondView.showMe
    in Data's getName
    mainview's data.name: name
    in Data's setData
    in Data's getName
    in Data's getName
    secondView's data.name: name
    interface secondView's updateGUI
    in Data's getName
    printout of what was set in secondView's data.name: name
    in Data's getName
    printout of the text that was set for jTextField1: name
    interface secondView.showme's invokeLater
    interface secondView constructor



    I hope that's enough println statements!

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Textfield not updating text value

    Yes that looks better.
    Did you look at what was printed?
    Did you see anything unexpected?
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    Silvertale is offline Member
    Join Date
    May 2014
    Posts
    11
    Rep Power
    0

    Default Re: Textfield not updating text value

    Yes, I looked at it. everything is as expected. Everything can be printed out just fine. The problem is when I try to set the text of secondView's jTextField. I can use jTextField1.getText() to print out the changed value, but that value doens't actually show up on the GUI. it's like I set the text fine, but it doesn't show up on the actual form.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Textfield not updating text value

    everything is as expected.
    interface secondView constructor
    That line is printed two times. That means that 2 instances of secondView were created. But only one of them is shown.
    Where is the other one? Should it be shown also?
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    Silvertale is offline Member
    Join Date
    May 2014
    Posts
    11
    Rep Power
    0

    Default Re: Textfield not updating text value

    hmm, that's interesting. I didn't see that. Also, that should be printing out "in secondView constructor", not interface... stupid word templates. There shouldn't be a second instance. Any idea why it's making two SecondViews?

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Textfield not updating text value

    why it's making two SecondViews
    Look in the code for any place there is a new statement for the secondView class. Each new statement will create a new instance of the class.
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    Silvertale is offline Member
    Join Date
    May 2014
    Posts
    11
    Rep Power
    0

    Default Re: Textfield not updating text value

    I found it! Thanks so much for pointing that out. turns out I was constructing the secondView once in the constructor, and once in MainView. It works great now :) Just have to transfer that over to my larger program now.
    I +1ed you. How do I mark the thread as solved?

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Textfield not updating text value

    Glad you got it working.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Checking textfield text
    By jsobel in forum New To Java
    Replies: 4
    Last Post: 06-14-2012, 05:48 AM
  2. Textfield not displaying correct text
    By nksjolinder1 in forum AWT / Swing
    Replies: 6
    Last Post: 10-25-2011, 10:33 PM
  3. Replies: 3
    Last Post: 11-16-2010, 09:43 AM
  4. Replies: 2
    Last Post: 06-14-2010, 11:18 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
  •